001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hdfs.server.namenode;
019
020import java.io.PrintWriter;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Comparator;
024import java.util.List;
025
026import org.apache.hadoop.fs.permission.FsPermission;
027import org.apache.hadoop.fs.permission.PermissionStatus;
028import org.apache.hadoop.hdfs.protocol.QuotaExceededException;
029import org.apache.hadoop.hdfs.server.namenode.snapshot.DirectoryWithSnapshotFeature;
030import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot;
031
032import com.google.common.base.Preconditions;
033import org.mortbay.log.Log;
034
035/**
036 * An anonymous reference to an inode.
037 *
038 * This class and its subclasses are used to support multiple access paths.
039 * A file/directory may have multiple access paths when it is stored in some
040 * snapshots and it is renamed/moved to other locations.
041 * 
042 * For example,
043 * (1) Suppose we have /abc/foo, say the inode of foo is inode(id=1000,name=foo)
044 * (2) create snapshot s0 for /abc
045 * (3) mv /abc/foo /xyz/bar, i.e. inode(id=1000,name=...) is renamed from "foo"
046 *     to "bar" and its parent becomes /xyz.
047 * 
048 * Then, /xyz/bar and /abc/.snapshot/s0/foo are two different access paths to
049 * the same inode, inode(id=1000,name=bar).
050 *
051 * With references, we have the following
052 * - /abc has a child ref(id=1001,name=foo).
053 * - /xyz has a child ref(id=1002) 
054 * - Both ref(id=1001,name=foo) and ref(id=1002) point to another reference,
055 *   ref(id=1003,count=2).
056 * - Finally, ref(id=1003,count=2) points to inode(id=1000,name=bar).
057 * 
058 * Note 1: For a reference without name, e.g. ref(id=1002), it uses the name
059 *         of the referred inode.
060 * Note 2: getParent() always returns the parent in the current state, e.g.
061 *         inode(id=1000,name=bar).getParent() returns /xyz but not /abc.
062 */
063public abstract class INodeReference extends INode {
064  /**
065   * Try to remove the given reference and then return the reference count.
066   * If the given inode is not a reference, return -1;
067   */
068  public static int tryRemoveReference(INode inode) {
069    if (!inode.isReference()) {
070      return -1;
071    }
072    return removeReference(inode.asReference());
073  }
074
075  /**
076   * Remove the given reference and then return the reference count.
077   * If the referred inode is not a WithCount, return -1;
078   */
079  private static int removeReference(INodeReference ref) {
080    final INode referred = ref.getReferredINode();
081    if (!(referred instanceof WithCount)) {
082      return -1;
083    }
084    
085    WithCount wc = (WithCount) referred;
086    wc.removeReference(ref);
087    return wc.getReferenceCount();
088  }
089
090  /**
091   * When destroying a reference node (WithName or DstReference), we call this
092   * method to identify the snapshot which is the latest snapshot before the
093   * reference node's creation. 
094   */
095  static int getPriorSnapshot(INodeReference ref) {
096    WithCount wc = (WithCount) ref.getReferredINode();
097    WithName wn = null;
098    if (ref instanceof DstReference) {
099      wn = wc.getLastWithName();
100    } else if (ref instanceof WithName) {
101      wn = wc.getPriorWithName((WithName) ref);
102    }
103    if (wn != null) {
104      INode referred = wc.getReferredINode();
105      if (referred.isFile() && referred.asFile().isWithSnapshot()) {
106        return referred.asFile().getDiffs().getPrior(wn.lastSnapshotId);
107      } else if (referred.isDirectory()) {
108        DirectoryWithSnapshotFeature sf = referred.asDirectory()
109            .getDirectoryWithSnapshotFeature();
110        if (sf != null) {
111          return sf.getDiffs().getPrior(wn.lastSnapshotId);
112        }
113      }
114    }
115    return Snapshot.NO_SNAPSHOT_ID;
116  }
117  
118  private INode referred;
119  
120  public INodeReference(INode parent, INode referred) {
121    super(parent);
122    this.referred = referred;
123  }
124
125  public final INode getReferredINode() {
126    return referred;
127  }
128
129  public final void setReferredINode(INode referred) {
130    this.referred = referred;
131  }
132  
133  @Override
134  public final boolean isReference() {
135    return true;
136  }
137  
138  @Override
139  public final INodeReference asReference() {
140    return this;
141  }
142
143  @Override
144  public final boolean isFile() {
145    return referred.isFile();
146  }
147  
148  @Override
149  public final INodeFile asFile() {
150    return referred.asFile();
151  }
152  
153  @Override
154  public final boolean isDirectory() {
155    return referred.isDirectory();
156  }
157  
158  @Override
159  public final INodeDirectory asDirectory() {
160    return referred.asDirectory();
161  }
162  
163  @Override
164  public final boolean isSymlink() {
165    return referred.isSymlink();
166  }
167  
168  @Override
169  public final INodeSymlink asSymlink() {
170    return referred.asSymlink();
171  }
172
173  @Override
174  public byte[] getLocalNameBytes() {
175    return referred.getLocalNameBytes();
176  }
177
178  @Override
179  public void setLocalName(byte[] name) {
180    referred.setLocalName(name);
181  }
182
183  @Override
184  public final long getId() {
185    return referred.getId();
186  }
187  
188  @Override
189  public final PermissionStatus getPermissionStatus(int snapshotId) {
190    return referred.getPermissionStatus(snapshotId);
191  }
192  
193  @Override
194  public final String getUserName(int snapshotId) {
195    return referred.getUserName(snapshotId);
196  }
197  
198  @Override
199  final void setUser(String user) {
200    referred.setUser(user);
201  }
202  
203  @Override
204  public final String getGroupName(int snapshotId) {
205    return referred.getGroupName(snapshotId);
206  }
207  
208  @Override
209  final void setGroup(String group) {
210    referred.setGroup(group);
211  }
212  
213  @Override
214  public final FsPermission getFsPermission(int snapshotId) {
215    return referred.getFsPermission(snapshotId);
216  }
217
218  @Override
219  public final AclFeature getAclFeature(int snapshotId) {
220    return referred.getAclFeature(snapshotId);
221  }
222
223  @Override
224  final void addAclFeature(AclFeature aclFeature) {
225    referred.addAclFeature(aclFeature);
226  }
227
228  @Override
229  final void removeAclFeature() {
230    referred.removeAclFeature();
231  }
232  
233  @Override
234  final XAttrFeature getXAttrFeature(int snapshotId) {
235    return referred.getXAttrFeature(snapshotId);
236  }
237  
238  @Override
239  final void addXAttrFeature(XAttrFeature xAttrFeature) {
240    referred.addXAttrFeature(xAttrFeature);
241  }
242  
243  @Override
244  final void removeXAttrFeature() {
245    referred.removeXAttrFeature();
246  }
247
248  @Override
249  public final short getFsPermissionShort() {
250    return referred.getFsPermissionShort();
251  }
252  
253  @Override
254  void setPermission(FsPermission permission) {
255    referred.setPermission(permission);
256  }
257
258  @Override
259  public long getPermissionLong() {
260    return referred.getPermissionLong();
261  }
262
263  @Override
264  public final long getModificationTime(int snapshotId) {
265    return referred.getModificationTime(snapshotId);
266  }
267  
268  @Override
269  public final INode updateModificationTime(long mtime, int latestSnapshotId) {
270    return referred.updateModificationTime(mtime, latestSnapshotId);
271  }
272  
273  @Override
274  public final void setModificationTime(long modificationTime) {
275    referred.setModificationTime(modificationTime);
276  }
277  
278  @Override
279  public final long getAccessTime(int snapshotId) {
280    return referred.getAccessTime(snapshotId);
281  }
282  
283  @Override
284  public final void setAccessTime(long accessTime) {
285    referred.setAccessTime(accessTime);
286  }
287
288  @Override
289  public final byte getStoragePolicyID() {
290    return referred.getStoragePolicyID();
291  }
292
293  @Override
294  public final byte getLocalStoragePolicyID() {
295    return referred.getLocalStoragePolicyID();
296  }
297
298  @Override
299  final void recordModification(int latestSnapshotId) {
300    referred.recordModification(latestSnapshotId);
301  }
302
303  @Override // used by WithCount
304  public Quota.Counts cleanSubtree(int snapshot, int prior,
305      BlocksMapUpdateInfo collectedBlocks, final List<INode> removedINodes) {
306    return referred.cleanSubtree(snapshot, prior, collectedBlocks,
307        removedINodes);
308  }
309
310  @Override // used by WithCount
311  public void destroyAndCollectBlocks(
312      BlocksMapUpdateInfo collectedBlocks, final List<INode> removedINodes) {
313    if (removeReference(this) <= 0) {
314      referred.destroyAndCollectBlocks(collectedBlocks, removedINodes);
315    }
316  }
317
318  @Override
319  public ContentSummaryComputationContext computeContentSummary(
320      ContentSummaryComputationContext summary) {
321    return referred.computeContentSummary(summary);
322  }
323
324  @Override
325  public Quota.Counts computeQuotaUsage(Quota.Counts counts, boolean useCache,
326      int lastSnapshotId) {
327    return referred.computeQuotaUsage(counts, useCache, lastSnapshotId);
328  }
329  
330  @Override
331  public final INodeAttributes getSnapshotINode(int snapshotId) {
332    return referred.getSnapshotINode(snapshotId);
333  }
334
335  @Override
336  public Quota.Counts getQuotaCounts() {
337    return referred.getQuotaCounts();
338  }
339
340  @Override
341  public final void clear() {
342    super.clear();
343    referred = null;
344  }
345
346  @Override
347  public void dumpTreeRecursively(PrintWriter out, StringBuilder prefix,
348      final int snapshot) {
349    super.dumpTreeRecursively(out, prefix, snapshot);
350    if (this instanceof DstReference) {
351      out.print(", dstSnapshotId=" + ((DstReference) this).dstSnapshotId);
352    }
353    if (this instanceof WithCount) {
354      out.print(", count=" + ((WithCount)this).getReferenceCount());
355    }
356    out.println();
357    
358    final StringBuilder b = new StringBuilder();
359    for(int i = 0; i < prefix.length(); i++) {
360      b.append(' ');
361    }
362    b.append("->");
363    getReferredINode().dumpTreeRecursively(out, b, snapshot);
364  }
365  
366  public int getDstSnapshotId() {
367    return Snapshot.CURRENT_STATE_ID;
368  }
369  
370  /** An anonymous reference with reference count. */
371  public static class WithCount extends INodeReference {
372    
373    private final List<WithName> withNameList = new ArrayList<WithName>();
374    
375    /**
376     * Compare snapshot with IDs, where null indicates the current status thus
377     * is greater than any non-null snapshot.
378     */
379    public static final Comparator<WithName> WITHNAME_COMPARATOR
380        = new Comparator<WithName>() {
381      @Override
382      public int compare(WithName left, WithName right) {
383        return left.lastSnapshotId - right.lastSnapshotId;
384      }
385    };
386    
387    public WithCount(INodeReference parent, INode referred) {
388      super(parent, referred);
389      Preconditions.checkArgument(!referred.isReference());
390      referred.setParentReference(this);
391    }
392    
393    public int getReferenceCount() {
394      int count = withNameList.size();
395      if (getParentReference() != null) {
396        count++;
397      }
398      return count;
399    }
400
401    /** Increment and then return the reference count. */
402    public void addReference(INodeReference ref) {
403      if (ref instanceof WithName) {
404        WithName refWithName = (WithName) ref;
405        int i = Collections.binarySearch(withNameList, refWithName,
406            WITHNAME_COMPARATOR);
407        Preconditions.checkState(i < 0);
408        withNameList.add(-i - 1, refWithName);
409      } else if (ref instanceof DstReference) {
410        setParentReference(ref);
411      }
412    }
413
414    /** Decrement and then return the reference count. */
415    public void removeReference(INodeReference ref) {
416      if (ref instanceof WithName) {
417        int i = Collections.binarySearch(withNameList, (WithName) ref,
418            WITHNAME_COMPARATOR);
419        if (i >= 0) {
420          withNameList.remove(i);
421        }
422      } else if (ref == getParentReference()) {
423        setParent(null);
424      }
425    }
426    
427    WithName getLastWithName() {
428      return withNameList.size() > 0 ? 
429          withNameList.get(withNameList.size() - 1) : null;
430    }
431    
432    WithName getPriorWithName(WithName post) {
433      int i = Collections.binarySearch(withNameList, post, WITHNAME_COMPARATOR);
434      if (i > 0) {
435        return withNameList.get(i - 1);
436      } else if (i == 0 || i == -1) {
437        return null;
438      } else {
439        return withNameList.get(-i - 2);
440      }
441    }
442
443    /**
444     * @return the WithName/DstReference node contained in the given snapshot.
445     */
446    public INodeReference getParentRef(int snapshotId) {
447      int start = 0;
448      int end = withNameList.size() - 1;
449      while (start < end) {
450        int mid = start + (end - start) / 2;
451        int sid = withNameList.get(mid).lastSnapshotId; 
452        if (sid == snapshotId) {
453          return withNameList.get(mid);
454        } else if (sid < snapshotId) {
455          start = mid + 1;
456        } else {
457          end = mid;
458        }
459      }
460      if (start < withNameList.size() &&
461          withNameList.get(start).lastSnapshotId >= snapshotId) {
462        return withNameList.get(start);
463      } else {
464        return this.getParentReference();
465      }
466    }
467  }
468  
469  /** A reference with a fixed name. */
470  public static class WithName extends INodeReference {
471
472    private final byte[] name;
473
474    /**
475     * The id of the last snapshot in the src tree when this WithName node was 
476     * generated. When calculating the quota usage of the referred node, only 
477     * the files/dirs existing when this snapshot was taken will be counted for 
478     * this WithName node and propagated along its ancestor path.
479     */
480    private final int lastSnapshotId;
481    
482    public WithName(INodeDirectory parent, WithCount referred, byte[] name,
483        int lastSnapshotId) {
484      super(parent, referred);
485      this.name = name;
486      this.lastSnapshotId = lastSnapshotId;
487      referred.addReference(this);
488    }
489
490    @Override
491    public final byte[] getLocalNameBytes() {
492      return name;
493    }
494
495    @Override
496    public final void setLocalName(byte[] name) {
497      throw new UnsupportedOperationException("Cannot set name: " + getClass()
498          + " is immutable.");
499    }
500    
501    public int getLastSnapshotId() {
502      return lastSnapshotId;
503    }
504    
505    @Override
506    public final ContentSummaryComputationContext computeContentSummary(
507        ContentSummaryComputationContext summary) {
508      //only count diskspace for WithName
509      final Quota.Counts q = Quota.Counts.newInstance();
510      computeQuotaUsage(q, false, lastSnapshotId);
511      summary.getCounts().add(Content.DISKSPACE, q.get(Quota.DISKSPACE));
512      return summary;
513    }
514
515    @Override
516    public final Quota.Counts computeQuotaUsage(Quota.Counts counts,
517        boolean useCache, int lastSnapshotId) {
518      // if this.lastSnapshotId < lastSnapshotId, the rename of the referred 
519      // node happened before the rename of its ancestor. This should be 
520      // impossible since for WithName node we only count its children at the 
521      // time of the rename. 
522      Preconditions.checkState(lastSnapshotId == Snapshot.CURRENT_STATE_ID
523          || this.lastSnapshotId >= lastSnapshotId);
524      final INode referred = this.getReferredINode().asReference()
525          .getReferredINode();
526      // We will continue the quota usage computation using the same snapshot id
527      // as time line (if the given snapshot id is valid). Also, we cannot use 
528      // cache for the referred node since its cached quota may have already 
529      // been updated by changes in the current tree.
530      int id = lastSnapshotId != Snapshot.CURRENT_STATE_ID ? 
531          lastSnapshotId : this.lastSnapshotId;
532      return referred.computeQuotaUsage(counts, false, id);
533    }
534    
535    @Override
536    public Quota.Counts cleanSubtree(final int snapshot, int prior,
537        final BlocksMapUpdateInfo collectedBlocks,
538        final List<INode> removedINodes) {
539      // since WithName node resides in deleted list acting as a snapshot copy,
540      // the parameter snapshot must be non-null
541      Preconditions.checkArgument(snapshot != Snapshot.CURRENT_STATE_ID);
542      // if prior is NO_SNAPSHOT_ID, we need to check snapshot belonging to the
543      // previous WithName instance
544      if (prior == Snapshot.NO_SNAPSHOT_ID) {
545        prior = getPriorSnapshot(this);
546      }
547      
548      if (prior != Snapshot.NO_SNAPSHOT_ID
549          && Snapshot.ID_INTEGER_COMPARATOR.compare(snapshot, prior) <= 0) {
550        return Quota.Counts.newInstance();
551      }
552
553      Quota.Counts counts = getReferredINode().cleanSubtree(snapshot, prior,
554          collectedBlocks, removedINodes);
555      INodeReference ref = getReferredINode().getParentReference();
556      if (ref != null) {
557        try {
558          ref.addSpaceConsumed(-counts.get(Quota.NAMESPACE),
559              -counts.get(Quota.DISKSPACE), true);
560        } catch (QuotaExceededException e) {
561          Log.warn("Should not have QuotaExceededException");
562        }
563      }
564      
565      if (snapshot < lastSnapshotId) {
566        // for a WithName node, when we compute its quota usage, we only count
567        // in all the nodes existing at the time of the corresponding rename op.
568        // Thus if we are deleting a snapshot before/at the snapshot associated 
569        // with lastSnapshotId, we do not need to update the quota upwards.
570        counts = Quota.Counts.newInstance();
571      }
572      return counts;
573    }
574    
575    @Override
576    public void destroyAndCollectBlocks(BlocksMapUpdateInfo collectedBlocks,
577        final List<INode> removedINodes) {
578      int snapshot = getSelfSnapshot();
579      if (removeReference(this) <= 0) {
580        getReferredINode().destroyAndCollectBlocks(collectedBlocks,
581            removedINodes);
582      } else {
583        int prior = getPriorSnapshot(this);
584        INode referred = getReferredINode().asReference().getReferredINode();
585        
586        if (snapshot != Snapshot.NO_SNAPSHOT_ID) {
587          if (prior != Snapshot.NO_SNAPSHOT_ID && snapshot <= prior) {
588            // the snapshot to be deleted has been deleted while traversing 
589            // the src tree of the previous rename operation. This usually 
590            // happens when rename's src and dst are under the same 
591            // snapshottable directory. E.g., the following operation sequence:
592            // 1. create snapshot s1 on /test
593            // 2. rename /test/foo/bar to /test/foo2/bar
594            // 3. create snapshot s2 on /test
595            // 4. rename foo2 again
596            // 5. delete snapshot s2
597            return;
598          }
599          try {
600            Quota.Counts counts = referred.cleanSubtree(snapshot, prior,
601                collectedBlocks, removedINodes);
602            INodeReference ref = getReferredINode().getParentReference();
603            if (ref != null) {
604              ref.addSpaceConsumed(-counts.get(Quota.NAMESPACE),
605                  -counts.get(Quota.DISKSPACE), true);
606            }
607          } catch (QuotaExceededException e) {
608            LOG.error("should not exceed quota while snapshot deletion", e);
609          }
610        }
611      }
612    }
613    
614    private int getSelfSnapshot() {
615      INode referred = getReferredINode().asReference().getReferredINode();
616      int snapshot = Snapshot.NO_SNAPSHOT_ID;
617      if (referred.isFile() && referred.asFile().isWithSnapshot()) {
618        snapshot = referred.asFile().getDiffs().getPrior(lastSnapshotId);
619      } else if (referred.isDirectory()) {
620        DirectoryWithSnapshotFeature sf = referred.asDirectory()
621            .getDirectoryWithSnapshotFeature();
622        if (sf != null) {
623          snapshot = sf.getDiffs().getPrior(lastSnapshotId);
624        }
625      }
626      return snapshot;
627    }
628  }
629  
630  public static class DstReference extends INodeReference {
631    /**
632     * Record the latest snapshot of the dst subtree before the rename. For
633     * later operations on the moved/renamed files/directories, if the latest
634     * snapshot is after this dstSnapshot, changes will be recorded to the
635     * latest snapshot. Otherwise changes will be recorded to the snapshot
636     * belonging to the src of the rename.
637     * 
638     * {@link Snapshot#NO_SNAPSHOT_ID} means no dstSnapshot (e.g., src of the
639     * first-time rename).
640     */
641    private final int dstSnapshotId;
642    
643    @Override
644    public final int getDstSnapshotId() {
645      return dstSnapshotId;
646    }
647    
648    public DstReference(INodeDirectory parent, WithCount referred,
649        final int dstSnapshotId) {
650      super(parent, referred);
651      this.dstSnapshotId = dstSnapshotId;
652      referred.addReference(this);
653    }
654    
655    @Override
656    public Quota.Counts cleanSubtree(int snapshot, int prior,
657        BlocksMapUpdateInfo collectedBlocks, List<INode> removedINodes) {
658      if (snapshot == Snapshot.CURRENT_STATE_ID
659          && prior == Snapshot.NO_SNAPSHOT_ID) {
660        Quota.Counts counts = Quota.Counts.newInstance();
661        this.computeQuotaUsage(counts, true);
662        destroyAndCollectBlocks(collectedBlocks, removedINodes);
663        return counts;
664      } else {
665        // if prior is NO_SNAPSHOT_ID, we need to check snapshot belonging to 
666        // the previous WithName instance
667        if (prior == Snapshot.NO_SNAPSHOT_ID) {
668          prior = getPriorSnapshot(this);
669        }
670        // if prior is not NO_SNAPSHOT_ID, and prior is not before the
671        // to-be-deleted snapshot, we can quit here and leave the snapshot
672        // deletion work to the src tree of rename
673        if (snapshot != Snapshot.CURRENT_STATE_ID
674            && prior != Snapshot.NO_SNAPSHOT_ID
675            && Snapshot.ID_INTEGER_COMPARATOR.compare(snapshot, prior) <= 0) {
676          return Quota.Counts.newInstance();
677        }
678        return getReferredINode().cleanSubtree(snapshot, prior,
679            collectedBlocks, removedINodes);
680      }
681    }
682    
683    /**
684     * {@inheritDoc}
685     * <br/>
686     * To destroy a DstReference node, we first remove its link with the 
687     * referred node. If the reference number of the referred node is <= 0, we 
688     * destroy the subtree of the referred node. Otherwise, we clean the 
689     * referred node's subtree and delete everything created after the last 
690     * rename operation, i.e., everything outside of the scope of the prior 
691     * WithName nodes.
692     */
693    @Override
694    public void destroyAndCollectBlocks(
695        BlocksMapUpdateInfo collectedBlocks, final List<INode> removedINodes) {
696      if (removeReference(this) <= 0) {
697        getReferredINode().destroyAndCollectBlocks(collectedBlocks,
698            removedINodes);
699      } else {
700        // we will clean everything, including files, directories, and 
701        // snapshots, that were created after this prior snapshot
702        int prior = getPriorSnapshot(this);
703        // prior must be non-null, otherwise we do not have any previous 
704        // WithName nodes, and the reference number will be 0.
705        Preconditions.checkState(prior != Snapshot.NO_SNAPSHOT_ID);
706        // identify the snapshot created after prior
707        int snapshot = getSelfSnapshot(prior);
708        
709        INode referred = getReferredINode().asReference().getReferredINode();
710        if (referred.isFile()) {
711          // if referred is a file, it must be a file with snapshot since we did
712          // recordModification before the rename
713          INodeFile file = referred.asFile();
714          Preconditions.checkState(file.isWithSnapshot());
715          // make sure we mark the file as deleted
716          file.getFileWithSnapshotFeature().deleteCurrentFile();
717          // when calling cleanSubtree of the referred node, since we
718          // compute quota usage updates before calling this destroy
719          // function, we use true for countDiffChange
720          referred.cleanSubtree(snapshot, prior, collectedBlocks,
721              removedINodes);
722        } else if (referred.isDirectory()) {
723          // similarly, if referred is a directory, it must be an
724          // INodeDirectory with snapshot
725          INodeDirectory dir = referred.asDirectory();
726          Preconditions.checkState(dir.isWithSnapshot());
727          try {
728            DirectoryWithSnapshotFeature.destroyDstSubtree(dir, snapshot,
729                prior, collectedBlocks, removedINodes);
730          } catch (QuotaExceededException e) {
731            LOG.error("should not exceed quota while snapshot deletion", e);
732          }
733        }
734      }
735    }
736    
737    private int getSelfSnapshot(final int prior) {
738      WithCount wc = (WithCount) getReferredINode().asReference();
739      INode referred = wc.getReferredINode();
740      int lastSnapshot = Snapshot.CURRENT_STATE_ID;
741      if (referred.isFile() && referred.asFile().isWithSnapshot()) {
742        lastSnapshot = referred.asFile().getDiffs().getLastSnapshotId();
743      } else if (referred.isDirectory()) {
744        DirectoryWithSnapshotFeature sf = referred.asDirectory()
745            .getDirectoryWithSnapshotFeature();
746        if (sf != null) {
747          lastSnapshot = sf.getLastSnapshotId();
748        }
749      }
750      if (lastSnapshot != Snapshot.CURRENT_STATE_ID && lastSnapshot != prior) {
751        return lastSnapshot;
752      } else {
753        return Snapshot.CURRENT_STATE_ID;
754      }
755    }
756  }
757}