blob: 2ffc7fe8da52f272b5d6bf56da7e75f58a85cbda [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2817b002009-10-06 11:31:08 -07002
3#include <linux/spinlock.h>
4#include <linux/fs_struct.h>
5#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Sage Weil2817b002009-10-06 11:31:08 -07007#include <linux/sched.h>
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02008#include <linux/xattr.h>
Sage Weil2817b002009-10-06 11:31:08 -07009
10#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include "mds_client.h"
Sage Weil2817b002009-10-06 11:31:08 -070012
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 Weil52dfb8a2010-08-03 10:25:30 -070030const struct dentry_operations ceph_dentry_ops;
Sage Weil2817b002009-10-06 11:31:08 -070031
32/*
33 * Initialize ceph dentry state.
34 */
35int ceph_init_dentry(struct dentry *dentry)
36{
37 struct ceph_dentry_info *di;
38
39 if (dentry->d_fsdata)
40 return 0;
41
Geliang Tang99ec2692016-03-13 15:26:29 +080042 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -070043 if (!di)
44 return -ENOMEM; /* oh well */
45
46 spin_lock(&dentry->d_lock);
Sage Weil8c6efb52010-04-23 11:36:54 -070047 if (dentry->d_fsdata) {
48 /* lost a race */
49 kmem_cache_free(ceph_dentry_cachep, di);
Sage Weil2817b002009-10-06 11:31:08 -070050 goto out_unlock;
Sage Weil8c6efb52010-04-23 11:36:54 -070051 }
Sage Weil48d0cbd2011-07-26 11:30:15 -070052
David Howells2b0143b2015-03-17 22:25:59 +000053 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
Sage Weil48d0cbd2011-07-26 11:30:15 -070054 d_set_d_op(dentry, &ceph_dentry_ops);
David Howells2b0143b2015-03-17 22:25:59 +000055 else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
Sage Weil48d0cbd2011-07-26 11:30:15 -070056 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
57 else
58 d_set_d_op(dentry, &ceph_snap_dentry_ops);
59
Sage Weil2817b002009-10-06 11:31:08 -070060 di->dentry = dentry;
61 di->lease_session = NULL;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +020062 di->time = jiffies;
Sage Weil48d0cbd2011-07-26 11:30:15 -070063 /* avoid reordering d_fsdata setup so that the check above is safe */
64 smp_mb();
65 dentry->d_fsdata = di;
Sage Weil2817b002009-10-06 11:31:08 -070066 ceph_dentry_lru_add(dentry);
67out_unlock:
68 spin_unlock(&dentry->d_lock);
69 return 0;
70}
71
Sage Weil2817b002009-10-06 11:31:08 -070072/*
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080073 * for f_pos for readdir:
74 * - hash order:
75 * (0xff << 52) | ((24 bits hash) << 28) |
76 * (the nth entry has hash collision);
77 * - frag+name order;
78 * ((frag value) << 28) | (the nth entry in frag);
Sage Weil2817b002009-10-06 11:31:08 -070079 */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080080#define OFFSET_BITS 28
81#define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
82#define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
83loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
84{
85 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
86 if (hash_order)
87 fpos |= HASH_ORDER;
88 return fpos;
89}
90
91static bool is_hash_order(loff_t p)
92{
93 return (p & HASH_ORDER) == HASH_ORDER;
94}
95
Sage Weil2817b002009-10-06 11:31:08 -070096static unsigned fpos_frag(loff_t p)
97{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080098 return p >> OFFSET_BITS;
Sage Weil2817b002009-10-06 11:31:08 -070099}
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800100
101static unsigned fpos_hash(loff_t p)
102{
103 return ceph_frag_value(fpos_frag(p));
104}
105
Sage Weil2817b002009-10-06 11:31:08 -0700106static unsigned fpos_off(loff_t p)
107{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800108 return p & OFFSET_MASK;
Sage Weil2817b002009-10-06 11:31:08 -0700109}
110
Yan, Zheng4d5f5df2014-02-13 19:40:26 +0800111static int fpos_cmp(loff_t l, loff_t r)
112{
113 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
114 if (v)
115 return v;
116 return (int)(fpos_off(l) - fpos_off(r));
117}
118
Sage Weil2817b002009-10-06 11:31:08 -0700119/*
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800120 * make note of the last dentry we read, so we can
121 * continue at the same lexicographical point,
122 * regardless of what dir changes take place on the
123 * server.
124 */
125static int note_last_dentry(struct ceph_file_info *fi, const char *name,
126 int len, unsigned next_offset)
127{
128 char *buf = kmalloc(len+1, GFP_KERNEL);
129 if (!buf)
130 return -ENOMEM;
131 kfree(fi->last_name);
132 fi->last_name = buf;
133 memcpy(fi->last_name, name, len);
134 fi->last_name[len] = 0;
135 fi->next_offset = next_offset;
136 dout("note_last_dentry '%s'\n", fi->last_name);
137 return 0;
138}
139
Yan, Zhengc530cd22016-04-28 17:43:35 +0800140
141static struct dentry *
142__dcache_find_get_entry(struct dentry *parent, u64 idx,
143 struct ceph_readdir_cache_control *cache_ctl)
144{
145 struct inode *dir = d_inode(parent);
146 struct dentry *dentry;
147 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
148 loff_t ptr_pos = idx * sizeof(struct dentry *);
149 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
150
151 if (ptr_pos >= i_size_read(dir))
152 return NULL;
153
154 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
155 ceph_readdir_cache_release(cache_ctl);
156 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
157 if (!cache_ctl->page) {
158 dout(" page %lu not found\n", ptr_pgoff);
159 return ERR_PTR(-EAGAIN);
160 }
161 /* reading/filling the cache are serialized by
162 i_mutex, no need to use page lock */
163 unlock_page(cache_ctl->page);
164 cache_ctl->dentries = kmap(cache_ctl->page);
165 }
166
167 cache_ctl->index = idx & idx_mask;
168
169 rcu_read_lock();
170 spin_lock(&parent->d_lock);
171 /* check i_size again here, because empty directory can be
172 * marked as complete while not holding the i_mutex. */
173 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
174 dentry = cache_ctl->dentries[cache_ctl->index];
175 else
176 dentry = NULL;
177 spin_unlock(&parent->d_lock);
178 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
179 dentry = NULL;
180 rcu_read_unlock();
181 return dentry ? : ERR_PTR(-EAGAIN);
182}
183
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800184/*
Sage Weil2817b002009-10-06 11:31:08 -0700185 * When possible, we try to satisfy a readdir by peeking at the
186 * dcache. We make this work by carefully ordering dentries on
Al Viro946e51f2014-10-26 19:19:16 -0400187 * d_child when we initially get results back from the MDS, and
Sage Weil2817b002009-10-06 11:31:08 -0700188 * falling back to a "normal" sync readdir if any dentries in the dir
189 * are dropped.
190 *
Yan, Zheng2f276c52013-03-13 19:44:32 +0800191 * Complete dir indicates that we have all dentries in the dir. It is
Sage Weil2817b002009-10-06 11:31:08 -0700192 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
193 * the MDS if/when the directory is modified).
194 */
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800195static int __dcache_readdir(struct file *file, struct dir_context *ctx,
196 u32 shared_gen)
Sage Weil2817b002009-10-06 11:31:08 -0700197{
Al Viro77acfa22013-05-17 16:52:26 -0400198 struct ceph_file_info *fi = file->private_data;
Al Virob5830432014-10-31 01:22:04 -0400199 struct dentry *parent = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000200 struct inode *dir = d_inode(parent);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800201 struct dentry *dentry, *last = NULL;
Sage Weil2817b002009-10-06 11:31:08 -0700202 struct ceph_dentry_info *di;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800203 struct ceph_readdir_cache_control cache_ctl = {};
Yan, Zhengc530cd22016-04-28 17:43:35 +0800204 u64 idx = 0;
205 int err = 0;
Sage Weil2817b002009-10-06 11:31:08 -0700206
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800207 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
Sage Weil2817b002009-10-06 11:31:08 -0700208
Yan, Zhengc530cd22016-04-28 17:43:35 +0800209 /* search start position */
210 if (ctx->pos > 2) {
211 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
212 while (count > 0) {
213 u64 step = count >> 1;
214 dentry = __dcache_find_get_entry(parent, idx + step,
215 &cache_ctl);
216 if (!dentry) {
217 /* use linar search */
218 idx = 0;
219 break;
220 }
221 if (IS_ERR(dentry)) {
222 err = PTR_ERR(dentry);
223 goto out;
224 }
225 di = ceph_dentry(dentry);
226 spin_lock(&dentry->d_lock);
227 if (fpos_cmp(di->offset, ctx->pos) < 0) {
228 idx += step + 1;
229 count -= step + 1;
230 } else {
231 count = step;
232 }
233 spin_unlock(&dentry->d_lock);
234 dput(dentry);
235 }
236
237 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
Sage Weil2817b002009-10-06 11:31:08 -0700238 }
239
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800240
Yan, Zhengc530cd22016-04-28 17:43:35 +0800241 for (;;) {
242 bool emit_dentry = false;
243 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
244 if (!dentry) {
Sage Weil9cfa1092011-07-26 11:26:18 -0700245 fi->flags |= CEPH_F_ATEND;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800246 err = 0;
247 break;
Sage Weil2817b002009-10-06 11:31:08 -0700248 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800249 if (IS_ERR(dentry)) {
250 err = PTR_ERR(dentry);
251 goto out;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800252 }
253
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800254 di = ceph_dentry(dentry);
255 spin_lock(&dentry->d_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800256 if (di->lease_shared_gen == shared_gen &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800257 d_really_is_positive(dentry) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800258 fpos_cmp(ctx->pos, di->offset) <= 0) {
259 emit_dentry = true;
Sage Weil2817b002009-10-06 11:31:08 -0700260 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800261 spin_unlock(&dentry->d_lock);
262
263 if (emit_dentry) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800264 dout(" %llx dentry %p %pd %p\n", di->offset,
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800265 dentry, dentry, d_inode(dentry));
266 ctx->pos = di->offset;
267 if (!dir_emit(ctx, dentry->d_name.name,
268 dentry->d_name.len,
269 ceph_translate_ino(dentry->d_sb,
270 d_inode(dentry)->i_ino),
271 d_inode(dentry)->i_mode >> 12)) {
272 dput(dentry);
273 err = 0;
274 break;
275 }
276 ctx->pos++;
277
278 if (last)
279 dput(last);
280 last = dentry;
281 } else {
282 dput(dentry);
283 }
Sage Weil2817b002009-10-06 11:31:08 -0700284 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800285out:
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800286 ceph_readdir_cache_release(&cache_ctl);
287 if (last) {
288 int ret;
289 di = ceph_dentry(last);
290 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
291 fpos_off(di->offset) + 1);
292 if (ret < 0)
293 err = ret;
Al Viro77acfa22013-05-17 16:52:26 -0400294 dput(last);
Yan, Zhengacccf012017-07-06 11:12:21 +0800295 /* last_name no longer match cache index */
296 if (fi->readdir_cache_idx >= 0) {
297 fi->readdir_cache_idx = -1;
298 fi->dir_release_count = 0;
299 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800300 }
Sage Weil2817b002009-10-06 11:31:08 -0700301 return err;
302}
303
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800304static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
305{
306 if (!fi->last_readdir)
307 return true;
308 if (is_hash_order(pos))
309 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
310 else
311 return fi->frag != fpos_frag(pos);
312}
313
Al Viro77acfa22013-05-17 16:52:26 -0400314static int ceph_readdir(struct file *file, struct dir_context *ctx)
Sage Weil2817b002009-10-06 11:31:08 -0700315{
Al Viro77acfa22013-05-17 16:52:26 -0400316 struct ceph_file_info *fi = file->private_data;
317 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -0700318 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700319 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
320 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800321 int i;
Sage Weil2817b002009-10-06 11:31:08 -0700322 int err;
Yan, Zheng94035142017-04-24 11:56:50 +0800323 unsigned frag = -1;
Sage Weil2817b002009-10-06 11:31:08 -0700324 struct ceph_mds_reply_info_parsed *rinfo;
Sage Weil2817b002009-10-06 11:31:08 -0700325
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800326 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
Sage Weil9cfa1092011-07-26 11:26:18 -0700327 if (fi->flags & CEPH_F_ATEND)
Sage Weil2817b002009-10-06 11:31:08 -0700328 return 0;
329
330 /* always start with . and .. */
Al Viro77acfa22013-05-17 16:52:26 -0400331 if (ctx->pos == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700332 dout("readdir off 0 -> '.'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400333 if (!dir_emit(ctx, ".", 1,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800334 ceph_translate_ino(inode->i_sb, inode->i_ino),
Al Viro77acfa22013-05-17 16:52:26 -0400335 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700336 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400337 ctx->pos = 1;
Sage Weil2817b002009-10-06 11:31:08 -0700338 }
Al Viro77acfa22013-05-17 16:52:26 -0400339 if (ctx->pos == 1) {
Al Virob5830432014-10-31 01:22:04 -0400340 ino_t ino = parent_ino(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700341 dout("readdir off 1 -> '..'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400342 if (!dir_emit(ctx, "..", 2,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800343 ceph_translate_ino(inode->i_sb, ino),
Al Viro77acfa22013-05-17 16:52:26 -0400344 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700345 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400346 ctx->pos = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700347 }
348
349 /* can we use the dcache? */
Sage Weilbe655592011-11-30 09:47:09 -0800350 spin_lock(&ci->i_ceph_lock);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800351 if (ceph_test_mount_opt(fsc, DCACHE) &&
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700352 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
Sage Weila0dff782010-07-22 13:47:21 -0700353 ceph_snap(inode) != CEPH_SNAPDIR &&
Yan, Zheng70db4f32014-10-21 18:09:56 -0700354 __ceph_dir_is_complete_ordered(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700355 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800356 u32 shared_gen = ci->i_shared_gen;
Sage Weilbe655592011-11-30 09:47:09 -0800357 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800358 err = __dcache_readdir(file, ctx, shared_gen);
Sage Weilefa4c122010-10-18 14:04:31 -0700359 if (err != -EAGAIN)
Sage Weil2817b002009-10-06 11:31:08 -0700360 return err;
Sage Weilefa4c122010-10-18 14:04:31 -0700361 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800362 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700363 }
Sage Weil2817b002009-10-06 11:31:08 -0700364
365 /* proceed with a normal readdir */
Sage Weil2817b002009-10-06 11:31:08 -0700366more:
367 /* do we have the correct frag content buffered? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800368 if (need_send_readdir(fi, ctx->pos)) {
Sage Weil2817b002009-10-06 11:31:08 -0700369 struct ceph_mds_request *req;
370 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
371 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
372
373 /* discard old result, if any */
Sage Weil393f6622010-03-10 12:03:32 -0800374 if (fi->last_readdir) {
Sage Weil2817b002009-10-06 11:31:08 -0700375 ceph_mdsc_put_request(fi->last_readdir);
Sage Weil393f6622010-03-10 12:03:32 -0800376 fi->last_readdir = NULL;
377 }
Sage Weil2817b002009-10-06 11:31:08 -0700378
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800379 if (is_hash_order(ctx->pos)) {
Yan, Zheng94035142017-04-24 11:56:50 +0800380 /* fragtree isn't always accurate. choose frag
381 * based on previous reply when possible. */
382 if (frag == (unsigned)-1)
383 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
384 NULL, NULL);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800385 } else {
386 frag = fpos_frag(ctx->pos);
387 }
388
Sage Weil2817b002009-10-06 11:31:08 -0700389 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
390 ceph_vinop(inode), frag, fi->last_name);
391 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
392 if (IS_ERR(req))
393 return PTR_ERR(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800394 err = ceph_alloc_readdir_reply_buffer(req, inode);
395 if (err) {
396 ceph_mdsc_put_request(req);
397 return err;
398 }
Sage Weil2817b002009-10-06 11:31:08 -0700399 /* hints to request -> mds selection code */
400 req->r_direct_mode = USE_AUTH_MDS;
401 req->r_direct_hash = ceph_frag_value(frag);
402 req->r_direct_is_hash = true;
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400403 if (fi->last_name) {
Yan, Zheng687265e2015-06-13 17:27:05 +0800404 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400405 if (!req->r_path2) {
406 ceph_mdsc_put_request(req);
407 return -ENOMEM;
408 }
409 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800410 req->r_dir_release_cnt = fi->dir_release_count;
411 req->r_dir_ordered_cnt = fi->dir_ordered_count;
412 req->r_readdir_cache_idx = fi->readdir_cache_idx;
Sage Weil2817b002009-10-06 11:31:08 -0700413 req->r_readdir_offset = fi->next_offset;
414 req->r_args.readdir.frag = cpu_to_le32(frag);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800415 req->r_args.readdir.flags =
416 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400417
418 req->r_inode = inode;
419 ihold(inode);
420 req->r_dentry = dget(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700421 err = ceph_mdsc_do_request(mdsc, NULL, req);
422 if (err < 0) {
423 ceph_mdsc_put_request(req);
424 return err;
425 }
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800426 dout("readdir got and parsed readdir result=%d on "
427 "frag %x, end=%d, complete=%d, hash_order=%d\n",
428 err, frag,
Sage Weil2817b002009-10-06 11:31:08 -0700429 (int)req->r_reply_info.dir_end,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800430 (int)req->r_reply_info.dir_complete,
431 (int)req->r_reply_info.hash_order);
Sage Weil2817b002009-10-06 11:31:08 -0700432
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800433 rinfo = &req->r_reply_info;
434 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
435 frag = le32_to_cpu(rinfo->dir_dir->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800436 if (!rinfo->hash_order) {
437 fi->next_offset = req->r_readdir_offset;
438 /* adjust ctx->pos to beginning of frag */
439 ctx->pos = ceph_make_fpos(frag,
440 fi->next_offset,
441 false);
442 }
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800443 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800444
Yan, Zhengf0494202014-02-27 16:26:24 +0800445 fi->frag = frag;
Sage Weil2817b002009-10-06 11:31:08 -0700446 fi->last_readdir = req;
447
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800448 if (req->r_did_prepopulate) {
449 fi->readdir_cache_idx = req->r_readdir_cache_idx;
450 if (fi->readdir_cache_idx < 0) {
451 /* preclude from marking dir ordered */
452 fi->dir_ordered_count = 0;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800453 } else if (ceph_frag_is_leftmost(frag) &&
454 fi->next_offset == 2) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800455 /* note dir version at start of readdir so
456 * we can tell if any dentries get dropped */
457 fi->dir_release_count = req->r_dir_release_cnt;
458 fi->dir_ordered_count = req->r_dir_ordered_cnt;
459 }
460 } else {
461 dout("readdir !did_prepopulate");
462 /* disable readdir cache */
463 fi->readdir_cache_idx = -1;
464 /* preclude from marking dir complete */
465 fi->dir_release_count = 0;
466 }
467
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800468 /* note next offset and last dentry name */
469 if (rinfo->dir_nr > 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800470 struct ceph_mds_reply_dir_entry *rde =
471 rinfo->dir_entries + (rinfo->dir_nr-1);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800472 unsigned next_offset = req->r_reply_info.dir_end ?
473 2 : (fpos_off(rde->offset) + 1);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800474 err = note_last_dentry(fi, rde->name, rde->name_len,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800475 next_offset);
Sage Weil2817b002009-10-06 11:31:08 -0700476 if (err)
477 return err;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800478 } else if (req->r_reply_info.dir_end) {
479 fi->next_offset = 2;
480 /* keep last name */
Sage Weil2817b002009-10-06 11:31:08 -0700481 }
482 }
483
484 rinfo = &fi->last_readdir->r_reply_info;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800485 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800486 fi->frag, rinfo->dir_nr, ctx->pos,
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800487 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
Al Viro77acfa22013-05-17 16:52:26 -0400488
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800489 i = 0;
490 /* search start position */
491 if (rinfo->dir_nr > 0) {
492 int step, nr = rinfo->dir_nr;
493 while (nr > 0) {
494 step = nr >> 1;
495 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
496 i += step + 1;
497 nr -= step + 1;
498 } else {
499 nr = step;
500 }
501 }
502 }
503 for (; i < rinfo->dir_nr; i++) {
504 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
Sage Weil3105c192010-11-18 09:15:07 -0800505 struct ceph_vino vino;
506 ino_t ino;
Yan, Zheng94035142017-04-24 11:56:50 +0800507 u32 ftype;
Sage Weil3105c192010-11-18 09:15:07 -0800508
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800509 BUG_ON(rde->offset < ctx->pos);
510
511 ctx->pos = rde->offset;
512 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
513 i, rinfo->dir_nr, ctx->pos,
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800514 rde->name_len, rde->name, &rde->inode.in);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800515
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800516 BUG_ON(!rde->inode.in);
517 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
518 vino.ino = le64_to_cpu(rde->inode.in->ino);
519 vino.snap = le64_to_cpu(rde->inode.in->snapid);
Sage Weil3105c192010-11-18 09:15:07 -0800520 ino = ceph_vino_to_ino(vino);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800521
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800522 if (!dir_emit(ctx, rde->name, rde->name_len,
523 ceph_translate_ino(inode->i_sb, ino), ftype)) {
Sage Weil2817b002009-10-06 11:31:08 -0700524 dout("filldir stopping us...\n");
525 return 0;
526 }
Al Viro77acfa22013-05-17 16:52:26 -0400527 ctx->pos++;
Sage Weil2817b002009-10-06 11:31:08 -0700528 }
529
Yan, Zheng94035142017-04-24 11:56:50 +0800530 ceph_mdsc_put_request(fi->last_readdir);
531 fi->last_readdir = NULL;
532
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800533 if (fi->next_offset > 2) {
Yan, Zheng94035142017-04-24 11:56:50 +0800534 frag = fi->frag;
Sage Weil2817b002009-10-06 11:31:08 -0700535 goto more;
536 }
537
538 /* more frags? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800539 if (!ceph_frag_is_rightmost(fi->frag)) {
Yan, Zheng94035142017-04-24 11:56:50 +0800540 frag = ceph_frag_next(fi->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800541 if (is_hash_order(ctx->pos)) {
542 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
543 fi->next_offset, true);
544 if (new_pos > ctx->pos)
545 ctx->pos = new_pos;
546 /* keep last_name */
547 } else {
548 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
549 kfree(fi->last_name);
550 fi->last_name = NULL;
551 }
Sage Weil2817b002009-10-06 11:31:08 -0700552 dout("readdir next frag is %x\n", frag);
553 goto more;
554 }
Sage Weil9cfa1092011-07-26 11:26:18 -0700555 fi->flags |= CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700556
557 /*
558 * if dir_release_count still matches the dir, no dentries
559 * were released during the whole readdir, and we should have
560 * the complete dir contents in our cache.
561 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800562 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
563 spin_lock(&ci->i_ceph_lock);
564 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700565 dout(" marking %p complete and ordered\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800566 /* use i_size to track number of entries in
567 * readdir cache */
568 BUG_ON(fi->readdir_cache_idx < 0);
569 i_size_write(inode, fi->readdir_cache_idx *
570 sizeof(struct dentry*));
571 } else {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700572 dout(" marking %p complete\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800573 }
Yan, Zheng70db4f32014-10-21 18:09:56 -0700574 __ceph_dir_set_complete(ci, fi->dir_release_count,
575 fi->dir_ordered_count);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800576 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700577 }
Sage Weil2817b002009-10-06 11:31:08 -0700578
Al Viro77acfa22013-05-17 16:52:26 -0400579 dout("readdir %p file %p done.\n", inode, file);
Sage Weil2817b002009-10-06 11:31:08 -0700580 return 0;
581}
582
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800583static void reset_readdir(struct ceph_file_info *fi)
Sage Weil2817b002009-10-06 11:31:08 -0700584{
585 if (fi->last_readdir) {
586 ceph_mdsc_put_request(fi->last_readdir);
587 fi->last_readdir = NULL;
588 }
589 kfree(fi->last_name);
Sage Weila1629c32010-11-11 15:24:06 -0800590 fi->last_name = NULL;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800591 fi->dir_release_count = 0;
592 fi->readdir_cache_idx = -1;
Yan, Zhenga78600e2016-04-27 17:32:34 +0800593 fi->next_offset = 2; /* compensate for . and .. */
Sage Weil9cfa1092011-07-26 11:26:18 -0700594 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700595}
596
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800597/*
598 * discard buffered readdir content on seekdir(0), or seek to new frag,
599 * or seek prior to current chunk
600 */
601static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
602{
603 struct ceph_mds_reply_info_parsed *rinfo;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800604 loff_t chunk_offset;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800605 if (new_pos == 0)
606 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800607 if (is_hash_order(new_pos)) {
608 /* no need to reset last_name for a forward seek when
609 * dentries are sotred in hash order */
Nicolas Iooss0f5aa882016-08-28 18:47:12 +0200610 } else if (fi->frag != fpos_frag(new_pos)) {
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800611 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800612 }
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800613 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
614 if (!rinfo || !rinfo->dir_nr)
615 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800616 chunk_offset = rinfo->dir_entries[0].offset;
617 return new_pos < chunk_offset ||
618 is_hash_order(new_pos) != is_hash_order(chunk_offset);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800619}
620
Andrew Morton965c8e52012-12-17 15:59:39 -0800621static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
Sage Weil2817b002009-10-06 11:31:08 -0700622{
623 struct ceph_file_info *fi = file->private_data;
624 struct inode *inode = file->f_mapping->host;
Sage Weil2817b002009-10-06 11:31:08 -0700625 loff_t retval;
626
Al Viro59551022016-01-22 15:40:57 -0500627 inode_lock(inode);
Josef Bacik06222e42011-07-18 13:21:38 -0400628 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800629 switch (whence) {
Sage Weil2817b002009-10-06 11:31:08 -0700630 case SEEK_CUR:
631 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -0400632 case SEEK_SET:
633 break;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800634 case SEEK_END:
635 retval = -EOPNOTSUPP;
Josef Bacik06222e42011-07-18 13:21:38 -0400636 default:
637 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700638 }
Josef Bacik06222e42011-07-18 13:21:38 -0400639
Yan, Zhengf0494202014-02-27 16:26:24 +0800640 if (offset >= 0) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800641 if (need_reset_readdir(fi, offset)) {
642 dout("dir_llseek dropping %p content\n", file);
643 reset_readdir(fi);
644 } else if (is_hash_order(offset) && offset > file->f_pos) {
645 /* for hash offset, we don't know if a forward seek
646 * is within same frag */
647 fi->dir_release_count = 0;
648 fi->readdir_cache_idx = -1;
649 }
650
Sage Weil2817b002009-10-06 11:31:08 -0700651 if (offset != file->f_pos) {
652 file->f_pos = offset;
653 file->f_version = 0;
Sage Weil9cfa1092011-07-26 11:26:18 -0700654 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700655 }
656 retval = offset;
Sage Weil2817b002009-10-06 11:31:08 -0700657 }
Josef Bacik06222e42011-07-18 13:21:38 -0400658out:
Al Viro59551022016-01-22 15:40:57 -0500659 inode_unlock(inode);
Sage Weil2817b002009-10-06 11:31:08 -0700660 return retval;
661}
662
663/*
Sage Weil468640e2011-07-26 11:28:11 -0700664 * Handle lookups for the hidden .snap directory.
Sage Weil2817b002009-10-06 11:31:08 -0700665 */
Sage Weil468640e2011-07-26 11:28:11 -0700666int ceph_handle_snapdir(struct ceph_mds_request *req,
667 struct dentry *dentry, int err)
Sage Weil2817b002009-10-06 11:31:08 -0700668{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700669 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000670 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
Sage Weil2817b002009-10-06 11:31:08 -0700671
672 /* .snap dir? */
673 if (err == -ENOENT &&
Sage Weil455cec02011-03-03 13:44:35 -0800674 ceph_snap(parent) == CEPH_NOSNAP &&
Sage Weil6b805182009-10-27 11:50:50 -0700675 strcmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700676 fsc->mount_options->snapdir_name) == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700677 struct inode *inode = ceph_get_snapdir(parent);
Al Viroa4555892014-10-21 20:11:25 -0400678 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
679 dentry, dentry, inode);
Sage Weil9358c6d2010-03-30 13:54:41 -0700680 BUG_ON(!d_unhashed(dentry));
Sage Weil2817b002009-10-06 11:31:08 -0700681 d_add(dentry, inode);
682 err = 0;
683 }
Sage Weil468640e2011-07-26 11:28:11 -0700684 return err;
685}
Sage Weil2817b002009-10-06 11:31:08 -0700686
Sage Weil468640e2011-07-26 11:28:11 -0700687/*
688 * Figure out final result of a lookup/open request.
689 *
690 * Mainly, make sure we return the final req->r_dentry (if it already
691 * existed) in place of the original VFS-provided dentry when they
692 * differ.
693 *
694 * Gracefully handle the case where the MDS replies with -ENOENT and
695 * no trace (which it may do, at its discretion, e.g., if it doesn't
696 * care to issue a lease on the negative dentry).
697 */
698struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
699 struct dentry *dentry, int err)
700{
Sage Weil2817b002009-10-06 11:31:08 -0700701 if (err == -ENOENT) {
702 /* no trace? */
703 err = 0;
704 if (!req->r_reply_info.head->is_dentry) {
705 dout("ENOENT and no trace, dentry %p inode %p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000706 dentry, d_inode(dentry));
707 if (d_really_is_positive(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700708 d_drop(dentry);
709 err = -ENOENT;
710 } else {
711 d_add(dentry, NULL);
712 }
713 }
714 }
715 if (err)
716 dentry = ERR_PTR(err);
717 else if (dentry != req->r_dentry)
718 dentry = dget(req->r_dentry); /* we got spliced */
719 else
720 dentry = NULL;
721 return dentry;
722}
723
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -0400724static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
Sage Weil1d1de9162009-12-02 11:54:25 -0800725{
726 return ceph_ino(inode) == CEPH_INO_ROOT &&
727 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
728}
729
Sage Weil2817b002009-10-06 11:31:08 -0700730/*
731 * Look up a single dir entry. If there is a lookup intent, inform
732 * the MDS so that it gets our 'caps wanted' value in a single op.
733 */
734static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400735 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -0700736{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700737 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
738 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700739 struct ceph_mds_request *req;
740 int op;
Yan, Zheng315f2402016-03-07 10:34:50 +0800741 int mask;
Sage Weil2817b002009-10-06 11:31:08 -0700742 int err;
743
Al Viroa4555892014-10-21 20:11:25 -0400744 dout("lookup %p dentry %p '%pd'\n",
745 dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700746
747 if (dentry->d_name.len > NAME_MAX)
748 return ERR_PTR(-ENAMETOOLONG);
749
750 err = ceph_init_dentry(dentry);
751 if (err < 0)
752 return ERR_PTR(err);
753
Sage Weil2817b002009-10-06 11:31:08 -0700754 /* can we conclude ENOENT locally? */
David Howells2b0143b2015-03-17 22:25:59 +0000755 if (d_really_is_negative(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700756 struct ceph_inode_info *ci = ceph_inode(dir);
757 struct ceph_dentry_info *di = ceph_dentry(dentry);
758
Sage Weilbe655592011-11-30 09:47:09 -0800759 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700760 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
761 if (strncmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700762 fsc->mount_options->snapdir_name,
Sage Weil2817b002009-10-06 11:31:08 -0700763 dentry->d_name.len) &&
Sage Weil1d1de9162009-12-02 11:54:25 -0800764 !is_root_ceph_dentry(dir, dentry) &&
Yan, Zhenge2c3de02015-03-04 16:05:04 +0800765 ceph_test_mount_opt(fsc, DCACHE) &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800766 __ceph_dir_is_complete(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700767 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800768 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700769 dout(" dir %p complete, -ENOENT\n", dir);
770 d_add(dentry, NULL);
771 di->lease_shared_gen = ci->i_shared_gen;
772 return NULL;
773 }
Sage Weilbe655592011-11-30 09:47:09 -0800774 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700775 }
776
777 op = ceph_snap(dir) == CEPH_SNAPDIR ?
778 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
779 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
780 if (IS_ERR(req))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200781 return ERR_CAST(req);
Sage Weil2817b002009-10-06 11:31:08 -0700782 req->r_dentry = dget(dentry);
783 req->r_num_caps = 2;
Yan, Zheng315f2402016-03-07 10:34:50 +0800784
785 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
786 if (ceph_security_xattr_wanted(dir))
787 mask |= CEPH_CAP_XATTR_SHARED;
788 req->r_args.getattr.mask = cpu_to_le32(mask);
789
Sage Weil2817b002009-10-06 11:31:08 -0700790 req->r_locked_dir = dir;
791 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil468640e2011-07-26 11:28:11 -0700792 err = ceph_handle_snapdir(req, dentry, err);
Sage Weil2817b002009-10-06 11:31:08 -0700793 dentry = ceph_finish_lookup(req, dentry, err);
794 ceph_mdsc_put_request(req); /* will dput(dentry) */
795 dout("lookup result=%p\n", dentry);
796 return dentry;
797}
798
799/*
800 * If we do a create but get no trace back from the MDS, follow up with
801 * a lookup (the VFS expects us to link up the provided dentry).
802 */
803int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
804{
Al Viro00cd8dd2012-06-10 17:13:09 -0400805 struct dentry *result = ceph_lookup(dir, dentry, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700806
807 if (result && !IS_ERR(result)) {
808 /*
809 * We created the item, then did a lookup, and found
810 * it was already linked to another inode we already
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800811 * had in our cache (and thus got spliced). To not
812 * confuse VFS (especially when inode is a directory),
813 * we don't link our dentry to that inode, return an
814 * error instead.
815 *
816 * This event should be rare and it happens only when
817 * we talk to old MDS. Recent MDS does not send traceless
818 * reply for request that creates new inode.
Sage Weil2817b002009-10-06 11:31:08 -0700819 */
Yan, Zheng5cba3722015-02-02 11:27:56 +0800820 d_drop(result);
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800821 return -ESTALE;
Sage Weil2817b002009-10-06 11:31:08 -0700822 }
823 return PTR_ERR(result);
824}
825
826static int ceph_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400827 umode_t mode, dev_t rdev)
Sage Weil2817b002009-10-06 11:31:08 -0700828{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700829 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
830 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700831 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800832 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700833 int err;
834
835 if (ceph_snap(dir) != CEPH_NOSNAP)
836 return -EROFS;
837
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800838 err = ceph_pre_init_acls(dir, &mode, &acls);
839 if (err < 0)
840 return err;
841
Al Viro1a67aaf2011-07-26 01:52:52 -0400842 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
Sage Weil2817b002009-10-06 11:31:08 -0700843 dir, dentry, mode, rdev);
844 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
845 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800846 err = PTR_ERR(req);
847 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700848 }
849 req->r_dentry = dget(dentry);
850 req->r_num_caps = 2;
851 req->r_locked_dir = dir;
852 req->r_args.mknod.mode = cpu_to_le32(mode);
853 req->r_args.mknod.rdev = cpu_to_le32(rdev);
854 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
855 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800856 if (acls.pagelist) {
857 req->r_pagelist = acls.pagelist;
858 acls.pagelist = NULL;
859 }
Sage Weil2817b002009-10-06 11:31:08 -0700860 err = ceph_mdsc_do_request(mdsc, dir, req);
861 if (!err && !req->r_reply_info.head->is_dentry)
862 err = ceph_handle_notrace_create(dir, dentry);
863 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800864out:
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800865 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000866 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800867 else
Sage Weil2817b002009-10-06 11:31:08 -0700868 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800869 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700870 return err;
871}
872
Al Viro4acdaf22011-07-26 01:42:34 -0400873static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400874 bool excl)
Sage Weil2817b002009-10-06 11:31:08 -0700875{
Miklos Szeredi2d83bde2012-06-05 15:10:25 +0200876 return ceph_mknod(dir, dentry, mode, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700877}
878
879static int ceph_symlink(struct inode *dir, struct dentry *dentry,
880 const char *dest)
881{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700882 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
883 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700884 struct ceph_mds_request *req;
885 int err;
886
887 if (ceph_snap(dir) != CEPH_NOSNAP)
888 return -EROFS;
889
890 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
891 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
892 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800893 err = PTR_ERR(req);
894 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700895 }
Yan, Zheng687265e2015-06-13 17:27:05 +0800896 req->r_path2 = kstrdup(dest, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400897 if (!req->r_path2) {
898 err = -ENOMEM;
899 ceph_mdsc_put_request(req);
900 goto out;
901 }
902 req->r_locked_dir = dir;
Sage Weil2817b002009-10-06 11:31:08 -0700903 req->r_dentry = dget(dentry);
904 req->r_num_caps = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700905 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
906 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
907 err = ceph_mdsc_do_request(mdsc, dir, req);
908 if (!err && !req->r_reply_info.head->is_dentry)
909 err = ceph_handle_notrace_create(dir, dentry);
910 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800911out:
912 if (err)
Sage Weil2817b002009-10-06 11:31:08 -0700913 d_drop(dentry);
914 return err;
915}
916
Al Viro18bb1db2011-07-26 01:41:39 -0400917static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Sage Weil2817b002009-10-06 11:31:08 -0700918{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700919 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
920 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700921 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800922 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700923 int err = -EROFS;
924 int op;
925
926 if (ceph_snap(dir) == CEPH_SNAPDIR) {
927 /* mkdir .snap/foo is a MKSNAP */
928 op = CEPH_MDS_OP_MKSNAP;
Al Viroa4555892014-10-21 20:11:25 -0400929 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
930 dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700931 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
Al Viro18bb1db2011-07-26 01:41:39 -0400932 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
Sage Weil2817b002009-10-06 11:31:08 -0700933 op = CEPH_MDS_OP_MKDIR;
934 } else {
935 goto out;
936 }
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800937
938 mode |= S_IFDIR;
939 err = ceph_pre_init_acls(dir, &mode, &acls);
940 if (err < 0)
941 goto out;
942
Sage Weil2817b002009-10-06 11:31:08 -0700943 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
944 if (IS_ERR(req)) {
945 err = PTR_ERR(req);
946 goto out;
947 }
948
949 req->r_dentry = dget(dentry);
950 req->r_num_caps = 2;
951 req->r_locked_dir = dir;
952 req->r_args.mkdir.mode = cpu_to_le32(mode);
953 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
954 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800955 if (acls.pagelist) {
956 req->r_pagelist = acls.pagelist;
957 acls.pagelist = NULL;
958 }
Sage Weil2817b002009-10-06 11:31:08 -0700959 err = ceph_mdsc_do_request(mdsc, dir, req);
Yan, Zheng275dd192014-12-10 16:17:31 +0800960 if (!err &&
961 !req->r_reply_info.head->is_target &&
962 !req->r_reply_info.head->is_dentry)
Sage Weil2817b002009-10-06 11:31:08 -0700963 err = ceph_handle_notrace_create(dir, dentry);
964 ceph_mdsc_put_request(req);
965out:
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800966 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000967 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800968 else
Sage Weil2817b002009-10-06 11:31:08 -0700969 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800970 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700971 return err;
972}
973
974static int ceph_link(struct dentry *old_dentry, struct inode *dir,
975 struct dentry *dentry)
976{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700977 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
978 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700979 struct ceph_mds_request *req;
980 int err;
981
982 if (ceph_snap(dir) != CEPH_NOSNAP)
983 return -EROFS;
984
985 dout("link in dir %p old_dentry %p dentry %p\n", dir,
986 old_dentry, dentry);
987 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
988 if (IS_ERR(req)) {
989 d_drop(dentry);
990 return PTR_ERR(req);
991 }
992 req->r_dentry = dget(dentry);
993 req->r_num_caps = 2;
Sage Weil4b58c9b2013-02-05 13:41:23 -0800994 req->r_old_dentry = dget(old_dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700995 req->r_locked_dir = dir;
996 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
997 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengad88f232013-07-21 20:25:25 +0800998 /* release LINK_SHARED on source inode (mds will lock it) */
999 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
Sage Weil2817b002009-10-06 11:31:08 -07001000 err = ceph_mdsc_do_request(mdsc, dir, req);
Sage Weil70b666c2011-05-27 09:24:26 -07001001 if (err) {
Sage Weil2817b002009-10-06 11:31:08 -07001002 d_drop(dentry);
Sage Weil70b666c2011-05-27 09:24:26 -07001003 } else if (!req->r_reply_info.head->is_dentry) {
David Howells2b0143b2015-03-17 22:25:59 +00001004 ihold(d_inode(old_dentry));
1005 d_instantiate(dentry, d_inode(old_dentry));
Sage Weil70b666c2011-05-27 09:24:26 -07001006 }
Sage Weil2817b002009-10-06 11:31:08 -07001007 ceph_mdsc_put_request(req);
1008 return err;
1009}
1010
1011/*
1012 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1013 * looks like the link count will hit 0, drop any other caps (other
1014 * than PIN) we don't specifically want (due to the file still being
1015 * open).
1016 */
1017static int drop_caps_for_unlink(struct inode *inode)
1018{
1019 struct ceph_inode_info *ci = ceph_inode(inode);
1020 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1021
Sage Weilbe655592011-11-30 09:47:09 -08001022 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001023 if (inode->i_nlink == 1) {
1024 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1025 ci->i_ceph_flags |= CEPH_I_NODELAY;
1026 }
Sage Weilbe655592011-11-30 09:47:09 -08001027 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001028 return drop;
1029}
1030
1031/*
1032 * rmdir and unlink are differ only by the metadata op code
1033 */
1034static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1035{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001036 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1037 struct ceph_mds_client *mdsc = fsc->mdsc;
David Howells2b0143b2015-03-17 22:25:59 +00001038 struct inode *inode = d_inode(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001039 struct ceph_mds_request *req;
1040 int err = -EROFS;
1041 int op;
1042
1043 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1044 /* rmdir .snap/foo is RMSNAP */
Al Viroa4555892014-10-21 20:11:25 -04001045 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001046 op = CEPH_MDS_OP_RMSNAP;
1047 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1048 dout("unlink/rmdir dir %p dn %p inode %p\n",
1049 dir, dentry, inode);
David Howellse36cb0b2015-01-29 12:02:35 +00001050 op = d_is_dir(dentry) ?
Sage Weil2817b002009-10-06 11:31:08 -07001051 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1052 } else
1053 goto out;
1054 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1055 if (IS_ERR(req)) {
1056 err = PTR_ERR(req);
1057 goto out;
1058 }
1059 req->r_dentry = dget(dentry);
1060 req->r_num_caps = 2;
1061 req->r_locked_dir = dir;
1062 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1063 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1064 req->r_inode_drop = drop_caps_for_unlink(inode);
1065 err = ceph_mdsc_do_request(mdsc, dir, req);
1066 if (!err && !req->r_reply_info.head->is_dentry)
1067 d_delete(dentry);
1068 ceph_mdsc_put_request(req);
1069out:
1070 return err;
1071}
1072
1073static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001074 struct inode *new_dir, struct dentry *new_dentry,
1075 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001076{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001077 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1078 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001079 struct ceph_mds_request *req;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001080 int op = CEPH_MDS_OP_RENAME;
Sage Weil2817b002009-10-06 11:31:08 -07001081 int err;
1082
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001083 if (flags)
1084 return -EINVAL;
1085
Sage Weil2817b002009-10-06 11:31:08 -07001086 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1087 return -EXDEV;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001088 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1089 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1090 op = CEPH_MDS_OP_RENAMESNAP;
1091 else
1092 return -EROFS;
1093 }
Sage Weil2817b002009-10-06 11:31:08 -07001094 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1095 old_dir, old_dentry, new_dir, new_dentry);
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001096 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Sage Weil2817b002009-10-06 11:31:08 -07001097 if (IS_ERR(req))
1098 return PTR_ERR(req);
Sage Weil180061a2013-02-05 13:36:05 -08001099 ihold(old_dir);
Sage Weil2817b002009-10-06 11:31:08 -07001100 req->r_dentry = dget(new_dentry);
1101 req->r_num_caps = 2;
1102 req->r_old_dentry = dget(old_dentry);
Sage Weil180061a2013-02-05 13:36:05 -08001103 req->r_old_dentry_dir = old_dir;
Sage Weil2817b002009-10-06 11:31:08 -07001104 req->r_locked_dir = new_dir;
1105 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1106 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1107 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1108 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1109 /* release LINK_RDCACHE on source inode (mds will lock it) */
1110 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
David Howells2b0143b2015-03-17 22:25:59 +00001111 if (d_really_is_positive(new_dentry))
1112 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
Sage Weil2817b002009-10-06 11:31:08 -07001113 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1114 if (!err && !req->r_reply_info.head->is_dentry) {
1115 /*
1116 * Normally d_move() is done by fill_trace (called by
1117 * do_request, above). If there is no trace, we need
1118 * to do it here.
1119 */
Sage Weilea1409f2010-04-28 16:12:06 -07001120
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001121 /* d_move screws up sibling dentries' offsets */
1122 ceph_dir_clear_complete(old_dir);
1123 ceph_dir_clear_complete(new_dir);
1124
Sage Weil2817b002009-10-06 11:31:08 -07001125 d_move(old_dentry, new_dentry);
Sage Weilea1409f2010-04-28 16:12:06 -07001126
1127 /* ensure target dentry is invalidated, despite
1128 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001129 ceph_invalidate_dentry_lease(new_dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001130 }
1131 ceph_mdsc_put_request(req);
1132 return err;
1133}
1134
Sage Weil81a6cf22010-05-14 09:35:38 -07001135/*
1136 * Ensure a dentry lease will no longer revalidate.
1137 */
1138void ceph_invalidate_dentry_lease(struct dentry *dentry)
1139{
1140 spin_lock(&dentry->d_lock);
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02001141 ceph_dentry(dentry)->time = jiffies;
Sage Weil81a6cf22010-05-14 09:35:38 -07001142 ceph_dentry(dentry)->lease_shared_gen = 0;
1143 spin_unlock(&dentry->d_lock);
1144}
Sage Weil2817b002009-10-06 11:31:08 -07001145
1146/*
1147 * Check if dentry lease is valid. If not, delete the lease. Try to
1148 * renew if the least is more than half up.
1149 */
Jeff Layton14fb9c92016-07-01 09:39:20 -04001150static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1151 struct inode *dir)
Sage Weil2817b002009-10-06 11:31:08 -07001152{
1153 struct ceph_dentry_info *di;
1154 struct ceph_mds_session *s;
1155 int valid = 0;
1156 u32 gen;
1157 unsigned long ttl;
1158 struct ceph_mds_session *session = NULL;
Sage Weil2817b002009-10-06 11:31:08 -07001159 u32 seq = 0;
1160
1161 spin_lock(&dentry->d_lock);
1162 di = ceph_dentry(dentry);
Jeff Layton14fb9c92016-07-01 09:39:20 -04001163 if (di && di->lease_session) {
Sage Weil2817b002009-10-06 11:31:08 -07001164 s = di->lease_session;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001165 spin_lock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001166 gen = s->s_cap_gen;
1167 ttl = s->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001168 spin_unlock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001169
1170 if (di->lease_gen == gen &&
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02001171 time_before(jiffies, di->time) &&
Sage Weil2817b002009-10-06 11:31:08 -07001172 time_before(jiffies, ttl)) {
1173 valid = 1;
1174 if (di->lease_renew_after &&
1175 time_after(jiffies, di->lease_renew_after)) {
Jeff Layton14fb9c92016-07-01 09:39:20 -04001176 /*
1177 * We should renew. If we're in RCU walk mode
1178 * though, we can't do that so just return
1179 * -ECHILD.
1180 */
1181 if (flags & LOOKUP_RCU) {
1182 valid = -ECHILD;
1183 } else {
1184 session = ceph_get_mds_session(s);
1185 seq = di->lease_seq;
1186 di->lease_renew_after = 0;
1187 di->lease_renew_from = jiffies;
1188 }
Sage Weil2817b002009-10-06 11:31:08 -07001189 }
Sage Weil2817b002009-10-06 11:31:08 -07001190 }
1191 }
1192 spin_unlock(&dentry->d_lock);
1193
1194 if (session) {
1195 ceph_mdsc_lease_send_msg(session, dir, dentry,
1196 CEPH_MDS_LEASE_RENEW, seq);
1197 ceph_put_mds_session(session);
1198 }
1199 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1200 return valid;
1201}
1202
1203/*
1204 * Check if directory-wide content lease/cap is valid.
1205 */
1206static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1207{
1208 struct ceph_inode_info *ci = ceph_inode(dir);
1209 struct ceph_dentry_info *di = ceph_dentry(dentry);
1210 int valid = 0;
1211
Sage Weilbe655592011-11-30 09:47:09 -08001212 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001213 if (ci->i_shared_gen == di->lease_shared_gen)
1214 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
Sage Weilbe655592011-11-30 09:47:09 -08001215 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001216 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1217 dir, (unsigned)ci->i_shared_gen, dentry,
1218 (unsigned)di->lease_shared_gen, valid);
1219 return valid;
1220}
1221
1222/*
1223 * Check if cached dentry can be trusted.
1224 */
Al Viro0b728e12012-06-10 16:03:43 -04001225static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001226{
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001227 int valid = 0;
Yan, Zheng641235d2016-03-16 16:40:23 +08001228 struct dentry *parent;
Nick Piggin34286d62011-01-07 17:49:57 +11001229 struct inode *dir;
1230
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001231 if (flags & LOOKUP_RCU) {
1232 parent = ACCESS_ONCE(dentry->d_parent);
1233 dir = d_inode_rcu(parent);
1234 if (!dir)
1235 return -ECHILD;
1236 } else {
1237 parent = dget_parent(dentry);
1238 dir = d_inode(parent);
1239 }
Nick Piggin34286d62011-01-07 17:49:57 +11001240
Al Viroa4555892014-10-21 20:11:25 -04001241 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001242 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
Sage Weil2817b002009-10-06 11:31:08 -07001243
1244 /* always trust cached snapped dentries, snapdir dentry */
1245 if (ceph_snap(dir) != CEPH_NOSNAP) {
Al Viroa4555892014-10-21 20:11:25 -04001246 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001247 dentry, d_inode(dentry));
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001248 valid = 1;
David Howells2b0143b2015-03-17 22:25:59 +00001249 } else if (d_really_is_positive(dentry) &&
1250 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001251 valid = 1;
Jeff Layton14fb9c92016-07-01 09:39:20 -04001252 } else {
1253 valid = dentry_lease_is_valid(dentry, flags, dir);
1254 if (valid == -ECHILD)
1255 return valid;
1256 if (valid || dir_lease_is_valid(dir, dentry)) {
1257 if (d_really_is_positive(dentry))
1258 valid = ceph_is_any_caps(d_inode(dentry));
1259 else
1260 valid = 1;
1261 }
Sage Weil2817b002009-10-06 11:31:08 -07001262 }
Sage Weil2817b002009-10-06 11:31:08 -07001263
Yan, Zheng200fd272016-03-17 14:41:59 +08001264 if (!valid) {
1265 struct ceph_mds_client *mdsc =
1266 ceph_sb_to_client(dir->i_sb)->mdsc;
1267 struct ceph_mds_request *req;
Jeff Layton2e4f2132017-01-12 14:42:38 -05001268 int op, err;
1269 u32 mask;
Yan, Zheng200fd272016-03-17 14:41:59 +08001270
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001271 if (flags & LOOKUP_RCU)
1272 return -ECHILD;
1273
Yan, Zheng200fd272016-03-17 14:41:59 +08001274 op = ceph_snap(dir) == CEPH_SNAPDIR ?
Jeff Laytonc3f46882016-11-30 15:56:46 -05001275 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_GETATTR;
Yan, Zheng200fd272016-03-17 14:41:59 +08001276 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1277 if (!IS_ERR(req)) {
1278 req->r_dentry = dget(dentry);
Jeff Laytonc3f46882016-11-30 15:56:46 -05001279 req->r_num_caps = op == CEPH_MDS_OP_GETATTR ? 1 : 2;
Yan, Zheng200fd272016-03-17 14:41:59 +08001280
1281 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1282 if (ceph_security_xattr_wanted(dir))
1283 mask |= CEPH_CAP_XATTR_SHARED;
Jeff Layton2e4f2132017-01-12 14:42:38 -05001284 req->r_args.getattr.mask = cpu_to_le32(mask);
Yan, Zheng200fd272016-03-17 14:41:59 +08001285
Yan, Zheng200fd272016-03-17 14:41:59 +08001286 err = ceph_mdsc_do_request(mdsc, NULL, req);
Jeff Laytonc3f46882016-11-30 15:56:46 -05001287 switch (err) {
1288 case 0:
1289 if (d_really_is_positive(dentry) &&
1290 d_inode(dentry) == req->r_target_inode)
1291 valid = 1;
1292 break;
1293 case -ENOENT:
1294 if (d_really_is_negative(dentry))
1295 valid = 1;
1296 /* Fallthrough */
1297 default:
1298 break;
Yan, Zheng200fd272016-03-17 14:41:59 +08001299 }
1300 ceph_mdsc_put_request(req);
1301 dout("d_revalidate %p lookup result=%d\n",
1302 dentry, err);
1303 }
1304 }
1305
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001306 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
Yan, Zheng9215aee2013-11-30 12:47:41 +08001307 if (valid) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001308 ceph_dentry_lru_touch(dentry);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001309 } else {
1310 ceph_dir_clear_complete(dir);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001311 }
Yan, Zheng641235d2016-03-16 16:40:23 +08001312
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001313 if (!(flags & LOOKUP_RCU))
1314 dput(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001315 return valid;
Sage Weil2817b002009-10-06 11:31:08 -07001316}
1317
1318/*
Sage Weil147851d2011-03-15 14:57:41 -07001319 * Release our ceph_dentry_info.
Sage Weil2817b002009-10-06 11:31:08 -07001320 */
Sage Weil147851d2011-03-15 14:57:41 -07001321static void ceph_d_release(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -07001322{
1323 struct ceph_dentry_info *di = ceph_dentry(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001324
Sage Weil147851d2011-03-15 14:57:41 -07001325 dout("d_release %p\n", dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001326 ceph_dentry_lru_del(dentry);
Jeff Layton5b484a52016-07-01 09:39:20 -04001327
1328 spin_lock(&dentry->d_lock);
1329 dentry->d_fsdata = NULL;
1330 spin_unlock(&dentry->d_lock);
1331
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001332 if (di->lease_session)
1333 ceph_put_mds_session(di->lease_session);
1334 kmem_cache_free(ceph_dentry_cachep, di);
Sage Weil2817b002009-10-06 11:31:08 -07001335}
1336
1337static int ceph_snapdir_d_revalidate(struct dentry *dentry,
Al Viro0b728e12012-06-10 16:03:43 -04001338 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001339{
1340 /*
1341 * Eventually, we'll want to revalidate snapped metadata
1342 * too... probably...
1343 */
1344 return 1;
1345}
1346
Sage Weilb58dc412011-03-15 15:53:40 -07001347/*
1348 * When the VFS prunes a dentry from the cache, we need to clear the
1349 * complete flag on the parent directory.
1350 *
1351 * Called under dentry->d_lock.
1352 */
1353static void ceph_d_prune(struct dentry *dentry)
1354{
Sage Weil774ac212011-11-11 09:48:08 -08001355 dout("ceph_d_prune %p\n", dentry);
Sage Weilb58dc412011-03-15 15:53:40 -07001356
1357 /* do we have a valid parent? */
Sage Weil8842b3b2012-06-07 13:43:35 -07001358 if (IS_ROOT(dentry))
Sage Weilb58dc412011-03-15 15:53:40 -07001359 return;
1360
Yan, Zheng2f276c52013-03-13 19:44:32 +08001361 /* if we are not hashed, we don't affect dir's completeness */
Sage Weilb58dc412011-03-15 15:53:40 -07001362 if (d_unhashed(dentry))
1363 return;
1364
1365 /*
1366 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1367 * cleared until d_release
1368 */
David Howells2b0143b2015-03-17 22:25:59 +00001369 ceph_dir_clear_complete(d_inode(dentry->d_parent));
Sage Weilb58dc412011-03-15 15:53:40 -07001370}
Sage Weil2817b002009-10-06 11:31:08 -07001371
1372/*
1373 * read() on a dir. This weird interface hack only works if mounted
1374 * with '-o dirstat'.
1375 */
1376static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1377 loff_t *ppos)
1378{
1379 struct ceph_file_info *cf = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05001380 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -07001381 struct ceph_inode_info *ci = ceph_inode(inode);
1382 int left;
Sage Weilae598082011-05-12 14:28:05 -07001383 const int bufsize = 1024;
Sage Weil2817b002009-10-06 11:31:08 -07001384
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001385 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
Sage Weil2817b002009-10-06 11:31:08 -07001386 return -EISDIR;
1387
1388 if (!cf->dir_info) {
Yan, Zheng687265e2015-06-13 17:27:05 +08001389 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -07001390 if (!cf->dir_info)
1391 return -ENOMEM;
1392 cf->dir_info_len =
Sage Weilae598082011-05-12 14:28:05 -07001393 snprintf(cf->dir_info, bufsize,
Sage Weil2817b002009-10-06 11:31:08 -07001394 "entries: %20lld\n"
1395 " files: %20lld\n"
1396 " subdirs: %20lld\n"
1397 "rentries: %20lld\n"
1398 " rfiles: %20lld\n"
1399 " rsubdirs: %20lld\n"
1400 "rbytes: %20lld\n"
1401 "rctime: %10ld.%09ld\n",
1402 ci->i_files + ci->i_subdirs,
1403 ci->i_files,
1404 ci->i_subdirs,
1405 ci->i_rfiles + ci->i_rsubdirs,
1406 ci->i_rfiles,
1407 ci->i_rsubdirs,
1408 ci->i_rbytes,
1409 (long)ci->i_rctime.tv_sec,
1410 (long)ci->i_rctime.tv_nsec);
1411 }
1412
1413 if (*ppos >= cf->dir_info_len)
1414 return 0;
1415 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1416 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1417 if (left == size)
1418 return -EFAULT;
1419 *ppos += (size - left);
1420 return size - left;
1421}
1422
1423/*
Sage Weil2817b002009-10-06 11:31:08 -07001424 * We maintain a private dentry LRU.
1425 *
1426 * FIXME: this needs to be changed to a per-mds lru to be useful.
1427 */
1428void ceph_dentry_lru_add(struct dentry *dn)
1429{
1430 struct ceph_dentry_info *di = ceph_dentry(dn);
1431 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001432
Al Viroa4555892014-10-21 20:11:25 -04001433 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001434 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1435 spin_lock(&mdsc->dentry_lru_lock);
1436 list_add_tail(&di->lru, &mdsc->dentry_lru);
1437 mdsc->num_dentry++;
1438 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001439}
1440
1441void ceph_dentry_lru_touch(struct dentry *dn)
1442{
1443 struct ceph_dentry_info *di = ceph_dentry(dn);
1444 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001445
Al Viroa4555892014-10-21 20:11:25 -04001446 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1447 di->offset);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001448 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1449 spin_lock(&mdsc->dentry_lru_lock);
1450 list_move_tail(&di->lru, &mdsc->dentry_lru);
1451 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001452}
1453
1454void ceph_dentry_lru_del(struct dentry *dn)
1455{
1456 struct ceph_dentry_info *di = ceph_dentry(dn);
1457 struct ceph_mds_client *mdsc;
1458
Al Viroa4555892014-10-21 20:11:25 -04001459 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001460 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1461 spin_lock(&mdsc->dentry_lru_lock);
1462 list_del_init(&di->lru);
1463 mdsc->num_dentry--;
1464 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001465}
1466
Sage Weil6c0f3af2010-11-16 11:14:34 -08001467/*
1468 * Return name hash for a given dentry. This is dependent on
1469 * the parent directory's hash function.
1470 */
Sage Weile5f86dc2011-07-26 11:30:55 -07001471unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
Sage Weil6c0f3af2010-11-16 11:14:34 -08001472{
Sage Weil6c0f3af2010-11-16 11:14:34 -08001473 struct ceph_inode_info *dci = ceph_inode(dir);
Jeff Laytona9adfd92019-04-17 12:58:28 -04001474 unsigned hash;
Sage Weil6c0f3af2010-11-16 11:14:34 -08001475
1476 switch (dci->i_dir_layout.dl_dir_hash) {
1477 case 0: /* for backward compat */
1478 case CEPH_STR_HASH_LINUX:
1479 return dn->d_name.hash;
1480
1481 default:
Jeff Laytona9adfd92019-04-17 12:58:28 -04001482 spin_lock(&dn->d_lock);
1483 hash = ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
Sage Weil6c0f3af2010-11-16 11:14:34 -08001484 dn->d_name.name, dn->d_name.len);
Jeff Laytona9adfd92019-04-17 12:58:28 -04001485 spin_unlock(&dn->d_lock);
1486 return hash;
Sage Weil6c0f3af2010-11-16 11:14:34 -08001487 }
1488}
1489
Sage Weil2817b002009-10-06 11:31:08 -07001490const struct file_operations ceph_dir_fops = {
1491 .read = ceph_read_dir,
Al Viro77acfa22013-05-17 16:52:26 -04001492 .iterate = ceph_readdir,
Sage Weil2817b002009-10-06 11:31:08 -07001493 .llseek = ceph_dir_llseek,
1494 .open = ceph_open,
1495 .release = ceph_release,
1496 .unlocked_ioctl = ceph_ioctl,
Yan, Zhengda819c82015-05-27 11:19:34 +08001497 .fsync = ceph_fsync,
Sage Weil2817b002009-10-06 11:31:08 -07001498};
1499
Yan, Zheng38c48b52015-01-14 13:46:04 +08001500const struct file_operations ceph_snapdir_fops = {
1501 .iterate = ceph_readdir,
1502 .llseek = ceph_dir_llseek,
1503 .open = ceph_open,
1504 .release = ceph_release,
1505};
1506
Sage Weil2817b002009-10-06 11:31:08 -07001507const struct inode_operations ceph_dir_iops = {
1508 .lookup = ceph_lookup,
1509 .permission = ceph_permission,
1510 .getattr = ceph_getattr,
1511 .setattr = ceph_setattr,
Sage Weil2817b002009-10-06 11:31:08 -07001512 .listxattr = ceph_listxattr,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001513 .get_acl = ceph_get_acl,
Sage Weil72466d02014-01-29 06:22:25 -08001514 .set_acl = ceph_set_acl,
Sage Weil2817b002009-10-06 11:31:08 -07001515 .mknod = ceph_mknod,
1516 .symlink = ceph_symlink,
1517 .mkdir = ceph_mkdir,
1518 .link = ceph_link,
1519 .unlink = ceph_unlink,
1520 .rmdir = ceph_unlink,
1521 .rename = ceph_rename,
1522 .create = ceph_create,
Miklos Szeredi2d83bde2012-06-05 15:10:25 +02001523 .atomic_open = ceph_atomic_open,
Sage Weil2817b002009-10-06 11:31:08 -07001524};
1525
Yan, Zheng38c48b52015-01-14 13:46:04 +08001526const struct inode_operations ceph_snapdir_iops = {
1527 .lookup = ceph_lookup,
1528 .permission = ceph_permission,
1529 .getattr = ceph_getattr,
1530 .mkdir = ceph_mkdir,
1531 .rmdir = ceph_unlink,
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001532 .rename = ceph_rename,
Yan, Zheng38c48b52015-01-14 13:46:04 +08001533};
1534
Sage Weil52dfb8a2010-08-03 10:25:30 -07001535const struct dentry_operations ceph_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001536 .d_revalidate = ceph_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001537 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001538 .d_prune = ceph_d_prune,
Sage Weil2817b002009-10-06 11:31:08 -07001539};
1540
Sage Weil52dfb8a2010-08-03 10:25:30 -07001541const struct dentry_operations ceph_snapdir_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001542 .d_revalidate = ceph_snapdir_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001543 .d_release = ceph_d_release,
Sage Weil2817b002009-10-06 11:31:08 -07001544};
1545
Sage Weil52dfb8a2010-08-03 10:25:30 -07001546const struct dentry_operations ceph_snap_dentry_ops = {
Sage Weil147851d2011-03-15 14:57:41 -07001547 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001548 .d_prune = ceph_d_prune,
Sage Weil2817b002009-10-06 11:31:08 -07001549};