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