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