Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 2 | |
| 3 | #include <linux/spinlock.h> |
| 4 | #include <linux/fs_struct.h> |
| 5 | #include <linux/namei.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 7 | #include <linux/sched.h> |
| 8 | |
| 9 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 10 | #include "mds_client.h" |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * Directory operations: readdir, lookup, create, link, unlink, |
| 14 | * rename, etc. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Ceph MDS operations are specified in terms of a base ino and |
| 19 | * relative path. Thus, the client can specify an operation on a |
| 20 | * specific inode (e.g., a getattr due to fstat(2)), or as a path |
| 21 | * relative to, say, the root directory. |
| 22 | * |
| 23 | * Normally, we limit ourselves to strict inode ops (no path component) |
| 24 | * or dentry operations (a single path component relative to an ino). The |
| 25 | * exception to this is open_root_dentry(), which will open the mount |
| 26 | * point by name. |
| 27 | */ |
| 28 | |
| 29 | const struct inode_operations ceph_dir_iops; |
| 30 | const struct file_operations ceph_dir_fops; |
Sage Weil | 52dfb8a | 2010-08-03 10:25:30 -0700 | [diff] [blame] | 31 | const struct dentry_operations ceph_dentry_ops; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Initialize ceph dentry state. |
| 35 | */ |
| 36 | int ceph_init_dentry(struct dentry *dentry) |
| 37 | { |
| 38 | struct ceph_dentry_info *di; |
| 39 | |
| 40 | if (dentry->d_fsdata) |
| 41 | return 0; |
| 42 | |
Sage Weil | 36e2168 | 2010-08-24 16:23:48 -0700 | [diff] [blame] | 43 | di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS | __GFP_ZERO); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 44 | if (!di) |
| 45 | return -ENOMEM; /* oh well */ |
| 46 | |
| 47 | spin_lock(&dentry->d_lock); |
Sage Weil | 8c6efb5 | 2010-04-23 11:36:54 -0700 | [diff] [blame] | 48 | if (dentry->d_fsdata) { |
| 49 | /* lost a race */ |
| 50 | kmem_cache_free(ceph_dentry_cachep, di); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 51 | goto out_unlock; |
Sage Weil | 8c6efb5 | 2010-04-23 11:36:54 -0700 | [diff] [blame] | 52 | } |
Sage Weil | 48d0cbd | 2011-07-26 11:30:15 -0700 | [diff] [blame] | 53 | |
Sage Weil | 8842b3b | 2012-06-07 13:43:35 -0700 | [diff] [blame] | 54 | if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) |
Sage Weil | 48d0cbd | 2011-07-26 11:30:15 -0700 | [diff] [blame] | 55 | d_set_d_op(dentry, &ceph_dentry_ops); |
| 56 | else if (ceph_snap(dentry->d_parent->d_inode) == CEPH_SNAPDIR) |
| 57 | d_set_d_op(dentry, &ceph_snapdir_dentry_ops); |
| 58 | else |
| 59 | d_set_d_op(dentry, &ceph_snap_dentry_ops); |
| 60 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 61 | di->dentry = dentry; |
| 62 | di->lease_session = NULL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 63 | dentry->d_time = jiffies; |
Sage Weil | 48d0cbd | 2011-07-26 11:30:15 -0700 | [diff] [blame] | 64 | /* avoid reordering d_fsdata setup so that the check above is safe */ |
| 65 | smp_mb(); |
| 66 | dentry->d_fsdata = di; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 67 | ceph_dentry_lru_add(dentry); |
| 68 | out_unlock: |
| 69 | spin_unlock(&dentry->d_lock); |
| 70 | return 0; |
| 71 | } |
| 72 | |
Sage Weil | 5f21c96 | 2011-07-26 11:30:29 -0700 | [diff] [blame] | 73 | struct inode *ceph_get_dentry_parent_inode(struct dentry *dentry) |
| 74 | { |
| 75 | struct inode *inode = NULL; |
| 76 | |
| 77 | if (!dentry) |
| 78 | return NULL; |
| 79 | |
| 80 | spin_lock(&dentry->d_lock); |
Sage Weil | 8842b3b | 2012-06-07 13:43:35 -0700 | [diff] [blame] | 81 | if (!IS_ROOT(dentry)) { |
Sage Weil | 5f21c96 | 2011-07-26 11:30:29 -0700 | [diff] [blame] | 82 | inode = dentry->d_parent->d_inode; |
| 83 | ihold(inode); |
| 84 | } |
| 85 | spin_unlock(&dentry->d_lock); |
| 86 | return inode; |
| 87 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /* |
| 91 | * for readdir, we encode the directory frag and offset within that |
| 92 | * frag into f_pos. |
| 93 | */ |
| 94 | static unsigned fpos_frag(loff_t p) |
| 95 | { |
| 96 | return p >> 32; |
| 97 | } |
| 98 | static unsigned fpos_off(loff_t p) |
| 99 | { |
| 100 | return p & 0xffffffff; |
| 101 | } |
| 102 | |
Yan, Zheng | 4d5f5df | 2014-02-13 19:40:26 +0800 | [diff] [blame] | 103 | static int fpos_cmp(loff_t l, loff_t r) |
| 104 | { |
| 105 | int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r)); |
| 106 | if (v) |
| 107 | return v; |
| 108 | return (int)(fpos_off(l) - fpos_off(r)); |
| 109 | } |
| 110 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 111 | /* |
| 112 | * When possible, we try to satisfy a readdir by peeking at the |
| 113 | * dcache. We make this work by carefully ordering dentries on |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 114 | * d_child when we initially get results back from the MDS, and |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 115 | * falling back to a "normal" sync readdir if any dentries in the dir |
| 116 | * are dropped. |
| 117 | * |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 118 | * Complete dir indicates that we have all dentries in the dir. It is |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 119 | * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by |
| 120 | * the MDS if/when the directory is modified). |
| 121 | */ |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 122 | static int __dcache_readdir(struct file *file, struct dir_context *ctx, |
| 123 | u32 shared_gen) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 124 | { |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 125 | struct ceph_file_info *fi = file->private_data; |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 126 | struct dentry *parent = file->f_path.dentry; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 127 | struct inode *dir = parent->d_inode; |
| 128 | struct list_head *p; |
| 129 | struct dentry *dentry, *last; |
| 130 | struct ceph_dentry_info *di; |
| 131 | int err = 0; |
| 132 | |
| 133 | /* claim ref on last dentry we returned */ |
| 134 | last = fi->dentry; |
| 135 | fi->dentry = NULL; |
| 136 | |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 137 | dout("__dcache_readdir %p v%u at %llu (last %p)\n", |
| 138 | dir, shared_gen, ctx->pos, last); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 139 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 140 | spin_lock(&parent->d_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 141 | |
| 142 | /* start at beginning? */ |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 143 | if (ctx->pos == 2 || last == NULL || |
Yan, Zheng | 6da5246 | 2014-04-09 09:35:19 +0800 | [diff] [blame] | 144 | fpos_cmp(ctx->pos, ceph_dentry(last)->offset) < 0) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 145 | if (list_empty(&parent->d_subdirs)) |
| 146 | goto out_unlock; |
| 147 | p = parent->d_subdirs.prev; |
| 148 | dout(" initial p %p/%p\n", p->prev, p->next); |
| 149 | } else { |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 150 | p = last->d_child.prev; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | more: |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 154 | dentry = list_entry(p, struct dentry, d_child); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 155 | di = ceph_dentry(dentry); |
| 156 | while (1) { |
Sage Weil | 1cd3935 | 2010-05-03 22:08:02 -0700 | [diff] [blame] | 157 | dout(" p %p/%p %s d_subdirs %p/%p\n", p->prev, p->next, |
| 158 | d_unhashed(dentry) ? "!hashed" : "hashed", |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 159 | parent->d_subdirs.prev, parent->d_subdirs.next); |
| 160 | if (p == &parent->d_subdirs) { |
Sage Weil | 9cfa109 | 2011-07-26 11:26:18 -0700 | [diff] [blame] | 161 | fi->flags |= CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 162 | goto out_unlock; |
| 163 | } |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 164 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 165 | if (di->lease_shared_gen == shared_gen && |
| 166 | !d_unhashed(dentry) && dentry->d_inode && |
Sage Weil | 09b8a7d | 2009-11-11 15:21:27 -0800 | [diff] [blame] | 167 | ceph_snap(dentry->d_inode) != CEPH_SNAPDIR && |
Sage Weil | 1d1de916 | 2009-12-02 11:54:25 -0800 | [diff] [blame] | 168 | ceph_ino(dentry->d_inode) != CEPH_INO_CEPH && |
Yan, Zheng | 4d5f5df | 2014-02-13 19:40:26 +0800 | [diff] [blame] | 169 | fpos_cmp(ctx->pos, di->offset) <= 0) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 170 | break; |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 171 | dout(" skipping %p %pd at %llu (%llu)%s%s\n", dentry, |
| 172 | dentry, di->offset, |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 173 | ctx->pos, d_unhashed(dentry) ? " unhashed" : "", |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 174 | !dentry->d_inode ? " null" : ""); |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 175 | spin_unlock(&dentry->d_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 176 | p = p->prev; |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 177 | dentry = list_entry(p, struct dentry, d_child); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 178 | di = ceph_dentry(dentry); |
| 179 | } |
| 180 | |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 181 | dget_dlock(dentry); |
Nick Piggin | b7ab39f | 2011-01-07 17:49:32 +1100 | [diff] [blame] | 182 | spin_unlock(&dentry->d_lock); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 183 | spin_unlock(&parent->d_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 184 | |
Yan, Zheng | 0081bd8 | 2014-04-08 21:42:59 +0800 | [diff] [blame] | 185 | /* make sure a dentry wasn't dropped while we didn't have parent lock */ |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 186 | if (!ceph_dir_is_complete_ordered(dir)) { |
Yan, Zheng | 0081bd8 | 2014-04-08 21:42:59 +0800 | [diff] [blame] | 187 | dout(" lost dir complete on %p; falling back to mds\n", dir); |
| 188 | dput(dentry); |
| 189 | err = -EAGAIN; |
| 190 | goto out; |
| 191 | } |
| 192 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 193 | dout(" %llu (%llu) dentry %p %pd %p\n", di->offset, ctx->pos, |
| 194 | dentry, dentry, dentry->d_inode); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 195 | if (!dir_emit(ctx, dentry->d_name.name, |
| 196 | dentry->d_name.len, |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 197 | ceph_translate_ino(dentry->d_sb, dentry->d_inode->i_ino), |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 198 | dentry->d_inode->i_mode >> 12)) { |
| 199 | if (last) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 200 | /* remember our position */ |
| 201 | fi->dentry = last; |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 202 | fi->next_offset = fpos_off(di->offset); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 203 | } |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 204 | dput(dentry); |
| 205 | return 0; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 206 | } |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 207 | |
Yan, Zheng | 0081bd8 | 2014-04-08 21:42:59 +0800 | [diff] [blame] | 208 | ctx->pos = di->offset + 1; |
| 209 | |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 210 | if (last) |
| 211 | dput(last); |
Sage Weil | f5b0662 | 2010-04-12 14:24:28 -0700 | [diff] [blame] | 212 | last = dentry; |
| 213 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 214 | spin_lock(&parent->d_lock); |
Sage Weil | efa4c12 | 2010-10-18 14:04:31 -0700 | [diff] [blame] | 215 | p = p->prev; /* advance to next dentry */ |
| 216 | goto more; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 217 | |
| 218 | out_unlock: |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 219 | spin_unlock(&parent->d_lock); |
Sage Weil | efa4c12 | 2010-10-18 14:04:31 -0700 | [diff] [blame] | 220 | out: |
| 221 | if (last) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 222 | dput(last); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 223 | return err; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * make note of the last dentry we read, so we can |
| 228 | * continue at the same lexicographical point, |
| 229 | * regardless of what dir changes take place on the |
| 230 | * server. |
| 231 | */ |
| 232 | static int note_last_dentry(struct ceph_file_info *fi, const char *name, |
| 233 | int len) |
| 234 | { |
| 235 | kfree(fi->last_name); |
| 236 | fi->last_name = kmalloc(len+1, GFP_NOFS); |
| 237 | if (!fi->last_name) |
| 238 | return -ENOMEM; |
| 239 | memcpy(fi->last_name, name, len); |
| 240 | fi->last_name[len] = 0; |
| 241 | dout("note_last_dentry '%s'\n", fi->last_name); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 245 | static int ceph_readdir(struct file *file, struct dir_context *ctx) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 246 | { |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 247 | struct ceph_file_info *fi = file->private_data; |
| 248 | struct inode *inode = file_inode(file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 249 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 250 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 251 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 252 | unsigned frag = fpos_frag(ctx->pos); |
| 253 | int off = fpos_off(ctx->pos); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 254 | int err; |
| 255 | u32 ftype; |
| 256 | struct ceph_mds_reply_info_parsed *rinfo; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 257 | |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 258 | dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off); |
Sage Weil | 9cfa109 | 2011-07-26 11:26:18 -0700 | [diff] [blame] | 259 | if (fi->flags & CEPH_F_ATEND) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | |
| 262 | /* always start with . and .. */ |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 263 | if (ctx->pos == 0) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 264 | dout("readdir off 0 -> '.'\n"); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 265 | if (!dir_emit(ctx, ".", 1, |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 266 | ceph_translate_ino(inode->i_sb, inode->i_ino), |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 267 | inode->i_mode >> 12)) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 268 | return 0; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 269 | ctx->pos = 1; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 270 | off = 1; |
| 271 | } |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 272 | if (ctx->pos == 1) { |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 273 | ino_t ino = parent_ino(file->f_path.dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 274 | dout("readdir off 1 -> '..'\n"); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 275 | if (!dir_emit(ctx, "..", 2, |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 276 | ceph_translate_ino(inode->i_sb, ino), |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 277 | inode->i_mode >> 12)) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 278 | return 0; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 279 | ctx->pos = 2; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 280 | off = 2; |
| 281 | } |
| 282 | |
| 283 | /* can we use the dcache? */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 284 | spin_lock(&ci->i_ceph_lock); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 285 | if ((ctx->pos == 2 || fi->dentry) && |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 286 | !ceph_test_mount_opt(fsc, NOASYNCREADDIR) && |
Sage Weil | a0dff78 | 2010-07-22 13:47:21 -0700 | [diff] [blame] | 287 | ceph_snap(inode) != CEPH_SNAPDIR && |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 288 | __ceph_dir_is_complete_ordered(ci) && |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 289 | __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) { |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 290 | u32 shared_gen = ci->i_shared_gen; |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 291 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 292 | err = __dcache_readdir(file, ctx, shared_gen); |
Sage Weil | efa4c12 | 2010-10-18 14:04:31 -0700 | [diff] [blame] | 293 | if (err != -EAGAIN) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 294 | return err; |
Yan, Zheng | 0081bd8 | 2014-04-08 21:42:59 +0800 | [diff] [blame] | 295 | frag = fpos_frag(ctx->pos); |
| 296 | off = fpos_off(ctx->pos); |
Sage Weil | efa4c12 | 2010-10-18 14:04:31 -0700 | [diff] [blame] | 297 | } else { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 298 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 299 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 300 | if (fi->dentry) { |
| 301 | err = note_last_dentry(fi, fi->dentry->d_name.name, |
| 302 | fi->dentry->d_name.len); |
| 303 | if (err) |
| 304 | return err; |
| 305 | dput(fi->dentry); |
| 306 | fi->dentry = NULL; |
| 307 | } |
| 308 | |
| 309 | /* proceed with a normal readdir */ |
| 310 | |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 311 | if (ctx->pos == 2) { |
| 312 | /* note dir version at start of readdir so we can tell |
| 313 | * if any dentries get dropped */ |
| 314 | fi->dir_release_count = atomic_read(&ci->i_release_count); |
| 315 | fi->dir_ordered_count = ci->i_ordered_count; |
| 316 | } |
| 317 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 318 | more: |
| 319 | /* do we have the correct frag content buffered? */ |
| 320 | if (fi->frag != frag || fi->last_readdir == NULL) { |
| 321 | struct ceph_mds_request *req; |
| 322 | int op = ceph_snap(inode) == CEPH_SNAPDIR ? |
| 323 | CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR; |
| 324 | |
| 325 | /* discard old result, if any */ |
Sage Weil | 393f662 | 2010-03-10 12:03:32 -0800 | [diff] [blame] | 326 | if (fi->last_readdir) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 327 | ceph_mdsc_put_request(fi->last_readdir); |
Sage Weil | 393f662 | 2010-03-10 12:03:32 -0800 | [diff] [blame] | 328 | fi->last_readdir = NULL; |
| 329 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 330 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 331 | dout("readdir fetching %llx.%llx frag %x offset '%s'\n", |
| 332 | ceph_vinop(inode), frag, fi->last_name); |
| 333 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 334 | if (IS_ERR(req)) |
| 335 | return PTR_ERR(req); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 336 | err = ceph_alloc_readdir_reply_buffer(req, inode); |
| 337 | if (err) { |
| 338 | ceph_mdsc_put_request(req); |
| 339 | return err; |
| 340 | } |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 341 | req->r_inode = inode; |
| 342 | ihold(inode); |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 343 | req->r_dentry = dget(file->f_path.dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 344 | /* hints to request -> mds selection code */ |
| 345 | req->r_direct_mode = USE_AUTH_MDS; |
| 346 | req->r_direct_hash = ceph_frag_value(frag); |
| 347 | req->r_direct_is_hash = true; |
| 348 | req->r_path2 = kstrdup(fi->last_name, GFP_NOFS); |
| 349 | req->r_readdir_offset = fi->next_offset; |
| 350 | req->r_args.readdir.frag = cpu_to_le32(frag); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 351 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 352 | if (err < 0) { |
| 353 | ceph_mdsc_put_request(req); |
| 354 | return err; |
| 355 | } |
| 356 | dout("readdir got and parsed readdir result=%d" |
| 357 | " on frag %x, end=%d, complete=%d\n", err, frag, |
| 358 | (int)req->r_reply_info.dir_end, |
| 359 | (int)req->r_reply_info.dir_complete); |
| 360 | |
| 361 | if (!req->r_did_prepopulate) { |
| 362 | dout("readdir !did_prepopulate"); |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 363 | /* preclude from marking dir complete */ |
| 364 | fi->dir_release_count--; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* note next offset and last dentry name */ |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 368 | rinfo = &req->r_reply_info; |
| 369 | if (le32_to_cpu(rinfo->dir_dir->frag) != frag) { |
| 370 | frag = le32_to_cpu(rinfo->dir_dir->frag); |
| 371 | if (ceph_frag_is_leftmost(frag)) |
| 372 | fi->next_offset = 2; |
| 373 | else |
| 374 | fi->next_offset = 0; |
| 375 | off = fi->next_offset; |
| 376 | } |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 377 | fi->frag = frag; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 378 | fi->offset = fi->next_offset; |
| 379 | fi->last_readdir = req; |
| 380 | |
| 381 | if (req->r_reply_info.dir_end) { |
| 382 | kfree(fi->last_name); |
| 383 | fi->last_name = NULL; |
Sage Weil | 7b88dad | 2010-11-11 16:48:59 -0800 | [diff] [blame] | 384 | if (ceph_frag_is_rightmost(frag)) |
| 385 | fi->next_offset = 2; |
| 386 | else |
| 387 | fi->next_offset = 0; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 388 | } else { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 389 | err = note_last_dentry(fi, |
| 390 | rinfo->dir_dname[rinfo->dir_nr-1], |
| 391 | rinfo->dir_dname_len[rinfo->dir_nr-1]); |
| 392 | if (err) |
| 393 | return err; |
| 394 | fi->next_offset += rinfo->dir_nr; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | rinfo = &fi->last_readdir->r_reply_info; |
| 399 | dout("readdir frag %x num %d off %d chunkoff %d\n", frag, |
| 400 | rinfo->dir_nr, off, fi->offset); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 401 | |
| 402 | ctx->pos = ceph_make_fpos(frag, off); |
Sage Weil | da39822 | 2011-05-12 15:28:11 -0700 | [diff] [blame] | 403 | while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 404 | struct ceph_mds_reply_inode *in = |
| 405 | rinfo->dir_in[off - fi->offset].in; |
Sage Weil | 3105c19 | 2010-11-18 09:15:07 -0800 | [diff] [blame] | 406 | struct ceph_vino vino; |
| 407 | ino_t ino; |
| 408 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 409 | dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n", |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 410 | off, off - fi->offset, rinfo->dir_nr, ctx->pos, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 411 | rinfo->dir_dname_len[off - fi->offset], |
| 412 | rinfo->dir_dname[off - fi->offset], in); |
| 413 | BUG_ON(!in); |
| 414 | ftype = le32_to_cpu(in->mode) >> 12; |
Sage Weil | 3105c19 | 2010-11-18 09:15:07 -0800 | [diff] [blame] | 415 | vino.ino = le64_to_cpu(in->ino); |
| 416 | vino.snap = le64_to_cpu(in->snapid); |
| 417 | ino = ceph_vino_to_ino(vino); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 418 | if (!dir_emit(ctx, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 419 | rinfo->dir_dname[off - fi->offset], |
| 420 | rinfo->dir_dname_len[off - fi->offset], |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 421 | ceph_translate_ino(inode->i_sb, ino), ftype)) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 422 | dout("filldir stopping us...\n"); |
| 423 | return 0; |
| 424 | } |
| 425 | off++; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 426 | ctx->pos++; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (fi->last_name) { |
| 430 | ceph_mdsc_put_request(fi->last_readdir); |
| 431 | fi->last_readdir = NULL; |
| 432 | goto more; |
| 433 | } |
| 434 | |
| 435 | /* more frags? */ |
| 436 | if (!ceph_frag_is_rightmost(frag)) { |
| 437 | frag = ceph_frag_next(frag); |
| 438 | off = 0; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 439 | ctx->pos = ceph_make_fpos(frag, off); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 440 | dout("readdir next frag is %x\n", frag); |
| 441 | goto more; |
| 442 | } |
Sage Weil | 9cfa109 | 2011-07-26 11:26:18 -0700 | [diff] [blame] | 443 | fi->flags |= CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * if dir_release_count still matches the dir, no dentries |
| 447 | * were released during the whole readdir, and we should have |
| 448 | * the complete dir contents in our cache. |
| 449 | */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 450 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 451 | if (atomic_read(&ci->i_release_count) == fi->dir_release_count) { |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 452 | if (ci->i_ordered_count == fi->dir_ordered_count) |
| 453 | dout(" marking %p complete and ordered\n", inode); |
| 454 | else |
| 455 | dout(" marking %p complete\n", inode); |
| 456 | __ceph_dir_set_complete(ci, fi->dir_release_count, |
| 457 | fi->dir_ordered_count); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 458 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 459 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 460 | |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 461 | dout("readdir %p file %p done.\n", inode, file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
Yan, Zheng | dcd3cc0 | 2014-02-28 16:36:09 +0800 | [diff] [blame] | 465 | static void reset_readdir(struct ceph_file_info *fi, unsigned frag) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 466 | { |
| 467 | if (fi->last_readdir) { |
| 468 | ceph_mdsc_put_request(fi->last_readdir); |
| 469 | fi->last_readdir = NULL; |
| 470 | } |
| 471 | kfree(fi->last_name); |
Sage Weil | a1629c3 | 2010-11-11 15:24:06 -0800 | [diff] [blame] | 472 | fi->last_name = NULL; |
Yan, Zheng | dcd3cc0 | 2014-02-28 16:36:09 +0800 | [diff] [blame] | 473 | if (ceph_frag_is_leftmost(frag)) |
| 474 | fi->next_offset = 2; /* compensate for . and .. */ |
| 475 | else |
| 476 | fi->next_offset = 0; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 477 | if (fi->dentry) { |
| 478 | dput(fi->dentry); |
| 479 | fi->dentry = NULL; |
| 480 | } |
Sage Weil | 9cfa109 | 2011-07-26 11:26:18 -0700 | [diff] [blame] | 481 | fi->flags &= ~CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 484 | static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 485 | { |
| 486 | struct ceph_file_info *fi = file->private_data; |
| 487 | struct inode *inode = file->f_mapping->host; |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 488 | loff_t old_offset = ceph_make_fpos(fi->frag, fi->next_offset); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 489 | loff_t retval; |
| 490 | |
| 491 | mutex_lock(&inode->i_mutex); |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 492 | retval = -EINVAL; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 493 | switch (whence) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 494 | case SEEK_END: |
| 495 | offset += inode->i_size + 2; /* FIXME */ |
| 496 | break; |
| 497 | case SEEK_CUR: |
| 498 | offset += file->f_pos; |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 499 | case SEEK_SET: |
| 500 | break; |
| 501 | default: |
| 502 | goto out; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 503 | } |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 504 | |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 505 | if (offset >= 0) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 506 | if (offset != file->f_pos) { |
| 507 | file->f_pos = offset; |
| 508 | file->f_version = 0; |
Sage Weil | 9cfa109 | 2011-07-26 11:26:18 -0700 | [diff] [blame] | 509 | fi->flags &= ~CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 510 | } |
| 511 | retval = offset; |
| 512 | |
| 513 | /* |
| 514 | * discard buffered readdir content on seekdir(0), or |
| 515 | * seek to new frag, or seek prior to current chunk. |
| 516 | */ |
| 517 | if (offset == 0 || |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 518 | fpos_frag(offset) != fi->frag || |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 519 | fpos_off(offset) < fi->offset) { |
| 520 | dout("dir_llseek dropping %p content\n", file); |
Yan, Zheng | dcd3cc0 | 2014-02-28 16:36:09 +0800 | [diff] [blame] | 521 | reset_readdir(fi, fpos_frag(offset)); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* bump dir_release_count if we did a forward seek */ |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 525 | if (fpos_cmp(offset, old_offset) > 0) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 526 | fi->dir_release_count--; |
| 527 | } |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 528 | out: |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 529 | mutex_unlock(&inode->i_mutex); |
| 530 | return retval; |
| 531 | } |
| 532 | |
| 533 | /* |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 534 | * Handle lookups for the hidden .snap directory. |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 535 | */ |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 536 | int ceph_handle_snapdir(struct ceph_mds_request *req, |
| 537 | struct dentry *dentry, int err) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 538 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 539 | struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb); |
Sage Weil | d79698d | 2011-07-26 11:31:26 -0700 | [diff] [blame] | 540 | struct inode *parent = dentry->d_parent->d_inode; /* we hold i_mutex */ |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 541 | |
| 542 | /* .snap dir? */ |
| 543 | if (err == -ENOENT && |
Sage Weil | 455cec0 | 2011-03-03 13:44:35 -0800 | [diff] [blame] | 544 | ceph_snap(parent) == CEPH_NOSNAP && |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 545 | strcmp(dentry->d_name.name, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 546 | fsc->mount_options->snapdir_name) == 0) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 547 | struct inode *inode = ceph_get_snapdir(parent); |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 548 | dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n", |
| 549 | dentry, dentry, inode); |
Sage Weil | 9358c6d | 2010-03-30 13:54:41 -0700 | [diff] [blame] | 550 | BUG_ON(!d_unhashed(dentry)); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 551 | d_add(dentry, inode); |
| 552 | err = 0; |
| 553 | } |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 554 | return err; |
| 555 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 556 | |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 557 | /* |
| 558 | * Figure out final result of a lookup/open request. |
| 559 | * |
| 560 | * Mainly, make sure we return the final req->r_dentry (if it already |
| 561 | * existed) in place of the original VFS-provided dentry when they |
| 562 | * differ. |
| 563 | * |
| 564 | * Gracefully handle the case where the MDS replies with -ENOENT and |
| 565 | * no trace (which it may do, at its discretion, e.g., if it doesn't |
| 566 | * care to issue a lease on the negative dentry). |
| 567 | */ |
| 568 | struct dentry *ceph_finish_lookup(struct ceph_mds_request *req, |
| 569 | struct dentry *dentry, int err) |
| 570 | { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 571 | if (err == -ENOENT) { |
| 572 | /* no trace? */ |
| 573 | err = 0; |
| 574 | if (!req->r_reply_info.head->is_dentry) { |
| 575 | dout("ENOENT and no trace, dentry %p inode %p\n", |
| 576 | dentry, dentry->d_inode); |
| 577 | if (dentry->d_inode) { |
| 578 | d_drop(dentry); |
| 579 | err = -ENOENT; |
| 580 | } else { |
| 581 | d_add(dentry, NULL); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | if (err) |
| 586 | dentry = ERR_PTR(err); |
| 587 | else if (dentry != req->r_dentry) |
| 588 | dentry = dget(req->r_dentry); /* we got spliced */ |
| 589 | else |
| 590 | dentry = NULL; |
| 591 | return dentry; |
| 592 | } |
| 593 | |
Sage Weil | 1d1de916 | 2009-12-02 11:54:25 -0800 | [diff] [blame] | 594 | static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry) |
| 595 | { |
| 596 | return ceph_ino(inode) == CEPH_INO_ROOT && |
| 597 | strncmp(dentry->d_name.name, ".ceph", 5) == 0; |
| 598 | } |
| 599 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 600 | /* |
| 601 | * Look up a single dir entry. If there is a lookup intent, inform |
| 602 | * the MDS so that it gets our 'caps wanted' value in a single op. |
| 603 | */ |
| 604 | static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 605 | unsigned int flags) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 606 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 607 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 608 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 609 | struct ceph_mds_request *req; |
| 610 | int op; |
| 611 | int err; |
| 612 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 613 | dout("lookup %p dentry %p '%pd'\n", |
| 614 | dir, dentry, dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 615 | |
| 616 | if (dentry->d_name.len > NAME_MAX) |
| 617 | return ERR_PTR(-ENAMETOOLONG); |
| 618 | |
| 619 | err = ceph_init_dentry(dentry); |
| 620 | if (err < 0) |
| 621 | return ERR_PTR(err); |
| 622 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 623 | /* can we conclude ENOENT locally? */ |
| 624 | if (dentry->d_inode == NULL) { |
| 625 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 626 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 627 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 628 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 629 | dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags); |
| 630 | if (strncmp(dentry->d_name.name, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 631 | fsc->mount_options->snapdir_name, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 632 | dentry->d_name.len) && |
Sage Weil | 1d1de916 | 2009-12-02 11:54:25 -0800 | [diff] [blame] | 633 | !is_root_ceph_dentry(dir, dentry) && |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 634 | __ceph_dir_is_complete(ci) && |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 635 | (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 636 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 637 | dout(" dir %p complete, -ENOENT\n", dir); |
| 638 | d_add(dentry, NULL); |
| 639 | di->lease_shared_gen = ci->i_shared_gen; |
| 640 | return NULL; |
| 641 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 642 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | op = ceph_snap(dir) == CEPH_SNAPDIR ? |
| 646 | CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP; |
| 647 | req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS); |
| 648 | if (IS_ERR(req)) |
Julia Lawall | 7e34bc5 | 2010-05-22 12:01:14 +0200 | [diff] [blame] | 649 | return ERR_CAST(req); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 650 | req->r_dentry = dget(dentry); |
| 651 | req->r_num_caps = 2; |
| 652 | /* we only need inode linkage */ |
| 653 | req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE); |
| 654 | req->r_locked_dir = dir; |
| 655 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 656 | err = ceph_handle_snapdir(req, dentry, err); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 657 | dentry = ceph_finish_lookup(req, dentry, err); |
| 658 | ceph_mdsc_put_request(req); /* will dput(dentry) */ |
| 659 | dout("lookup result=%p\n", dentry); |
| 660 | return dentry; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * If we do a create but get no trace back from the MDS, follow up with |
| 665 | * a lookup (the VFS expects us to link up the provided dentry). |
| 666 | */ |
| 667 | int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry) |
| 668 | { |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 669 | struct dentry *result = ceph_lookup(dir, dentry, 0); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 670 | |
| 671 | if (result && !IS_ERR(result)) { |
| 672 | /* |
| 673 | * We created the item, then did a lookup, and found |
| 674 | * it was already linked to another inode we already |
| 675 | * had in our cache (and thus got spliced). Link our |
| 676 | * dentry to that inode, but don't hash it, just in |
| 677 | * case the VFS wants to dereference it. |
| 678 | */ |
| 679 | BUG_ON(!result->d_inode); |
| 680 | d_instantiate(dentry, result->d_inode); |
| 681 | return 0; |
| 682 | } |
| 683 | return PTR_ERR(result); |
| 684 | } |
| 685 | |
| 686 | static int ceph_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 687 | umode_t mode, dev_t rdev) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 688 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 689 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 690 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 691 | struct ceph_mds_request *req; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 692 | struct ceph_acls_info acls = {}; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 693 | int err; |
| 694 | |
| 695 | if (ceph_snap(dir) != CEPH_NOSNAP) |
| 696 | return -EROFS; |
| 697 | |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 698 | err = ceph_pre_init_acls(dir, &mode, &acls); |
| 699 | if (err < 0) |
| 700 | return err; |
| 701 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 702 | dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n", |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 703 | dir, dentry, mode, rdev); |
| 704 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS); |
| 705 | if (IS_ERR(req)) { |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 706 | err = PTR_ERR(req); |
| 707 | goto out; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 708 | } |
| 709 | req->r_dentry = dget(dentry); |
| 710 | req->r_num_caps = 2; |
| 711 | req->r_locked_dir = dir; |
| 712 | req->r_args.mknod.mode = cpu_to_le32(mode); |
| 713 | req->r_args.mknod.rdev = cpu_to_le32(rdev); |
| 714 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 715 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 716 | if (acls.pagelist) { |
| 717 | req->r_pagelist = acls.pagelist; |
| 718 | acls.pagelist = NULL; |
| 719 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 720 | err = ceph_mdsc_do_request(mdsc, dir, req); |
| 721 | if (!err && !req->r_reply_info.head->is_dentry) |
| 722 | err = ceph_handle_notrace_create(dir, dentry); |
| 723 | ceph_mdsc_put_request(req); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 724 | out: |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 725 | if (!err) |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 726 | ceph_init_inode_acls(dentry->d_inode, &acls); |
Yan, Zheng | b20a95a | 2014-02-11 12:55:05 +0800 | [diff] [blame] | 727 | else |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 728 | d_drop(dentry); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 729 | ceph_release_acls_info(&acls); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 730 | return err; |
| 731 | } |
| 732 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 733 | static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 734 | bool excl) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 735 | { |
Miklos Szeredi | 2d83bde | 2012-06-05 15:10:25 +0200 | [diff] [blame] | 736 | return ceph_mknod(dir, dentry, mode, 0); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static int ceph_symlink(struct inode *dir, struct dentry *dentry, |
| 740 | const char *dest) |
| 741 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 742 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 743 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 744 | struct ceph_mds_request *req; |
| 745 | int err; |
| 746 | |
| 747 | if (ceph_snap(dir) != CEPH_NOSNAP) |
| 748 | return -EROFS; |
| 749 | |
| 750 | dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest); |
| 751 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS); |
| 752 | if (IS_ERR(req)) { |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 753 | err = PTR_ERR(req); |
| 754 | goto out; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 755 | } |
| 756 | req->r_dentry = dget(dentry); |
| 757 | req->r_num_caps = 2; |
| 758 | req->r_path2 = kstrdup(dest, GFP_NOFS); |
| 759 | req->r_locked_dir = dir; |
| 760 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 761 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 762 | err = ceph_mdsc_do_request(mdsc, dir, req); |
| 763 | if (!err && !req->r_reply_info.head->is_dentry) |
| 764 | err = ceph_handle_notrace_create(dir, dentry); |
| 765 | ceph_mdsc_put_request(req); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 766 | out: |
| 767 | if (err) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 768 | d_drop(dentry); |
| 769 | return err; |
| 770 | } |
| 771 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 772 | static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 773 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 774 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 775 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 776 | struct ceph_mds_request *req; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 777 | struct ceph_acls_info acls = {}; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 778 | int err = -EROFS; |
| 779 | int op; |
| 780 | |
| 781 | if (ceph_snap(dir) == CEPH_SNAPDIR) { |
| 782 | /* mkdir .snap/foo is a MKSNAP */ |
| 783 | op = CEPH_MDS_OP_MKSNAP; |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 784 | dout("mksnap dir %p snap '%pd' dn %p\n", dir, |
| 785 | dentry, dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 786 | } else if (ceph_snap(dir) == CEPH_NOSNAP) { |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 787 | dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 788 | op = CEPH_MDS_OP_MKDIR; |
| 789 | } else { |
| 790 | goto out; |
| 791 | } |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 792 | |
| 793 | mode |= S_IFDIR; |
| 794 | err = ceph_pre_init_acls(dir, &mode, &acls); |
| 795 | if (err < 0) |
| 796 | goto out; |
| 797 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 798 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 799 | if (IS_ERR(req)) { |
| 800 | err = PTR_ERR(req); |
| 801 | goto out; |
| 802 | } |
| 803 | |
| 804 | req->r_dentry = dget(dentry); |
| 805 | req->r_num_caps = 2; |
| 806 | req->r_locked_dir = dir; |
| 807 | req->r_args.mkdir.mode = cpu_to_le32(mode); |
| 808 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 809 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 810 | if (acls.pagelist) { |
| 811 | req->r_pagelist = acls.pagelist; |
| 812 | acls.pagelist = NULL; |
| 813 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 814 | err = ceph_mdsc_do_request(mdsc, dir, req); |
Yan, Zheng | 275dd19 | 2014-12-10 16:17:31 +0800 | [diff] [blame] | 815 | if (!err && |
| 816 | !req->r_reply_info.head->is_target && |
| 817 | !req->r_reply_info.head->is_dentry) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 818 | err = ceph_handle_notrace_create(dir, dentry); |
| 819 | ceph_mdsc_put_request(req); |
| 820 | out: |
Yan, Zheng | b20a95a | 2014-02-11 12:55:05 +0800 | [diff] [blame] | 821 | if (!err) |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 822 | ceph_init_inode_acls(dentry->d_inode, &acls); |
Yan, Zheng | b20a95a | 2014-02-11 12:55:05 +0800 | [diff] [blame] | 823 | else |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 824 | d_drop(dentry); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 825 | ceph_release_acls_info(&acls); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 826 | return err; |
| 827 | } |
| 828 | |
| 829 | static int ceph_link(struct dentry *old_dentry, struct inode *dir, |
| 830 | struct dentry *dentry) |
| 831 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 832 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 833 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 834 | struct ceph_mds_request *req; |
| 835 | int err; |
| 836 | |
| 837 | if (ceph_snap(dir) != CEPH_NOSNAP) |
| 838 | return -EROFS; |
| 839 | |
| 840 | dout("link in dir %p old_dentry %p dentry %p\n", dir, |
| 841 | old_dentry, dentry); |
| 842 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS); |
| 843 | if (IS_ERR(req)) { |
| 844 | d_drop(dentry); |
| 845 | return PTR_ERR(req); |
| 846 | } |
| 847 | req->r_dentry = dget(dentry); |
| 848 | req->r_num_caps = 2; |
Sage Weil | 4b58c9b | 2013-02-05 13:41:23 -0800 | [diff] [blame] | 849 | req->r_old_dentry = dget(old_dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 850 | req->r_locked_dir = dir; |
| 851 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 852 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | ad88f23 | 2013-07-21 20:25:25 +0800 | [diff] [blame] | 853 | /* release LINK_SHARED on source inode (mds will lock it) */ |
| 854 | req->r_old_inode_drop = CEPH_CAP_LINK_SHARED; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 855 | err = ceph_mdsc_do_request(mdsc, dir, req); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 856 | if (err) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 857 | d_drop(dentry); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 858 | } else if (!req->r_reply_info.head->is_dentry) { |
| 859 | ihold(old_dentry->d_inode); |
| 860 | d_instantiate(dentry, old_dentry->d_inode); |
| 861 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 862 | ceph_mdsc_put_request(req); |
| 863 | return err; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it |
| 868 | * looks like the link count will hit 0, drop any other caps (other |
| 869 | * than PIN) we don't specifically want (due to the file still being |
| 870 | * open). |
| 871 | */ |
| 872 | static int drop_caps_for_unlink(struct inode *inode) |
| 873 | { |
| 874 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 875 | int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
| 876 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 877 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 878 | if (inode->i_nlink == 1) { |
| 879 | drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN); |
| 880 | ci->i_ceph_flags |= CEPH_I_NODELAY; |
| 881 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 882 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 883 | return drop; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * rmdir and unlink are differ only by the metadata op code |
| 888 | */ |
| 889 | static int ceph_unlink(struct inode *dir, struct dentry *dentry) |
| 890 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 891 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 892 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 893 | struct inode *inode = dentry->d_inode; |
| 894 | struct ceph_mds_request *req; |
| 895 | int err = -EROFS; |
| 896 | int op; |
| 897 | |
| 898 | if (ceph_snap(dir) == CEPH_SNAPDIR) { |
| 899 | /* rmdir .snap/foo is RMSNAP */ |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 900 | dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 901 | op = CEPH_MDS_OP_RMSNAP; |
| 902 | } else if (ceph_snap(dir) == CEPH_NOSNAP) { |
| 903 | dout("unlink/rmdir dir %p dn %p inode %p\n", |
| 904 | dir, dentry, inode); |
Al Viro | dba19c6 | 2011-07-25 20:49:29 -0400 | [diff] [blame] | 905 | op = S_ISDIR(dentry->d_inode->i_mode) ? |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 906 | CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK; |
| 907 | } else |
| 908 | goto out; |
| 909 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 910 | if (IS_ERR(req)) { |
| 911 | err = PTR_ERR(req); |
| 912 | goto out; |
| 913 | } |
| 914 | req->r_dentry = dget(dentry); |
| 915 | req->r_num_caps = 2; |
| 916 | req->r_locked_dir = dir; |
| 917 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 918 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 919 | req->r_inode_drop = drop_caps_for_unlink(inode); |
| 920 | err = ceph_mdsc_do_request(mdsc, dir, req); |
| 921 | if (!err && !req->r_reply_info.head->is_dentry) |
| 922 | d_delete(dentry); |
| 923 | ceph_mdsc_put_request(req); |
| 924 | out: |
| 925 | return err; |
| 926 | } |
| 927 | |
| 928 | static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 929 | struct inode *new_dir, struct dentry *new_dentry) |
| 930 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 931 | struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb); |
| 932 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 933 | struct ceph_mds_request *req; |
| 934 | int err; |
| 935 | |
| 936 | if (ceph_snap(old_dir) != ceph_snap(new_dir)) |
| 937 | return -EXDEV; |
| 938 | if (ceph_snap(old_dir) != CEPH_NOSNAP || |
| 939 | ceph_snap(new_dir) != CEPH_NOSNAP) |
| 940 | return -EROFS; |
| 941 | dout("rename dir %p dentry %p to dir %p dentry %p\n", |
| 942 | old_dir, old_dentry, new_dir, new_dentry); |
| 943 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS); |
| 944 | if (IS_ERR(req)) |
| 945 | return PTR_ERR(req); |
Sage Weil | 180061a | 2013-02-05 13:36:05 -0800 | [diff] [blame] | 946 | ihold(old_dir); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 947 | req->r_dentry = dget(new_dentry); |
| 948 | req->r_num_caps = 2; |
| 949 | req->r_old_dentry = dget(old_dentry); |
Sage Weil | 180061a | 2013-02-05 13:36:05 -0800 | [diff] [blame] | 950 | req->r_old_dentry_dir = old_dir; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 951 | req->r_locked_dir = new_dir; |
| 952 | req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 953 | req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 954 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 955 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 956 | /* release LINK_RDCACHE on source inode (mds will lock it) */ |
| 957 | req->r_old_inode_drop = CEPH_CAP_LINK_SHARED; |
| 958 | if (new_dentry->d_inode) |
| 959 | req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode); |
| 960 | err = ceph_mdsc_do_request(mdsc, old_dir, req); |
| 961 | if (!err && !req->r_reply_info.head->is_dentry) { |
| 962 | /* |
| 963 | * Normally d_move() is done by fill_trace (called by |
| 964 | * do_request, above). If there is no trace, we need |
| 965 | * to do it here. |
| 966 | */ |
Sage Weil | ea1409f | 2010-04-28 16:12:06 -0700 | [diff] [blame] | 967 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 968 | d_move(old_dentry, new_dentry); |
Sage Weil | ea1409f | 2010-04-28 16:12:06 -0700 | [diff] [blame] | 969 | |
| 970 | /* ensure target dentry is invalidated, despite |
| 971 | rehashing bug in vfs_rename_dir */ |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 972 | ceph_invalidate_dentry_lease(new_dentry); |
Yan, Zheng | 0a8a70f | 2014-04-14 13:13:02 +0800 | [diff] [blame] | 973 | |
| 974 | /* d_move screws up sibling dentries' offsets */ |
| 975 | ceph_dir_clear_complete(old_dir); |
| 976 | ceph_dir_clear_complete(new_dir); |
| 977 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 978 | } |
| 979 | ceph_mdsc_put_request(req); |
| 980 | return err; |
| 981 | } |
| 982 | |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 983 | /* |
| 984 | * Ensure a dentry lease will no longer revalidate. |
| 985 | */ |
| 986 | void ceph_invalidate_dentry_lease(struct dentry *dentry) |
| 987 | { |
| 988 | spin_lock(&dentry->d_lock); |
| 989 | dentry->d_time = jiffies; |
| 990 | ceph_dentry(dentry)->lease_shared_gen = 0; |
| 991 | spin_unlock(&dentry->d_lock); |
| 992 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 993 | |
| 994 | /* |
| 995 | * Check if dentry lease is valid. If not, delete the lease. Try to |
| 996 | * renew if the least is more than half up. |
| 997 | */ |
| 998 | static int dentry_lease_is_valid(struct dentry *dentry) |
| 999 | { |
| 1000 | struct ceph_dentry_info *di; |
| 1001 | struct ceph_mds_session *s; |
| 1002 | int valid = 0; |
| 1003 | u32 gen; |
| 1004 | unsigned long ttl; |
| 1005 | struct ceph_mds_session *session = NULL; |
| 1006 | struct inode *dir = NULL; |
| 1007 | u32 seq = 0; |
| 1008 | |
| 1009 | spin_lock(&dentry->d_lock); |
| 1010 | di = ceph_dentry(dentry); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1011 | if (di->lease_session) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1012 | s = di->lease_session; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 1013 | spin_lock(&s->s_gen_ttl_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1014 | gen = s->s_cap_gen; |
| 1015 | ttl = s->s_cap_ttl; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 1016 | spin_unlock(&s->s_gen_ttl_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (di->lease_gen == gen && |
| 1019 | time_before(jiffies, dentry->d_time) && |
| 1020 | time_before(jiffies, ttl)) { |
| 1021 | valid = 1; |
| 1022 | if (di->lease_renew_after && |
| 1023 | time_after(jiffies, di->lease_renew_after)) { |
| 1024 | /* we should renew */ |
| 1025 | dir = dentry->d_parent->d_inode; |
| 1026 | session = ceph_get_mds_session(s); |
| 1027 | seq = di->lease_seq; |
| 1028 | di->lease_renew_after = 0; |
| 1029 | di->lease_renew_from = jiffies; |
| 1030 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | spin_unlock(&dentry->d_lock); |
| 1034 | |
| 1035 | if (session) { |
| 1036 | ceph_mdsc_lease_send_msg(session, dir, dentry, |
| 1037 | CEPH_MDS_LEASE_RENEW, seq); |
| 1038 | ceph_put_mds_session(session); |
| 1039 | } |
| 1040 | dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid); |
| 1041 | return valid; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * Check if directory-wide content lease/cap is valid. |
| 1046 | */ |
| 1047 | static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry) |
| 1048 | { |
| 1049 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 1050 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1051 | int valid = 0; |
| 1052 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1053 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1054 | if (ci->i_shared_gen == di->lease_shared_gen) |
| 1055 | valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1056 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1057 | dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n", |
| 1058 | dir, (unsigned)ci->i_shared_gen, dentry, |
| 1059 | (unsigned)di->lease_shared_gen, valid); |
| 1060 | return valid; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Check if cached dentry can be trusted. |
| 1065 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1066 | static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1067 | { |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1068 | int valid = 0; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1069 | struct inode *dir; |
| 1070 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1071 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1072 | return -ECHILD; |
| 1073 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1074 | dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry, |
| 1075 | dentry, dentry->d_inode, ceph_dentry(dentry)->offset); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1076 | |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1077 | dir = ceph_get_dentry_parent_inode(dentry); |
| 1078 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1079 | /* always trust cached snapped dentries, snapdir dentry */ |
| 1080 | if (ceph_snap(dir) != CEPH_NOSNAP) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1081 | dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry, |
| 1082 | dentry, dentry->d_inode); |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1083 | valid = 1; |
| 1084 | } else if (dentry->d_inode && |
| 1085 | ceph_snap(dentry->d_inode) == CEPH_SNAPDIR) { |
| 1086 | valid = 1; |
| 1087 | } else if (dentry_lease_is_valid(dentry) || |
| 1088 | dir_lease_is_valid(dir, dentry)) { |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1089 | if (dentry->d_inode) |
| 1090 | valid = ceph_is_any_caps(dentry->d_inode); |
| 1091 | else |
| 1092 | valid = 1; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1093 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1094 | |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1095 | dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid"); |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1096 | if (valid) { |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1097 | ceph_dentry_lru_touch(dentry); |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1098 | } else { |
| 1099 | ceph_dir_clear_complete(dir); |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1100 | } |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1101 | iput(dir); |
| 1102 | return valid; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | /* |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1106 | * Release our ceph_dentry_info. |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1107 | */ |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1108 | static void ceph_d_release(struct dentry *dentry) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1109 | { |
| 1110 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1111 | |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1112 | dout("d_release %p\n", dentry); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1113 | ceph_dentry_lru_del(dentry); |
| 1114 | if (di->lease_session) |
| 1115 | ceph_put_mds_session(di->lease_session); |
| 1116 | kmem_cache_free(ceph_dentry_cachep, di); |
| 1117 | dentry->d_fsdata = NULL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | static int ceph_snapdir_d_revalidate(struct dentry *dentry, |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1121 | unsigned int flags) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1122 | { |
| 1123 | /* |
| 1124 | * Eventually, we'll want to revalidate snapped metadata |
| 1125 | * too... probably... |
| 1126 | */ |
| 1127 | return 1; |
| 1128 | } |
| 1129 | |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1130 | /* |
| 1131 | * When the VFS prunes a dentry from the cache, we need to clear the |
| 1132 | * complete flag on the parent directory. |
| 1133 | * |
| 1134 | * Called under dentry->d_lock. |
| 1135 | */ |
| 1136 | static void ceph_d_prune(struct dentry *dentry) |
| 1137 | { |
Sage Weil | 774ac21 | 2011-11-11 09:48:08 -0800 | [diff] [blame] | 1138 | dout("ceph_d_prune %p\n", dentry); |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1139 | |
| 1140 | /* do we have a valid parent? */ |
Sage Weil | 8842b3b | 2012-06-07 13:43:35 -0700 | [diff] [blame] | 1141 | if (IS_ROOT(dentry)) |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1142 | return; |
| 1143 | |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 1144 | /* if we are not hashed, we don't affect dir's completeness */ |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1145 | if (d_unhashed(dentry)) |
| 1146 | return; |
| 1147 | |
| 1148 | /* |
| 1149 | * we hold d_lock, so d_parent is stable, and d_fsdata is never |
| 1150 | * cleared until d_release |
| 1151 | */ |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 1152 | ceph_dir_clear_complete(dentry->d_parent->d_inode); |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1153 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* |
| 1156 | * read() on a dir. This weird interface hack only works if mounted |
| 1157 | * with '-o dirstat'. |
| 1158 | */ |
| 1159 | static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size, |
| 1160 | loff_t *ppos) |
| 1161 | { |
| 1162 | struct ceph_file_info *cf = file->private_data; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1163 | struct inode *inode = file_inode(file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1164 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1165 | int left; |
Sage Weil | ae59808 | 2011-05-12 14:28:05 -0700 | [diff] [blame] | 1166 | const int bufsize = 1024; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1167 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1168 | if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT)) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1169 | return -EISDIR; |
| 1170 | |
| 1171 | if (!cf->dir_info) { |
Sage Weil | ae59808 | 2011-05-12 14:28:05 -0700 | [diff] [blame] | 1172 | cf->dir_info = kmalloc(bufsize, GFP_NOFS); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1173 | if (!cf->dir_info) |
| 1174 | return -ENOMEM; |
| 1175 | cf->dir_info_len = |
Sage Weil | ae59808 | 2011-05-12 14:28:05 -0700 | [diff] [blame] | 1176 | snprintf(cf->dir_info, bufsize, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1177 | "entries: %20lld\n" |
| 1178 | " files: %20lld\n" |
| 1179 | " subdirs: %20lld\n" |
| 1180 | "rentries: %20lld\n" |
| 1181 | " rfiles: %20lld\n" |
| 1182 | " rsubdirs: %20lld\n" |
| 1183 | "rbytes: %20lld\n" |
| 1184 | "rctime: %10ld.%09ld\n", |
| 1185 | ci->i_files + ci->i_subdirs, |
| 1186 | ci->i_files, |
| 1187 | ci->i_subdirs, |
| 1188 | ci->i_rfiles + ci->i_rsubdirs, |
| 1189 | ci->i_rfiles, |
| 1190 | ci->i_rsubdirs, |
| 1191 | ci->i_rbytes, |
| 1192 | (long)ci->i_rctime.tv_sec, |
| 1193 | (long)ci->i_rctime.tv_nsec); |
| 1194 | } |
| 1195 | |
| 1196 | if (*ppos >= cf->dir_info_len) |
| 1197 | return 0; |
| 1198 | size = min_t(unsigned, size, cf->dir_info_len-*ppos); |
| 1199 | left = copy_to_user(buf, cf->dir_info + *ppos, size); |
| 1200 | if (left == size) |
| 1201 | return -EFAULT; |
| 1202 | *ppos += (size - left); |
| 1203 | return size - left; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * an fsync() on a dir will wait for any uncommitted directory |
| 1208 | * operations to commit. |
| 1209 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1210 | static int ceph_dir_fsync(struct file *file, loff_t start, loff_t end, |
| 1211 | int datasync) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1212 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1213 | struct inode *inode = file_inode(file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1214 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1215 | struct list_head *head = &ci->i_unsafe_dirops; |
| 1216 | struct ceph_mds_request *req; |
| 1217 | u64 last_tid; |
| 1218 | int ret = 0; |
| 1219 | |
| 1220 | dout("dir_fsync %p\n", inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1221 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 1222 | if (ret) |
| 1223 | return ret; |
| 1224 | mutex_lock(&inode->i_mutex); |
| 1225 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1226 | spin_lock(&ci->i_unsafe_lock); |
| 1227 | if (list_empty(head)) |
| 1228 | goto out; |
| 1229 | |
| 1230 | req = list_entry(head->prev, |
| 1231 | struct ceph_mds_request, r_unsafe_dir_item); |
| 1232 | last_tid = req->r_tid; |
| 1233 | |
| 1234 | do { |
| 1235 | ceph_mdsc_get_request(req); |
| 1236 | spin_unlock(&ci->i_unsafe_lock); |
Sage Weil | 2ff179e | 2012-01-03 10:09:07 -0800 | [diff] [blame] | 1237 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1238 | dout("dir_fsync %p wait on tid %llu (until %llu)\n", |
| 1239 | inode, req->r_tid, last_tid); |
| 1240 | if (req->r_timeout) { |
| 1241 | ret = wait_for_completion_timeout( |
| 1242 | &req->r_safe_completion, req->r_timeout); |
| 1243 | if (ret > 0) |
| 1244 | ret = 0; |
| 1245 | else if (ret == 0) |
| 1246 | ret = -EIO; /* timed out */ |
| 1247 | } else { |
| 1248 | wait_for_completion(&req->r_safe_completion); |
| 1249 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1250 | ceph_mdsc_put_request(req); |
| 1251 | |
Sage Weil | 2ff179e | 2012-01-03 10:09:07 -0800 | [diff] [blame] | 1252 | spin_lock(&ci->i_unsafe_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1253 | if (ret || list_empty(head)) |
| 1254 | break; |
| 1255 | req = list_entry(head->next, |
| 1256 | struct ceph_mds_request, r_unsafe_dir_item); |
| 1257 | } while (req->r_tid < last_tid); |
| 1258 | out: |
| 1259 | spin_unlock(&ci->i_unsafe_lock); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1260 | mutex_unlock(&inode->i_mutex); |
| 1261 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1262 | return ret; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * We maintain a private dentry LRU. |
| 1267 | * |
| 1268 | * FIXME: this needs to be changed to a per-mds lru to be useful. |
| 1269 | */ |
| 1270 | void ceph_dentry_lru_add(struct dentry *dn) |
| 1271 | { |
| 1272 | struct ceph_dentry_info *di = ceph_dentry(dn); |
| 1273 | struct ceph_mds_client *mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1274 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1275 | dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1276 | mdsc = ceph_sb_to_client(dn->d_sb)->mdsc; |
| 1277 | spin_lock(&mdsc->dentry_lru_lock); |
| 1278 | list_add_tail(&di->lru, &mdsc->dentry_lru); |
| 1279 | mdsc->num_dentry++; |
| 1280 | spin_unlock(&mdsc->dentry_lru_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | void ceph_dentry_lru_touch(struct dentry *dn) |
| 1284 | { |
| 1285 | struct ceph_dentry_info *di = ceph_dentry(dn); |
| 1286 | struct ceph_mds_client *mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1287 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1288 | dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn, |
| 1289 | di->offset); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1290 | mdsc = ceph_sb_to_client(dn->d_sb)->mdsc; |
| 1291 | spin_lock(&mdsc->dentry_lru_lock); |
| 1292 | list_move_tail(&di->lru, &mdsc->dentry_lru); |
| 1293 | spin_unlock(&mdsc->dentry_lru_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | void ceph_dentry_lru_del(struct dentry *dn) |
| 1297 | { |
| 1298 | struct ceph_dentry_info *di = ceph_dentry(dn); |
| 1299 | struct ceph_mds_client *mdsc; |
| 1300 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1301 | dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1302 | mdsc = ceph_sb_to_client(dn->d_sb)->mdsc; |
| 1303 | spin_lock(&mdsc->dentry_lru_lock); |
| 1304 | list_del_init(&di->lru); |
| 1305 | mdsc->num_dentry--; |
| 1306 | spin_unlock(&mdsc->dentry_lru_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 1309 | /* |
| 1310 | * Return name hash for a given dentry. This is dependent on |
| 1311 | * the parent directory's hash function. |
| 1312 | */ |
Sage Weil | e5f86dc | 2011-07-26 11:30:55 -0700 | [diff] [blame] | 1313 | unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn) |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 1314 | { |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 1315 | struct ceph_inode_info *dci = ceph_inode(dir); |
| 1316 | |
| 1317 | switch (dci->i_dir_layout.dl_dir_hash) { |
| 1318 | case 0: /* for backward compat */ |
| 1319 | case CEPH_STR_HASH_LINUX: |
| 1320 | return dn->d_name.hash; |
| 1321 | |
| 1322 | default: |
| 1323 | return ceph_str_hash(dci->i_dir_layout.dl_dir_hash, |
| 1324 | dn->d_name.name, dn->d_name.len); |
| 1325 | } |
| 1326 | } |
| 1327 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1328 | const struct file_operations ceph_dir_fops = { |
| 1329 | .read = ceph_read_dir, |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 1330 | .iterate = ceph_readdir, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1331 | .llseek = ceph_dir_llseek, |
| 1332 | .open = ceph_open, |
| 1333 | .release = ceph_release, |
| 1334 | .unlocked_ioctl = ceph_ioctl, |
| 1335 | .fsync = ceph_dir_fsync, |
| 1336 | }; |
| 1337 | |
| 1338 | const struct inode_operations ceph_dir_iops = { |
| 1339 | .lookup = ceph_lookup, |
| 1340 | .permission = ceph_permission, |
| 1341 | .getattr = ceph_getattr, |
| 1342 | .setattr = ceph_setattr, |
| 1343 | .setxattr = ceph_setxattr, |
| 1344 | .getxattr = ceph_getxattr, |
| 1345 | .listxattr = ceph_listxattr, |
| 1346 | .removexattr = ceph_removexattr, |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 1347 | .get_acl = ceph_get_acl, |
Sage Weil | 72466d0 | 2014-01-29 06:22:25 -0800 | [diff] [blame] | 1348 | .set_acl = ceph_set_acl, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1349 | .mknod = ceph_mknod, |
| 1350 | .symlink = ceph_symlink, |
| 1351 | .mkdir = ceph_mkdir, |
| 1352 | .link = ceph_link, |
| 1353 | .unlink = ceph_unlink, |
| 1354 | .rmdir = ceph_unlink, |
| 1355 | .rename = ceph_rename, |
| 1356 | .create = ceph_create, |
Miklos Szeredi | 2d83bde | 2012-06-05 15:10:25 +0200 | [diff] [blame] | 1357 | .atomic_open = ceph_atomic_open, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1358 | }; |
| 1359 | |
Sage Weil | 52dfb8a | 2010-08-03 10:25:30 -0700 | [diff] [blame] | 1360 | const struct dentry_operations ceph_dentry_ops = { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1361 | .d_revalidate = ceph_d_revalidate, |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1362 | .d_release = ceph_d_release, |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1363 | .d_prune = ceph_d_prune, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1364 | }; |
| 1365 | |
Sage Weil | 52dfb8a | 2010-08-03 10:25:30 -0700 | [diff] [blame] | 1366 | const struct dentry_operations ceph_snapdir_dentry_ops = { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1367 | .d_revalidate = ceph_snapdir_d_revalidate, |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1368 | .d_release = ceph_d_release, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1369 | }; |
| 1370 | |
Sage Weil | 52dfb8a | 2010-08-03 10:25:30 -0700 | [diff] [blame] | 1371 | const struct dentry_operations ceph_snap_dentry_ops = { |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1372 | .d_release = ceph_d_release, |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1373 | .d_prune = ceph_d_prune, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1374 | }; |