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