@@ -40,14 +40,6 @@ int ceph_init_dentry(struct dentry *dentry)
4040 if (dentry -> d_fsdata )
4141 return 0 ;
4242
43- if (dentry -> d_parent == NULL || /* nfs fh_to_dentry */
44- ceph_snap (dentry -> d_parent -> d_inode ) == CEPH_NOSNAP )
45- d_set_d_op (dentry , & ceph_dentry_ops );
46- else if (ceph_snap (dentry -> d_parent -> d_inode ) == CEPH_SNAPDIR )
47- d_set_d_op (dentry , & ceph_snapdir_dentry_ops );
48- else
49- d_set_d_op (dentry , & ceph_snap_dentry_ops );
50-
5143 di = kmem_cache_alloc (ceph_dentry_cachep , GFP_NOFS | __GFP_ZERO );
5244 if (!di )
5345 return - ENOMEM ; /* oh well */
@@ -58,16 +50,42 @@ int ceph_init_dentry(struct dentry *dentry)
5850 kmem_cache_free (ceph_dentry_cachep , di );
5951 goto out_unlock ;
6052 }
53+
54+ if (dentry -> d_parent == NULL || /* nfs fh_to_dentry */
55+ ceph_snap (dentry -> d_parent -> d_inode ) == CEPH_NOSNAP )
56+ d_set_d_op (dentry , & ceph_dentry_ops );
57+ else if (ceph_snap (dentry -> d_parent -> d_inode ) == CEPH_SNAPDIR )
58+ d_set_d_op (dentry , & ceph_snapdir_dentry_ops );
59+ else
60+ d_set_d_op (dentry , & ceph_snap_dentry_ops );
61+
6162 di -> dentry = dentry ;
6263 di -> lease_session = NULL ;
63- dentry -> d_fsdata = di ;
6464 dentry -> d_time = jiffies ;
65+ /* avoid reordering d_fsdata setup so that the check above is safe */
66+ smp_mb ();
67+ dentry -> d_fsdata = di ;
6568 ceph_dentry_lru_add (dentry );
6669out_unlock :
6770 spin_unlock (& dentry -> d_lock );
6871 return 0 ;
6972}
7073
74+ struct inode * ceph_get_dentry_parent_inode (struct dentry * dentry )
75+ {
76+ struct inode * inode = NULL ;
77+
78+ if (!dentry )
79+ return NULL ;
80+
81+ spin_lock (& dentry -> d_lock );
82+ if (dentry -> d_parent ) {
83+ inode = dentry -> d_parent -> d_inode ;
84+ ihold (inode );
85+ }
86+ spin_unlock (& dentry -> d_lock );
87+ return inode ;
88+ }
7189
7290
7391/*
@@ -133,7 +151,7 @@ static int __dcache_readdir(struct file *filp,
133151 d_unhashed (dentry ) ? "!hashed" : "hashed" ,
134152 parent -> d_subdirs .prev , parent -> d_subdirs .next );
135153 if (p == & parent -> d_subdirs ) {
136- fi -> at_end = 1 ;
154+ fi -> flags |= CEPH_F_ATEND ;
137155 goto out_unlock ;
138156 }
139157 spin_lock_nested (& dentry -> d_lock , DENTRY_D_LOCK_NESTED );
@@ -234,7 +252,7 @@ static int ceph_readdir(struct file *filp, void *dirent, filldir_t filldir)
234252 const int max_bytes = fsc -> mount_options -> max_readdir_bytes ;
235253
236254 dout ("readdir %p filp %p frag %u off %u\n" , inode , filp , frag , off );
237- if (fi -> at_end )
255+ if (fi -> flags & CEPH_F_ATEND )
238256 return 0 ;
239257
240258 /* always start with . and .. */
@@ -403,7 +421,7 @@ static int ceph_readdir(struct file *filp, void *dirent, filldir_t filldir)
403421 dout ("readdir next frag is %x\n" , frag );
404422 goto more ;
405423 }
406- fi -> at_end = 1 ;
424+ fi -> flags |= CEPH_F_ATEND ;
407425
408426 /*
409427 * if dir_release_count still matches the dir, no dentries
@@ -435,7 +453,7 @@ static void reset_readdir(struct ceph_file_info *fi)
435453 dput (fi -> dentry );
436454 fi -> dentry = NULL ;
437455 }
438- fi -> at_end = 0 ;
456+ fi -> flags &= ~ CEPH_F_ATEND ;
439457}
440458
441459static loff_t ceph_dir_llseek (struct file * file , loff_t offset , int origin )
@@ -463,7 +481,7 @@ static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int origin)
463481 if (offset != file -> f_pos ) {
464482 file -> f_pos = offset ;
465483 file -> f_version = 0 ;
466- fi -> at_end = 0 ;
484+ fi -> flags &= ~ CEPH_F_ATEND ;
467485 }
468486 retval = offset ;
469487
@@ -488,21 +506,13 @@ static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int origin)
488506}
489507
490508/*
491- * Process result of a lookup/open request.
492- *
493- * Mainly, make sure we return the final req->r_dentry (if it already
494- * existed) in place of the original VFS-provided dentry when they
495- * differ.
496- *
497- * Gracefully handle the case where the MDS replies with -ENOENT and
498- * no trace (which it may do, at its discretion, e.g., if it doesn't
499- * care to issue a lease on the negative dentry).
509+ * Handle lookups for the hidden .snap directory.
500510 */
501- struct dentry * ceph_finish_lookup (struct ceph_mds_request * req ,
502- struct dentry * dentry , int err )
511+ int ceph_handle_snapdir (struct ceph_mds_request * req ,
512+ struct dentry * dentry , int err )
503513{
504514 struct ceph_fs_client * fsc = ceph_sb_to_client (dentry -> d_sb );
505- struct inode * parent = dentry -> d_parent -> d_inode ;
515+ struct inode * parent = dentry -> d_parent -> d_inode ; /* we hold i_mutex */
506516
507517 /* .snap dir? */
508518 if (err == - ENOENT &&
@@ -516,7 +526,23 @@ struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
516526 d_add (dentry , inode );
517527 err = 0 ;
518528 }
529+ return err ;
530+ }
519531
532+ /*
533+ * Figure out final result of a lookup/open request.
534+ *
535+ * Mainly, make sure we return the final req->r_dentry (if it already
536+ * existed) in place of the original VFS-provided dentry when they
537+ * differ.
538+ *
539+ * Gracefully handle the case where the MDS replies with -ENOENT and
540+ * no trace (which it may do, at its discretion, e.g., if it doesn't
541+ * care to issue a lease on the negative dentry).
542+ */
543+ struct dentry * ceph_finish_lookup (struct ceph_mds_request * req ,
544+ struct dentry * dentry , int err )
545+ {
520546 if (err == - ENOENT ) {
521547 /* no trace? */
522548 err = 0 ;
@@ -610,6 +636,7 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
610636 req -> r_args .getattr .mask = cpu_to_le32 (CEPH_STAT_CAP_INODE );
611637 req -> r_locked_dir = dir ;
612638 err = ceph_mdsc_do_request (mdsc , NULL , req );
639+ err = ceph_handle_snapdir (req , dentry , err );
613640 dentry = ceph_finish_lookup (req , dentry , err );
614641 ceph_mdsc_put_request (req ); /* will dput(dentry) */
615642 dout ("lookup result=%p\n" , dentry );
@@ -789,6 +816,7 @@ static int ceph_link(struct dentry *old_dentry, struct inode *dir,
789816 req -> r_dentry = dget (dentry );
790817 req -> r_num_caps = 2 ;
791818 req -> r_old_dentry = dget (old_dentry ); /* or inode? hrm. */
819+ req -> r_old_dentry_dir = ceph_get_dentry_parent_inode (old_dentry );
792820 req -> r_locked_dir = dir ;
793821 req -> r_dentry_drop = CEPH_CAP_FILE_SHARED ;
794822 req -> r_dentry_unless = CEPH_CAP_FILE_EXCL ;
@@ -887,6 +915,7 @@ static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
887915 req -> r_dentry = dget (new_dentry );
888916 req -> r_num_caps = 2 ;
889917 req -> r_old_dentry = dget (old_dentry );
918+ req -> r_old_dentry_dir = ceph_get_dentry_parent_inode (old_dentry );
890919 req -> r_locked_dir = new_dir ;
891920 req -> r_old_dentry_drop = CEPH_CAP_FILE_SHARED ;
892921 req -> r_old_dentry_unless = CEPH_CAP_FILE_EXCL ;
@@ -1002,36 +1031,38 @@ static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
10021031 */
10031032static int ceph_d_revalidate (struct dentry * dentry , struct nameidata * nd )
10041033{
1034+ int valid = 0 ;
10051035 struct inode * dir ;
10061036
10071037 if (nd && nd -> flags & LOOKUP_RCU )
10081038 return - ECHILD ;
10091039
1010- dir = dentry -> d_parent -> d_inode ;
1011-
10121040 dout ("d_revalidate %p '%.*s' inode %p offset %lld\n" , dentry ,
10131041 dentry -> d_name .len , dentry -> d_name .name , dentry -> d_inode ,
10141042 ceph_dentry (dentry )-> offset );
10151043
1044+ dir = ceph_get_dentry_parent_inode (dentry );
1045+
10161046 /* always trust cached snapped dentries, snapdir dentry */
10171047 if (ceph_snap (dir ) != CEPH_NOSNAP ) {
10181048 dout ("d_revalidate %p '%.*s' inode %p is SNAPPED\n" , dentry ,
10191049 dentry -> d_name .len , dentry -> d_name .name , dentry -> d_inode );
1020- goto out_touch ;
1050+ valid = 1 ;
1051+ } else if (dentry -> d_inode &&
1052+ ceph_snap (dentry -> d_inode ) == CEPH_SNAPDIR ) {
1053+ valid = 1 ;
1054+ } else if (dentry_lease_is_valid (dentry ) ||
1055+ dir_lease_is_valid (dir , dentry )) {
1056+ valid = 1 ;
10211057 }
1022- if (dentry -> d_inode && ceph_snap (dentry -> d_inode ) == CEPH_SNAPDIR )
1023- goto out_touch ;
1024-
1025- if (dentry_lease_is_valid (dentry ) ||
1026- dir_lease_is_valid (dir , dentry ))
1027- goto out_touch ;
10281058
1029- dout ("d_revalidate %p invalid\n" , dentry );
1030- d_drop (dentry );
1031- return 0 ;
1032- out_touch :
1033- ceph_dentry_lru_touch (dentry );
1034- return 1 ;
1059+ dout ("d_revalidate %p %s\n" , dentry , valid ? "valid" : "invalid" );
1060+ if (valid )
1061+ ceph_dentry_lru_touch (dentry );
1062+ else
1063+ d_drop (dentry );
1064+ iput (dir );
1065+ return valid ;
10351066}
10361067
10371068/*
@@ -1228,9 +1259,8 @@ void ceph_dentry_lru_del(struct dentry *dn)
12281259 * Return name hash for a given dentry. This is dependent on
12291260 * the parent directory's hash function.
12301261 */
1231- unsigned ceph_dentry_hash (struct dentry * dn )
1262+ unsigned ceph_dentry_hash (struct inode * dir , struct dentry * dn )
12321263{
1233- struct inode * dir = dn -> d_parent -> d_inode ;
12341264 struct ceph_inode_info * dci = ceph_inode (dir );
12351265
12361266 switch (dci -> i_dir_layout .dl_dir_hash ) {
0 commit comments