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