blob: f6279a1bd6ec0f808d63a7dbdec8243ed379347d [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>
8
9#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070010#include "mds_client.h"
Sage Weil2817b002009-10-06 11:31:08 -070011
12/*
13 * Directory operations: readdir, lookup, create, link, unlink,
14 * rename, etc.
15 */
16
17/*
18 * Ceph MDS operations are specified in terms of a base ino and
19 * relative path. Thus, the client can specify an operation on a
20 * specific inode (e.g., a getattr due to fstat(2)), or as a path
21 * relative to, say, the root directory.
22 *
23 * Normally, we limit ourselves to strict inode ops (no path component)
24 * or dentry operations (a single path component relative to an ino). The
25 * exception to this is open_root_dentry(), which will open the mount
26 * point by name.
27 */
28
Sage Weil52dfb8a2010-08-03 10:25:30 -070029const struct dentry_operations ceph_dentry_ops;
Sage Weil2817b002009-10-06 11:31:08 -070030
31/*
32 * Initialize ceph dentry state.
33 */
34int ceph_init_dentry(struct dentry *dentry)
35{
36 struct ceph_dentry_info *di;
37
38 if (dentry->d_fsdata)
39 return 0;
40
Geliang Tang99ec2692016-03-13 15:26:29 +080041 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -070042 if (!di)
43 return -ENOMEM; /* oh well */
44
45 spin_lock(&dentry->d_lock);
Sage Weil8c6efb52010-04-23 11:36:54 -070046 if (dentry->d_fsdata) {
47 /* lost a race */
48 kmem_cache_free(ceph_dentry_cachep, di);
Sage Weil2817b002009-10-06 11:31:08 -070049 goto out_unlock;
Sage Weil8c6efb52010-04-23 11:36:54 -070050 }
Sage Weil48d0cbd2011-07-26 11:30:15 -070051
David Howells2b0143b2015-03-17 22:25:59 +000052 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_NOSNAP)
Sage Weil48d0cbd2011-07-26 11:30:15 -070053 d_set_d_op(dentry, &ceph_dentry_ops);
David Howells2b0143b2015-03-17 22:25:59 +000054 else if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
Sage Weil48d0cbd2011-07-26 11:30:15 -070055 d_set_d_op(dentry, &ceph_snapdir_dentry_ops);
56 else
57 d_set_d_op(dentry, &ceph_snap_dentry_ops);
58
Sage Weil2817b002009-10-06 11:31:08 -070059 di->dentry = dentry;
60 di->lease_session = NULL;
Sage Weil2817b002009-10-06 11:31:08 -070061 dentry->d_time = jiffies;
Sage Weil48d0cbd2011-07-26 11:30:15 -070062 /* avoid reordering d_fsdata setup so that the check above is safe */
63 smp_mb();
64 dentry->d_fsdata = di;
Sage Weil2817b002009-10-06 11:31:08 -070065 ceph_dentry_lru_add(dentry);
66out_unlock:
67 spin_unlock(&dentry->d_lock);
68 return 0;
69}
70
Sage Weil2817b002009-10-06 11:31:08 -070071/*
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080072 * for f_pos for readdir:
73 * - hash order:
74 * (0xff << 52) | ((24 bits hash) << 28) |
75 * (the nth entry has hash collision);
76 * - frag+name order;
77 * ((frag value) << 28) | (the nth entry in frag);
Sage Weil2817b002009-10-06 11:31:08 -070078 */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080079#define OFFSET_BITS 28
80#define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
81#define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
82loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
83{
84 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
85 if (hash_order)
86 fpos |= HASH_ORDER;
87 return fpos;
88}
89
90static bool is_hash_order(loff_t p)
91{
92 return (p & HASH_ORDER) == HASH_ORDER;
93}
94
Sage Weil2817b002009-10-06 11:31:08 -070095static unsigned fpos_frag(loff_t p)
96{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080097 return p >> OFFSET_BITS;
Sage Weil2817b002009-10-06 11:31:08 -070098}
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080099
100static unsigned fpos_hash(loff_t p)
101{
102 return ceph_frag_value(fpos_frag(p));
103}
104
Sage Weil2817b002009-10-06 11:31:08 -0700105static unsigned fpos_off(loff_t p)
106{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800107 return p & OFFSET_MASK;
Sage Weil2817b002009-10-06 11:31:08 -0700108}
109
Yan, Zheng4d5f5df2014-02-13 19:40:26 +0800110static int fpos_cmp(loff_t l, loff_t r)
111{
112 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
113 if (v)
114 return v;
115 return (int)(fpos_off(l) - fpos_off(r));
116}
117
Sage Weil2817b002009-10-06 11:31:08 -0700118/*
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800119 * make note of the last dentry we read, so we can
120 * continue at the same lexicographical point,
121 * regardless of what dir changes take place on the
122 * server.
123 */
124static int note_last_dentry(struct ceph_file_info *fi, const char *name,
125 int len, unsigned next_offset)
126{
127 char *buf = kmalloc(len+1, GFP_KERNEL);
128 if (!buf)
129 return -ENOMEM;
130 kfree(fi->last_name);
131 fi->last_name = buf;
132 memcpy(fi->last_name, name, len);
133 fi->last_name[len] = 0;
134 fi->next_offset = next_offset;
135 dout("note_last_dentry '%s'\n", fi->last_name);
136 return 0;
137}
138
Yan, Zhengc530cd22016-04-28 17:43:35 +0800139
140static struct dentry *
141__dcache_find_get_entry(struct dentry *parent, u64 idx,
142 struct ceph_readdir_cache_control *cache_ctl)
143{
144 struct inode *dir = d_inode(parent);
145 struct dentry *dentry;
146 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
147 loff_t ptr_pos = idx * sizeof(struct dentry *);
148 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
149
150 if (ptr_pos >= i_size_read(dir))
151 return NULL;
152
153 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
154 ceph_readdir_cache_release(cache_ctl);
155 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
156 if (!cache_ctl->page) {
157 dout(" page %lu not found\n", ptr_pgoff);
158 return ERR_PTR(-EAGAIN);
159 }
160 /* reading/filling the cache are serialized by
161 i_mutex, no need to use page lock */
162 unlock_page(cache_ctl->page);
163 cache_ctl->dentries = kmap(cache_ctl->page);
164 }
165
166 cache_ctl->index = idx & idx_mask;
167
168 rcu_read_lock();
169 spin_lock(&parent->d_lock);
170 /* check i_size again here, because empty directory can be
171 * marked as complete while not holding the i_mutex. */
172 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
173 dentry = cache_ctl->dentries[cache_ctl->index];
174 else
175 dentry = NULL;
176 spin_unlock(&parent->d_lock);
177 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
178 dentry = NULL;
179 rcu_read_unlock();
180 return dentry ? : ERR_PTR(-EAGAIN);
181}
182
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800183/*
Sage Weil2817b002009-10-06 11:31:08 -0700184 * When possible, we try to satisfy a readdir by peeking at the
185 * dcache. We make this work by carefully ordering dentries on
Al Viro946e51f2014-10-26 19:19:16 -0400186 * d_child when we initially get results back from the MDS, and
Sage Weil2817b002009-10-06 11:31:08 -0700187 * falling back to a "normal" sync readdir if any dentries in the dir
188 * are dropped.
189 *
Yan, Zheng2f276c52013-03-13 19:44:32 +0800190 * Complete dir indicates that we have all dentries in the dir. It is
Sage Weil2817b002009-10-06 11:31:08 -0700191 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
192 * the MDS if/when the directory is modified).
193 */
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800194static int __dcache_readdir(struct file *file, struct dir_context *ctx,
195 u32 shared_gen)
Sage Weil2817b002009-10-06 11:31:08 -0700196{
Al Viro77acfa22013-05-17 16:52:26 -0400197 struct ceph_file_info *fi = file->private_data;
Al Virob5830432014-10-31 01:22:04 -0400198 struct dentry *parent = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000199 struct inode *dir = d_inode(parent);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800200 struct dentry *dentry, *last = NULL;
Sage Weil2817b002009-10-06 11:31:08 -0700201 struct ceph_dentry_info *di;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800202 struct ceph_readdir_cache_control cache_ctl = {};
Yan, Zhengc530cd22016-04-28 17:43:35 +0800203 u64 idx = 0;
204 int err = 0;
Sage Weil2817b002009-10-06 11:31:08 -0700205
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800206 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
Sage Weil2817b002009-10-06 11:31:08 -0700207
Yan, Zhengc530cd22016-04-28 17:43:35 +0800208 /* search start position */
209 if (ctx->pos > 2) {
210 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
211 while (count > 0) {
212 u64 step = count >> 1;
213 dentry = __dcache_find_get_entry(parent, idx + step,
214 &cache_ctl);
215 if (!dentry) {
216 /* use linar search */
217 idx = 0;
218 break;
219 }
220 if (IS_ERR(dentry)) {
221 err = PTR_ERR(dentry);
222 goto out;
223 }
224 di = ceph_dentry(dentry);
225 spin_lock(&dentry->d_lock);
226 if (fpos_cmp(di->offset, ctx->pos) < 0) {
227 idx += step + 1;
228 count -= step + 1;
229 } else {
230 count = step;
231 }
232 spin_unlock(&dentry->d_lock);
233 dput(dentry);
234 }
235
236 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
Sage Weil2817b002009-10-06 11:31:08 -0700237 }
238
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800239
Yan, Zhengc530cd22016-04-28 17:43:35 +0800240 for (;;) {
241 bool emit_dentry = false;
242 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
243 if (!dentry) {
Sage Weil9cfa1092011-07-26 11:26:18 -0700244 fi->flags |= CEPH_F_ATEND;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800245 err = 0;
246 break;
Sage Weil2817b002009-10-06 11:31:08 -0700247 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800248 if (IS_ERR(dentry)) {
249 err = PTR_ERR(dentry);
250 goto out;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800251 }
252
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800253 di = ceph_dentry(dentry);
254 spin_lock(&dentry->d_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800255 if (di->lease_shared_gen == shared_gen &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800256 d_really_is_positive(dentry) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800257 fpos_cmp(ctx->pos, di->offset) <= 0) {
258 emit_dentry = true;
Sage Weil2817b002009-10-06 11:31:08 -0700259 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800260 spin_unlock(&dentry->d_lock);
261
262 if (emit_dentry) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800263 dout(" %llx dentry %p %pd %p\n", di->offset,
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800264 dentry, dentry, d_inode(dentry));
265 ctx->pos = di->offset;
266 if (!dir_emit(ctx, dentry->d_name.name,
267 dentry->d_name.len,
268 ceph_translate_ino(dentry->d_sb,
269 d_inode(dentry)->i_ino),
270 d_inode(dentry)->i_mode >> 12)) {
271 dput(dentry);
272 err = 0;
273 break;
274 }
275 ctx->pos++;
276
277 if (last)
278 dput(last);
279 last = dentry;
280 } else {
281 dput(dentry);
282 }
Sage Weil2817b002009-10-06 11:31:08 -0700283 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800284out:
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800285 ceph_readdir_cache_release(&cache_ctl);
286 if (last) {
287 int ret;
288 di = ceph_dentry(last);
289 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
290 fpos_off(di->offset) + 1);
291 if (ret < 0)
292 err = ret;
Al Viro77acfa22013-05-17 16:52:26 -0400293 dput(last);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800294 }
Sage Weil2817b002009-10-06 11:31:08 -0700295 return err;
296}
297
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800298static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
299{
300 if (!fi->last_readdir)
301 return true;
302 if (is_hash_order(pos))
303 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
304 else
305 return fi->frag != fpos_frag(pos);
306}
307
Al Viro77acfa22013-05-17 16:52:26 -0400308static int ceph_readdir(struct file *file, struct dir_context *ctx)
Sage Weil2817b002009-10-06 11:31:08 -0700309{
Al Viro77acfa22013-05-17 16:52:26 -0400310 struct ceph_file_info *fi = file->private_data;
311 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -0700312 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700313 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
314 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800315 int i;
Sage Weil2817b002009-10-06 11:31:08 -0700316 int err;
317 u32 ftype;
318 struct ceph_mds_reply_info_parsed *rinfo;
Sage Weil2817b002009-10-06 11:31:08 -0700319
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800320 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
Sage Weil9cfa1092011-07-26 11:26:18 -0700321 if (fi->flags & CEPH_F_ATEND)
Sage Weil2817b002009-10-06 11:31:08 -0700322 return 0;
323
324 /* always start with . and .. */
Al Viro77acfa22013-05-17 16:52:26 -0400325 if (ctx->pos == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700326 dout("readdir off 0 -> '.'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400327 if (!dir_emit(ctx, ".", 1,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800328 ceph_translate_ino(inode->i_sb, inode->i_ino),
Al Viro77acfa22013-05-17 16:52:26 -0400329 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700330 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400331 ctx->pos = 1;
Sage Weil2817b002009-10-06 11:31:08 -0700332 }
Al Viro77acfa22013-05-17 16:52:26 -0400333 if (ctx->pos == 1) {
Al Virob5830432014-10-31 01:22:04 -0400334 ino_t ino = parent_ino(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700335 dout("readdir off 1 -> '..'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400336 if (!dir_emit(ctx, "..", 2,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800337 ceph_translate_ino(inode->i_sb, ino),
Al Viro77acfa22013-05-17 16:52:26 -0400338 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700339 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400340 ctx->pos = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700341 }
342
343 /* can we use the dcache? */
Sage Weilbe655592011-11-30 09:47:09 -0800344 spin_lock(&ci->i_ceph_lock);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800345 if (ceph_test_mount_opt(fsc, DCACHE) &&
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700346 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
Sage Weila0dff782010-07-22 13:47:21 -0700347 ceph_snap(inode) != CEPH_SNAPDIR &&
Yan, Zheng70db4f32014-10-21 18:09:56 -0700348 __ceph_dir_is_complete_ordered(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700349 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800350 u32 shared_gen = ci->i_shared_gen;
Sage Weilbe655592011-11-30 09:47:09 -0800351 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800352 err = __dcache_readdir(file, ctx, shared_gen);
Sage Weilefa4c122010-10-18 14:04:31 -0700353 if (err != -EAGAIN)
Sage Weil2817b002009-10-06 11:31:08 -0700354 return err;
Sage Weilefa4c122010-10-18 14:04:31 -0700355 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800356 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700357 }
Sage Weil2817b002009-10-06 11:31:08 -0700358
359 /* proceed with a normal readdir */
Sage Weil2817b002009-10-06 11:31:08 -0700360more:
361 /* do we have the correct frag content buffered? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800362 if (need_send_readdir(fi, ctx->pos)) {
Sage Weil2817b002009-10-06 11:31:08 -0700363 struct ceph_mds_request *req;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800364 unsigned frag;
Sage Weil2817b002009-10-06 11:31:08 -0700365 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
366 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
367
368 /* discard old result, if any */
Sage Weil393f6622010-03-10 12:03:32 -0800369 if (fi->last_readdir) {
Sage Weil2817b002009-10-06 11:31:08 -0700370 ceph_mdsc_put_request(fi->last_readdir);
Sage Weil393f6622010-03-10 12:03:32 -0800371 fi->last_readdir = NULL;
372 }
Sage Weil2817b002009-10-06 11:31:08 -0700373
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800374 if (is_hash_order(ctx->pos)) {
375 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
376 NULL, NULL);
377 } else {
378 frag = fpos_frag(ctx->pos);
379 }
380
Sage Weil2817b002009-10-06 11:31:08 -0700381 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
382 ceph_vinop(inode), frag, fi->last_name);
383 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
384 if (IS_ERR(req))
385 return PTR_ERR(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800386 err = ceph_alloc_readdir_reply_buffer(req, inode);
387 if (err) {
388 ceph_mdsc_put_request(req);
389 return err;
390 }
Sage Weil2817b002009-10-06 11:31:08 -0700391 /* hints to request -> mds selection code */
392 req->r_direct_mode = USE_AUTH_MDS;
393 req->r_direct_hash = ceph_frag_value(frag);
394 req->r_direct_is_hash = true;
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400395 if (fi->last_name) {
Yan, Zheng687265e2015-06-13 17:27:05 +0800396 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400397 if (!req->r_path2) {
398 ceph_mdsc_put_request(req);
399 return -ENOMEM;
400 }
401 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800402 req->r_dir_release_cnt = fi->dir_release_count;
403 req->r_dir_ordered_cnt = fi->dir_ordered_count;
404 req->r_readdir_cache_idx = fi->readdir_cache_idx;
Sage Weil2817b002009-10-06 11:31:08 -0700405 req->r_readdir_offset = fi->next_offset;
406 req->r_args.readdir.frag = cpu_to_le32(frag);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800407 req->r_args.readdir.flags =
408 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400409
410 req->r_inode = inode;
411 ihold(inode);
412 req->r_dentry = dget(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700413 err = ceph_mdsc_do_request(mdsc, NULL, req);
414 if (err < 0) {
415 ceph_mdsc_put_request(req);
416 return err;
417 }
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800418 dout("readdir got and parsed readdir result=%d on "
419 "frag %x, end=%d, complete=%d, hash_order=%d\n",
420 err, frag,
Sage Weil2817b002009-10-06 11:31:08 -0700421 (int)req->r_reply_info.dir_end,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800422 (int)req->r_reply_info.dir_complete,
423 (int)req->r_reply_info.hash_order);
Sage Weil2817b002009-10-06 11:31:08 -0700424
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800425 rinfo = &req->r_reply_info;
426 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
427 frag = le32_to_cpu(rinfo->dir_dir->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800428 if (!rinfo->hash_order) {
429 fi->next_offset = req->r_readdir_offset;
430 /* adjust ctx->pos to beginning of frag */
431 ctx->pos = ceph_make_fpos(frag,
432 fi->next_offset,
433 false);
434 }
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800435 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800436
Yan, Zhengf0494202014-02-27 16:26:24 +0800437 fi->frag = frag;
Sage Weil2817b002009-10-06 11:31:08 -0700438 fi->last_readdir = req;
439
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800440 if (req->r_did_prepopulate) {
441 fi->readdir_cache_idx = req->r_readdir_cache_idx;
442 if (fi->readdir_cache_idx < 0) {
443 /* preclude from marking dir ordered */
444 fi->dir_ordered_count = 0;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800445 } else if (ceph_frag_is_leftmost(frag) &&
446 fi->next_offset == 2) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800447 /* note dir version at start of readdir so
448 * we can tell if any dentries get dropped */
449 fi->dir_release_count = req->r_dir_release_cnt;
450 fi->dir_ordered_count = req->r_dir_ordered_cnt;
451 }
452 } else {
453 dout("readdir !did_prepopulate");
454 /* disable readdir cache */
455 fi->readdir_cache_idx = -1;
456 /* preclude from marking dir complete */
457 fi->dir_release_count = 0;
458 }
459
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800460 /* note next offset and last dentry name */
461 if (rinfo->dir_nr > 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800462 struct ceph_mds_reply_dir_entry *rde =
463 rinfo->dir_entries + (rinfo->dir_nr-1);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800464 unsigned next_offset = req->r_reply_info.dir_end ?
465 2 : (fpos_off(rde->offset) + 1);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800466 err = note_last_dentry(fi, rde->name, rde->name_len,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800467 next_offset);
Sage Weil2817b002009-10-06 11:31:08 -0700468 if (err)
469 return err;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800470 } else if (req->r_reply_info.dir_end) {
471 fi->next_offset = 2;
472 /* keep last name */
Sage Weil2817b002009-10-06 11:31:08 -0700473 }
474 }
475
476 rinfo = &fi->last_readdir->r_reply_info;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800477 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800478 fi->frag, rinfo->dir_nr, ctx->pos,
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800479 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
Al Viro77acfa22013-05-17 16:52:26 -0400480
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800481 i = 0;
482 /* search start position */
483 if (rinfo->dir_nr > 0) {
484 int step, nr = rinfo->dir_nr;
485 while (nr > 0) {
486 step = nr >> 1;
487 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
488 i += step + 1;
489 nr -= step + 1;
490 } else {
491 nr = step;
492 }
493 }
494 }
495 for (; i < rinfo->dir_nr; i++) {
496 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
Sage Weil3105c192010-11-18 09:15:07 -0800497 struct ceph_vino vino;
498 ino_t ino;
499
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800500 BUG_ON(rde->offset < ctx->pos);
501
502 ctx->pos = rde->offset;
503 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
504 i, rinfo->dir_nr, ctx->pos,
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800505 rde->name_len, rde->name, &rde->inode.in);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800506
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800507 BUG_ON(!rde->inode.in);
508 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
509 vino.ino = le64_to_cpu(rde->inode.in->ino);
510 vino.snap = le64_to_cpu(rde->inode.in->snapid);
Sage Weil3105c192010-11-18 09:15:07 -0800511 ino = ceph_vino_to_ino(vino);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800512
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800513 if (!dir_emit(ctx, rde->name, rde->name_len,
514 ceph_translate_ino(inode->i_sb, ino), ftype)) {
Sage Weil2817b002009-10-06 11:31:08 -0700515 dout("filldir stopping us...\n");
516 return 0;
517 }
Al Viro77acfa22013-05-17 16:52:26 -0400518 ctx->pos++;
Sage Weil2817b002009-10-06 11:31:08 -0700519 }
520
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800521 if (fi->next_offset > 2) {
Sage Weil2817b002009-10-06 11:31:08 -0700522 ceph_mdsc_put_request(fi->last_readdir);
523 fi->last_readdir = NULL;
524 goto more;
525 }
526
527 /* more frags? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800528 if (!ceph_frag_is_rightmost(fi->frag)) {
529 unsigned frag = ceph_frag_next(fi->frag);
530 if (is_hash_order(ctx->pos)) {
531 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
532 fi->next_offset, true);
533 if (new_pos > ctx->pos)
534 ctx->pos = new_pos;
535 /* keep last_name */
536 } else {
537 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
538 kfree(fi->last_name);
539 fi->last_name = NULL;
540 }
Sage Weil2817b002009-10-06 11:31:08 -0700541 dout("readdir next frag is %x\n", frag);
542 goto more;
543 }
Sage Weil9cfa1092011-07-26 11:26:18 -0700544 fi->flags |= CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700545
546 /*
547 * if dir_release_count still matches the dir, no dentries
548 * were released during the whole readdir, and we should have
549 * the complete dir contents in our cache.
550 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800551 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
552 spin_lock(&ci->i_ceph_lock);
553 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700554 dout(" marking %p complete and ordered\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800555 /* use i_size to track number of entries in
556 * readdir cache */
557 BUG_ON(fi->readdir_cache_idx < 0);
558 i_size_write(inode, fi->readdir_cache_idx *
559 sizeof(struct dentry*));
560 } else {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700561 dout(" marking %p complete\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800562 }
Yan, Zheng70db4f32014-10-21 18:09:56 -0700563 __ceph_dir_set_complete(ci, fi->dir_release_count,
564 fi->dir_ordered_count);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800565 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700566 }
Sage Weil2817b002009-10-06 11:31:08 -0700567
Al Viro77acfa22013-05-17 16:52:26 -0400568 dout("readdir %p file %p done.\n", inode, file);
Sage Weil2817b002009-10-06 11:31:08 -0700569 return 0;
570}
571
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800572static void reset_readdir(struct ceph_file_info *fi)
Sage Weil2817b002009-10-06 11:31:08 -0700573{
574 if (fi->last_readdir) {
575 ceph_mdsc_put_request(fi->last_readdir);
576 fi->last_readdir = NULL;
577 }
578 kfree(fi->last_name);
Sage Weila1629c32010-11-11 15:24:06 -0800579 fi->last_name = NULL;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800580 fi->dir_release_count = 0;
581 fi->readdir_cache_idx = -1;
Yan, Zhenga78600e2016-04-27 17:32:34 +0800582 fi->next_offset = 2; /* compensate for . and .. */
Sage Weil9cfa1092011-07-26 11:26:18 -0700583 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700584}
585
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800586/*
587 * discard buffered readdir content on seekdir(0), or seek to new frag,
588 * or seek prior to current chunk
589 */
590static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
591{
592 struct ceph_mds_reply_info_parsed *rinfo;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800593 loff_t chunk_offset;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800594 if (new_pos == 0)
595 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800596 if (is_hash_order(new_pos)) {
597 /* no need to reset last_name for a forward seek when
598 * dentries are sotred in hash order */
599 } else if (fi->frag |= fpos_frag(new_pos)) {
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800600 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800601 }
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800602 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
603 if (!rinfo || !rinfo->dir_nr)
604 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800605 chunk_offset = rinfo->dir_entries[0].offset;
606 return new_pos < chunk_offset ||
607 is_hash_order(new_pos) != is_hash_order(chunk_offset);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800608}
609
Andrew Morton965c8e52012-12-17 15:59:39 -0800610static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
Sage Weil2817b002009-10-06 11:31:08 -0700611{
612 struct ceph_file_info *fi = file->private_data;
613 struct inode *inode = file->f_mapping->host;
Sage Weil2817b002009-10-06 11:31:08 -0700614 loff_t retval;
615
Al Viro59551022016-01-22 15:40:57 -0500616 inode_lock(inode);
Josef Bacik06222e42011-07-18 13:21:38 -0400617 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800618 switch (whence) {
Sage Weil2817b002009-10-06 11:31:08 -0700619 case SEEK_CUR:
620 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -0400621 case SEEK_SET:
622 break;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800623 case SEEK_END:
624 retval = -EOPNOTSUPP;
Josef Bacik06222e42011-07-18 13:21:38 -0400625 default:
626 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700627 }
Josef Bacik06222e42011-07-18 13:21:38 -0400628
Yan, Zhengf0494202014-02-27 16:26:24 +0800629 if (offset >= 0) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800630 if (need_reset_readdir(fi, offset)) {
631 dout("dir_llseek dropping %p content\n", file);
632 reset_readdir(fi);
633 } else if (is_hash_order(offset) && offset > file->f_pos) {
634 /* for hash offset, we don't know if a forward seek
635 * is within same frag */
636 fi->dir_release_count = 0;
637 fi->readdir_cache_idx = -1;
638 }
639
Sage Weil2817b002009-10-06 11:31:08 -0700640 if (offset != file->f_pos) {
641 file->f_pos = offset;
642 file->f_version = 0;
Sage Weil9cfa1092011-07-26 11:26:18 -0700643 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700644 }
645 retval = offset;
Sage Weil2817b002009-10-06 11:31:08 -0700646 }
Josef Bacik06222e42011-07-18 13:21:38 -0400647out:
Al Viro59551022016-01-22 15:40:57 -0500648 inode_unlock(inode);
Sage Weil2817b002009-10-06 11:31:08 -0700649 return retval;
650}
651
652/*
Sage Weil468640e2011-07-26 11:28:11 -0700653 * Handle lookups for the hidden .snap directory.
Sage Weil2817b002009-10-06 11:31:08 -0700654 */
Sage Weil468640e2011-07-26 11:28:11 -0700655int ceph_handle_snapdir(struct ceph_mds_request *req,
656 struct dentry *dentry, int err)
Sage Weil2817b002009-10-06 11:31:08 -0700657{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700658 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000659 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
Sage Weil2817b002009-10-06 11:31:08 -0700660
661 /* .snap dir? */
662 if (err == -ENOENT &&
Sage Weil455cec02011-03-03 13:44:35 -0800663 ceph_snap(parent) == CEPH_NOSNAP &&
Sage Weil6b805182009-10-27 11:50:50 -0700664 strcmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700665 fsc->mount_options->snapdir_name) == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700666 struct inode *inode = ceph_get_snapdir(parent);
Al Viroa4555892014-10-21 20:11:25 -0400667 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
668 dentry, dentry, inode);
Sage Weil9358c6d2010-03-30 13:54:41 -0700669 BUG_ON(!d_unhashed(dentry));
Sage Weil2817b002009-10-06 11:31:08 -0700670 d_add(dentry, inode);
671 err = 0;
672 }
Sage Weil468640e2011-07-26 11:28:11 -0700673 return err;
674}
Sage Weil2817b002009-10-06 11:31:08 -0700675
Sage Weil468640e2011-07-26 11:28:11 -0700676/*
677 * Figure out final result of a lookup/open request.
678 *
679 * Mainly, make sure we return the final req->r_dentry (if it already
680 * existed) in place of the original VFS-provided dentry when they
681 * differ.
682 *
683 * Gracefully handle the case where the MDS replies with -ENOENT and
684 * no trace (which it may do, at its discretion, e.g., if it doesn't
685 * care to issue a lease on the negative dentry).
686 */
687struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
688 struct dentry *dentry, int err)
689{
Sage Weil2817b002009-10-06 11:31:08 -0700690 if (err == -ENOENT) {
691 /* no trace? */
692 err = 0;
693 if (!req->r_reply_info.head->is_dentry) {
694 dout("ENOENT and no trace, dentry %p inode %p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000695 dentry, d_inode(dentry));
696 if (d_really_is_positive(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700697 d_drop(dentry);
698 err = -ENOENT;
699 } else {
700 d_add(dentry, NULL);
701 }
702 }
703 }
704 if (err)
705 dentry = ERR_PTR(err);
706 else if (dentry != req->r_dentry)
707 dentry = dget(req->r_dentry); /* we got spliced */
708 else
709 dentry = NULL;
710 return dentry;
711}
712
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -0400713static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
Sage Weil1d1de9162009-12-02 11:54:25 -0800714{
715 return ceph_ino(inode) == CEPH_INO_ROOT &&
716 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
717}
718
Sage Weil2817b002009-10-06 11:31:08 -0700719/*
720 * Look up a single dir entry. If there is a lookup intent, inform
721 * the MDS so that it gets our 'caps wanted' value in a single op.
722 */
723static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400724 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -0700725{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700726 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
727 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700728 struct ceph_mds_request *req;
729 int op;
Yan, Zheng315f2402016-03-07 10:34:50 +0800730 int mask;
Sage Weil2817b002009-10-06 11:31:08 -0700731 int err;
732
Al Viroa4555892014-10-21 20:11:25 -0400733 dout("lookup %p dentry %p '%pd'\n",
734 dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700735
736 if (dentry->d_name.len > NAME_MAX)
737 return ERR_PTR(-ENAMETOOLONG);
738
739 err = ceph_init_dentry(dentry);
740 if (err < 0)
741 return ERR_PTR(err);
742
Sage Weil2817b002009-10-06 11:31:08 -0700743 /* can we conclude ENOENT locally? */
David Howells2b0143b2015-03-17 22:25:59 +0000744 if (d_really_is_negative(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700745 struct ceph_inode_info *ci = ceph_inode(dir);
746 struct ceph_dentry_info *di = ceph_dentry(dentry);
747
Sage Weilbe655592011-11-30 09:47:09 -0800748 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700749 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
750 if (strncmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700751 fsc->mount_options->snapdir_name,
Sage Weil2817b002009-10-06 11:31:08 -0700752 dentry->d_name.len) &&
Sage Weil1d1de9162009-12-02 11:54:25 -0800753 !is_root_ceph_dentry(dir, dentry) &&
Yan, Zhenge2c3de02015-03-04 16:05:04 +0800754 ceph_test_mount_opt(fsc, DCACHE) &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800755 __ceph_dir_is_complete(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700756 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800757 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700758 dout(" dir %p complete, -ENOENT\n", dir);
759 d_add(dentry, NULL);
760 di->lease_shared_gen = ci->i_shared_gen;
761 return NULL;
762 }
Sage Weilbe655592011-11-30 09:47:09 -0800763 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700764 }
765
766 op = ceph_snap(dir) == CEPH_SNAPDIR ?
767 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
768 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
769 if (IS_ERR(req))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200770 return ERR_CAST(req);
Sage Weil2817b002009-10-06 11:31:08 -0700771 req->r_dentry = dget(dentry);
772 req->r_num_caps = 2;
Yan, Zheng315f2402016-03-07 10:34:50 +0800773
774 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
775 if (ceph_security_xattr_wanted(dir))
776 mask |= CEPH_CAP_XATTR_SHARED;
777 req->r_args.getattr.mask = cpu_to_le32(mask);
778
Sage Weil2817b002009-10-06 11:31:08 -0700779 req->r_locked_dir = dir;
780 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil468640e2011-07-26 11:28:11 -0700781 err = ceph_handle_snapdir(req, dentry, err);
Sage Weil2817b002009-10-06 11:31:08 -0700782 dentry = ceph_finish_lookup(req, dentry, err);
783 ceph_mdsc_put_request(req); /* will dput(dentry) */
784 dout("lookup result=%p\n", dentry);
785 return dentry;
786}
787
788/*
789 * If we do a create but get no trace back from the MDS, follow up with
790 * a lookup (the VFS expects us to link up the provided dentry).
791 */
792int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
793{
Al Viro00cd8dd2012-06-10 17:13:09 -0400794 struct dentry *result = ceph_lookup(dir, dentry, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700795
796 if (result && !IS_ERR(result)) {
797 /*
798 * We created the item, then did a lookup, and found
799 * it was already linked to another inode we already
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800800 * had in our cache (and thus got spliced). To not
801 * confuse VFS (especially when inode is a directory),
802 * we don't link our dentry to that inode, return an
803 * error instead.
804 *
805 * This event should be rare and it happens only when
806 * we talk to old MDS. Recent MDS does not send traceless
807 * reply for request that creates new inode.
Sage Weil2817b002009-10-06 11:31:08 -0700808 */
Yan, Zheng5cba3722015-02-02 11:27:56 +0800809 d_drop(result);
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800810 return -ESTALE;
Sage Weil2817b002009-10-06 11:31:08 -0700811 }
812 return PTR_ERR(result);
813}
814
815static int ceph_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400816 umode_t mode, dev_t rdev)
Sage Weil2817b002009-10-06 11:31:08 -0700817{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700818 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
819 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700820 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800821 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700822 int err;
823
824 if (ceph_snap(dir) != CEPH_NOSNAP)
825 return -EROFS;
826
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800827 err = ceph_pre_init_acls(dir, &mode, &acls);
828 if (err < 0)
829 return err;
830
Al Viro1a67aaf2011-07-26 01:52:52 -0400831 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
Sage Weil2817b002009-10-06 11:31:08 -0700832 dir, dentry, mode, rdev);
833 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
834 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800835 err = PTR_ERR(req);
836 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700837 }
838 req->r_dentry = dget(dentry);
839 req->r_num_caps = 2;
840 req->r_locked_dir = dir;
841 req->r_args.mknod.mode = cpu_to_le32(mode);
842 req->r_args.mknod.rdev = cpu_to_le32(rdev);
843 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
844 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800845 if (acls.pagelist) {
846 req->r_pagelist = acls.pagelist;
847 acls.pagelist = NULL;
848 }
Sage Weil2817b002009-10-06 11:31:08 -0700849 err = ceph_mdsc_do_request(mdsc, dir, req);
850 if (!err && !req->r_reply_info.head->is_dentry)
851 err = ceph_handle_notrace_create(dir, dentry);
852 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800853out:
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800854 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000855 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800856 else
Sage Weil2817b002009-10-06 11:31:08 -0700857 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800858 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700859 return err;
860}
861
Al Viro4acdaf22011-07-26 01:42:34 -0400862static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400863 bool excl)
Sage Weil2817b002009-10-06 11:31:08 -0700864{
Miklos Szeredi2d83bde2012-06-05 15:10:25 +0200865 return ceph_mknod(dir, dentry, mode, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700866}
867
868static int ceph_symlink(struct inode *dir, struct dentry *dentry,
869 const char *dest)
870{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700871 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
872 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700873 struct ceph_mds_request *req;
874 int err;
875
876 if (ceph_snap(dir) != CEPH_NOSNAP)
877 return -EROFS;
878
879 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
880 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
881 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800882 err = PTR_ERR(req);
883 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700884 }
Yan, Zheng687265e2015-06-13 17:27:05 +0800885 req->r_path2 = kstrdup(dest, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400886 if (!req->r_path2) {
887 err = -ENOMEM;
888 ceph_mdsc_put_request(req);
889 goto out;
890 }
891 req->r_locked_dir = dir;
Sage Weil2817b002009-10-06 11:31:08 -0700892 req->r_dentry = dget(dentry);
893 req->r_num_caps = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700894 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
895 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
896 err = ceph_mdsc_do_request(mdsc, dir, req);
897 if (!err && !req->r_reply_info.head->is_dentry)
898 err = ceph_handle_notrace_create(dir, dentry);
899 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800900out:
901 if (err)
Sage Weil2817b002009-10-06 11:31:08 -0700902 d_drop(dentry);
903 return err;
904}
905
Al Viro18bb1db2011-07-26 01:41:39 -0400906static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Sage Weil2817b002009-10-06 11:31:08 -0700907{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700908 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
909 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700910 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800911 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700912 int err = -EROFS;
913 int op;
914
915 if (ceph_snap(dir) == CEPH_SNAPDIR) {
916 /* mkdir .snap/foo is a MKSNAP */
917 op = CEPH_MDS_OP_MKSNAP;
Al Viroa4555892014-10-21 20:11:25 -0400918 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
919 dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700920 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
Al Viro18bb1db2011-07-26 01:41:39 -0400921 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
Sage Weil2817b002009-10-06 11:31:08 -0700922 op = CEPH_MDS_OP_MKDIR;
923 } else {
924 goto out;
925 }
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800926
927 mode |= S_IFDIR;
928 err = ceph_pre_init_acls(dir, &mode, &acls);
929 if (err < 0)
930 goto out;
931
Sage Weil2817b002009-10-06 11:31:08 -0700932 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
933 if (IS_ERR(req)) {
934 err = PTR_ERR(req);
935 goto out;
936 }
937
938 req->r_dentry = dget(dentry);
939 req->r_num_caps = 2;
940 req->r_locked_dir = dir;
941 req->r_args.mkdir.mode = cpu_to_le32(mode);
942 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
943 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800944 if (acls.pagelist) {
945 req->r_pagelist = acls.pagelist;
946 acls.pagelist = NULL;
947 }
Sage Weil2817b002009-10-06 11:31:08 -0700948 err = ceph_mdsc_do_request(mdsc, dir, req);
Yan, Zheng275dd192014-12-10 16:17:31 +0800949 if (!err &&
950 !req->r_reply_info.head->is_target &&
951 !req->r_reply_info.head->is_dentry)
Sage Weil2817b002009-10-06 11:31:08 -0700952 err = ceph_handle_notrace_create(dir, dentry);
953 ceph_mdsc_put_request(req);
954out:
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800955 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000956 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800957 else
Sage Weil2817b002009-10-06 11:31:08 -0700958 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800959 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700960 return err;
961}
962
963static int ceph_link(struct dentry *old_dentry, struct inode *dir,
964 struct dentry *dentry)
965{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700966 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
967 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700968 struct ceph_mds_request *req;
969 int err;
970
971 if (ceph_snap(dir) != CEPH_NOSNAP)
972 return -EROFS;
973
974 dout("link in dir %p old_dentry %p dentry %p\n", dir,
975 old_dentry, dentry);
976 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
977 if (IS_ERR(req)) {
978 d_drop(dentry);
979 return PTR_ERR(req);
980 }
981 req->r_dentry = dget(dentry);
982 req->r_num_caps = 2;
Sage Weil4b58c9b192013-02-05 13:41:23 -0800983 req->r_old_dentry = dget(old_dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700984 req->r_locked_dir = dir;
985 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
986 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengad88f232013-07-21 20:25:25 +0800987 /* release LINK_SHARED on source inode (mds will lock it) */
988 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
Sage Weil2817b002009-10-06 11:31:08 -0700989 err = ceph_mdsc_do_request(mdsc, dir, req);
Sage Weil70b666c2011-05-27 09:24:26 -0700990 if (err) {
Sage Weil2817b002009-10-06 11:31:08 -0700991 d_drop(dentry);
Sage Weil70b666c2011-05-27 09:24:26 -0700992 } else if (!req->r_reply_info.head->is_dentry) {
David Howells2b0143b2015-03-17 22:25:59 +0000993 ihold(d_inode(old_dentry));
994 d_instantiate(dentry, d_inode(old_dentry));
Sage Weil70b666c2011-05-27 09:24:26 -0700995 }
Sage Weil2817b002009-10-06 11:31:08 -0700996 ceph_mdsc_put_request(req);
997 return err;
998}
999
1000/*
1001 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1002 * looks like the link count will hit 0, drop any other caps (other
1003 * than PIN) we don't specifically want (due to the file still being
1004 * open).
1005 */
1006static int drop_caps_for_unlink(struct inode *inode)
1007{
1008 struct ceph_inode_info *ci = ceph_inode(inode);
1009 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1010
Sage Weilbe655592011-11-30 09:47:09 -08001011 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001012 if (inode->i_nlink == 1) {
1013 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1014 ci->i_ceph_flags |= CEPH_I_NODELAY;
1015 }
Sage Weilbe655592011-11-30 09:47:09 -08001016 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001017 return drop;
1018}
1019
1020/*
1021 * rmdir and unlink are differ only by the metadata op code
1022 */
1023static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1024{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001025 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1026 struct ceph_mds_client *mdsc = fsc->mdsc;
David Howells2b0143b2015-03-17 22:25:59 +00001027 struct inode *inode = d_inode(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001028 struct ceph_mds_request *req;
1029 int err = -EROFS;
1030 int op;
1031
1032 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1033 /* rmdir .snap/foo is RMSNAP */
Al Viroa4555892014-10-21 20:11:25 -04001034 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001035 op = CEPH_MDS_OP_RMSNAP;
1036 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1037 dout("unlink/rmdir dir %p dn %p inode %p\n",
1038 dir, dentry, inode);
David Howellse36cb0b2015-01-29 12:02:35 +00001039 op = d_is_dir(dentry) ?
Sage Weil2817b002009-10-06 11:31:08 -07001040 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1041 } else
1042 goto out;
1043 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1044 if (IS_ERR(req)) {
1045 err = PTR_ERR(req);
1046 goto out;
1047 }
1048 req->r_dentry = dget(dentry);
1049 req->r_num_caps = 2;
1050 req->r_locked_dir = dir;
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);
1058out:
1059 return err;
1060}
1061
1062static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1063 struct inode *new_dir, struct dentry *new_dentry)
1064{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001065 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1066 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001067 struct ceph_mds_request *req;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001068 int op = CEPH_MDS_OP_RENAME;
Sage Weil2817b002009-10-06 11:31:08 -07001069 int err;
1070
1071 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1072 return -EXDEV;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001073 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1074 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1075 op = CEPH_MDS_OP_RENAMESNAP;
1076 else
1077 return -EROFS;
1078 }
Sage Weil2817b002009-10-06 11:31:08 -07001079 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1080 old_dir, old_dentry, new_dir, new_dentry);
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001081 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Sage Weil2817b002009-10-06 11:31:08 -07001082 if (IS_ERR(req))
1083 return PTR_ERR(req);
Sage Weil180061a2013-02-05 13:36:05 -08001084 ihold(old_dir);
Sage Weil2817b002009-10-06 11:31:08 -07001085 req->r_dentry = dget(new_dentry);
1086 req->r_num_caps = 2;
1087 req->r_old_dentry = dget(old_dentry);
Sage Weil180061a2013-02-05 13:36:05 -08001088 req->r_old_dentry_dir = old_dir;
Sage Weil2817b002009-10-06 11:31:08 -07001089 req->r_locked_dir = new_dir;
1090 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1091 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1092 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1093 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1094 /* release LINK_RDCACHE on source inode (mds will lock it) */
1095 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
David Howells2b0143b2015-03-17 22:25:59 +00001096 if (d_really_is_positive(new_dentry))
1097 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
Sage Weil2817b002009-10-06 11:31:08 -07001098 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1099 if (!err && !req->r_reply_info.head->is_dentry) {
1100 /*
1101 * Normally d_move() is done by fill_trace (called by
1102 * do_request, above). If there is no trace, we need
1103 * to do it here.
1104 */
Sage Weilea1409f2010-04-28 16:12:06 -07001105
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001106 /* d_move screws up sibling dentries' offsets */
1107 ceph_dir_clear_complete(old_dir);
1108 ceph_dir_clear_complete(new_dir);
1109
Sage Weil2817b002009-10-06 11:31:08 -07001110 d_move(old_dentry, new_dentry);
Sage Weilea1409f2010-04-28 16:12:06 -07001111
1112 /* ensure target dentry is invalidated, despite
1113 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001114 ceph_invalidate_dentry_lease(new_dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001115 }
1116 ceph_mdsc_put_request(req);
1117 return err;
1118}
1119
Sage Weil81a6cf22010-05-14 09:35:38 -07001120/*
1121 * Ensure a dentry lease will no longer revalidate.
1122 */
1123void ceph_invalidate_dentry_lease(struct dentry *dentry)
1124{
1125 spin_lock(&dentry->d_lock);
1126 dentry->d_time = jiffies;
1127 ceph_dentry(dentry)->lease_shared_gen = 0;
1128 spin_unlock(&dentry->d_lock);
1129}
Sage Weil2817b002009-10-06 11:31:08 -07001130
1131/*
1132 * Check if dentry lease is valid. If not, delete the lease. Try to
1133 * renew if the least is more than half up.
1134 */
1135static int dentry_lease_is_valid(struct dentry *dentry)
1136{
1137 struct ceph_dentry_info *di;
1138 struct ceph_mds_session *s;
1139 int valid = 0;
1140 u32 gen;
1141 unsigned long ttl;
1142 struct ceph_mds_session *session = NULL;
1143 struct inode *dir = NULL;
1144 u32 seq = 0;
1145
1146 spin_lock(&dentry->d_lock);
1147 di = ceph_dentry(dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001148 if (di->lease_session) {
Sage Weil2817b002009-10-06 11:31:08 -07001149 s = di->lease_session;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001150 spin_lock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001151 gen = s->s_cap_gen;
1152 ttl = s->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001153 spin_unlock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001154
1155 if (di->lease_gen == gen &&
1156 time_before(jiffies, dentry->d_time) &&
1157 time_before(jiffies, ttl)) {
1158 valid = 1;
1159 if (di->lease_renew_after &&
1160 time_after(jiffies, di->lease_renew_after)) {
1161 /* we should renew */
David Howells2b0143b2015-03-17 22:25:59 +00001162 dir = d_inode(dentry->d_parent);
Sage Weil2817b002009-10-06 11:31:08 -07001163 session = ceph_get_mds_session(s);
1164 seq = di->lease_seq;
1165 di->lease_renew_after = 0;
1166 di->lease_renew_from = jiffies;
1167 }
Sage Weil2817b002009-10-06 11:31:08 -07001168 }
1169 }
1170 spin_unlock(&dentry->d_lock);
1171
1172 if (session) {
1173 ceph_mdsc_lease_send_msg(session, dir, dentry,
1174 CEPH_MDS_LEASE_RENEW, seq);
1175 ceph_put_mds_session(session);
1176 }
1177 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1178 return valid;
1179}
1180
1181/*
1182 * Check if directory-wide content lease/cap is valid.
1183 */
1184static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1185{
1186 struct ceph_inode_info *ci = ceph_inode(dir);
1187 struct ceph_dentry_info *di = ceph_dentry(dentry);
1188 int valid = 0;
1189
Sage Weilbe655592011-11-30 09:47:09 -08001190 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001191 if (ci->i_shared_gen == di->lease_shared_gen)
1192 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
Sage Weilbe655592011-11-30 09:47:09 -08001193 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001194 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1195 dir, (unsigned)ci->i_shared_gen, dentry,
1196 (unsigned)di->lease_shared_gen, valid);
1197 return valid;
1198}
1199
1200/*
1201 * Check if cached dentry can be trusted.
1202 */
Al Viro0b728e12012-06-10 16:03:43 -04001203static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001204{
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001205 int valid = 0;
Yan, Zheng641235d2016-03-16 16:40:23 +08001206 struct dentry *parent;
Nick Piggin34286d62011-01-07 17:49:57 +11001207 struct inode *dir;
1208
Al Viro0b728e12012-06-10 16:03:43 -04001209 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001210 return -ECHILD;
1211
Al Viroa4555892014-10-21 20:11:25 -04001212 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001213 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
Sage Weil2817b002009-10-06 11:31:08 -07001214
Yan, Zheng641235d2016-03-16 16:40:23 +08001215 parent = dget_parent(dentry);
1216 dir = d_inode(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001217
Sage Weil2817b002009-10-06 11:31:08 -07001218 /* always trust cached snapped dentries, snapdir dentry */
1219 if (ceph_snap(dir) != CEPH_NOSNAP) {
Al Viroa4555892014-10-21 20:11:25 -04001220 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001221 dentry, d_inode(dentry));
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001222 valid = 1;
David Howells2b0143b2015-03-17 22:25:59 +00001223 } else if (d_really_is_positive(dentry) &&
1224 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001225 valid = 1;
1226 } else if (dentry_lease_is_valid(dentry) ||
1227 dir_lease_is_valid(dir, dentry)) {
David Howells2b0143b2015-03-17 22:25:59 +00001228 if (d_really_is_positive(dentry))
1229 valid = ceph_is_any_caps(d_inode(dentry));
Yan, Zheng9215aee2013-11-30 12:47:41 +08001230 else
1231 valid = 1;
Sage Weil2817b002009-10-06 11:31:08 -07001232 }
Sage Weil2817b002009-10-06 11:31:08 -07001233
Yan, Zheng200fd272016-03-17 14:41:59 +08001234 if (!valid) {
1235 struct ceph_mds_client *mdsc =
1236 ceph_sb_to_client(dir->i_sb)->mdsc;
1237 struct ceph_mds_request *req;
1238 int op, mask, err;
1239
1240 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1241 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1242 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1243 if (!IS_ERR(req)) {
1244 req->r_dentry = dget(dentry);
1245 req->r_num_caps = 2;
1246
1247 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1248 if (ceph_security_xattr_wanted(dir))
1249 mask |= CEPH_CAP_XATTR_SHARED;
1250 req->r_args.getattr.mask = mask;
1251
1252 req->r_locked_dir = dir;
1253 err = ceph_mdsc_do_request(mdsc, NULL, req);
1254 if (err == 0 || err == -ENOENT) {
1255 if (dentry == req->r_dentry) {
1256 valid = !d_unhashed(dentry);
1257 } else {
1258 d_invalidate(req->r_dentry);
1259 err = -EAGAIN;
1260 }
1261 }
1262 ceph_mdsc_put_request(req);
1263 dout("d_revalidate %p lookup result=%d\n",
1264 dentry, err);
1265 }
1266 }
1267
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001268 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
Yan, Zheng9215aee2013-11-30 12:47:41 +08001269 if (valid) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001270 ceph_dentry_lru_touch(dentry);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001271 } else {
1272 ceph_dir_clear_complete(dir);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001273 }
Yan, Zheng641235d2016-03-16 16:40:23 +08001274
1275 dput(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001276 return valid;
Sage Weil2817b002009-10-06 11:31:08 -07001277}
1278
1279/*
Sage Weil147851d2011-03-15 14:57:41 -07001280 * Release our ceph_dentry_info.
Sage Weil2817b002009-10-06 11:31:08 -07001281 */
Sage Weil147851d2011-03-15 14:57:41 -07001282static void ceph_d_release(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -07001283{
1284 struct ceph_dentry_info *di = ceph_dentry(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001285
Sage Weil147851d2011-03-15 14:57:41 -07001286 dout("d_release %p\n", dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001287 ceph_dentry_lru_del(dentry);
1288 if (di->lease_session)
1289 ceph_put_mds_session(di->lease_session);
1290 kmem_cache_free(ceph_dentry_cachep, di);
1291 dentry->d_fsdata = NULL;
Sage Weil2817b002009-10-06 11:31:08 -07001292}
1293
1294static int ceph_snapdir_d_revalidate(struct dentry *dentry,
Al Viro0b728e12012-06-10 16:03:43 -04001295 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001296{
1297 /*
1298 * Eventually, we'll want to revalidate snapped metadata
1299 * too... probably...
1300 */
1301 return 1;
1302}
1303
Sage Weilb58dc412011-03-15 15:53:40 -07001304/*
1305 * When the VFS prunes a dentry from the cache, we need to clear the
1306 * complete flag on the parent directory.
1307 *
1308 * Called under dentry->d_lock.
1309 */
1310static void ceph_d_prune(struct dentry *dentry)
1311{
Sage Weil774ac212011-11-11 09:48:08 -08001312 dout("ceph_d_prune %p\n", dentry);
Sage Weilb58dc412011-03-15 15:53:40 -07001313
1314 /* do we have a valid parent? */
Sage Weil8842b3b2012-06-07 13:43:35 -07001315 if (IS_ROOT(dentry))
Sage Weilb58dc412011-03-15 15:53:40 -07001316 return;
1317
Yan, Zheng2f276c52013-03-13 19:44:32 +08001318 /* if we are not hashed, we don't affect dir's completeness */
Sage Weilb58dc412011-03-15 15:53:40 -07001319 if (d_unhashed(dentry))
1320 return;
1321
1322 /*
1323 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1324 * cleared until d_release
1325 */
David Howells2b0143b2015-03-17 22:25:59 +00001326 ceph_dir_clear_complete(d_inode(dentry->d_parent));
Sage Weilb58dc412011-03-15 15:53:40 -07001327}
Sage Weil2817b002009-10-06 11:31:08 -07001328
1329/*
1330 * read() on a dir. This weird interface hack only works if mounted
1331 * with '-o dirstat'.
1332 */
1333static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1334 loff_t *ppos)
1335{
1336 struct ceph_file_info *cf = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05001337 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -07001338 struct ceph_inode_info *ci = ceph_inode(inode);
1339 int left;
Sage Weilae598082011-05-12 14:28:05 -07001340 const int bufsize = 1024;
Sage Weil2817b002009-10-06 11:31:08 -07001341
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001342 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
Sage Weil2817b002009-10-06 11:31:08 -07001343 return -EISDIR;
1344
1345 if (!cf->dir_info) {
Yan, Zheng687265e2015-06-13 17:27:05 +08001346 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -07001347 if (!cf->dir_info)
1348 return -ENOMEM;
1349 cf->dir_info_len =
Sage Weilae598082011-05-12 14:28:05 -07001350 snprintf(cf->dir_info, bufsize,
Sage Weil2817b002009-10-06 11:31:08 -07001351 "entries: %20lld\n"
1352 " files: %20lld\n"
1353 " subdirs: %20lld\n"
1354 "rentries: %20lld\n"
1355 " rfiles: %20lld\n"
1356 " rsubdirs: %20lld\n"
1357 "rbytes: %20lld\n"
1358 "rctime: %10ld.%09ld\n",
1359 ci->i_files + ci->i_subdirs,
1360 ci->i_files,
1361 ci->i_subdirs,
1362 ci->i_rfiles + ci->i_rsubdirs,
1363 ci->i_rfiles,
1364 ci->i_rsubdirs,
1365 ci->i_rbytes,
1366 (long)ci->i_rctime.tv_sec,
1367 (long)ci->i_rctime.tv_nsec);
1368 }
1369
1370 if (*ppos >= cf->dir_info_len)
1371 return 0;
1372 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1373 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1374 if (left == size)
1375 return -EFAULT;
1376 *ppos += (size - left);
1377 return size - left;
1378}
1379
1380/*
Sage Weil2817b002009-10-06 11:31:08 -07001381 * We maintain a private dentry LRU.
1382 *
1383 * FIXME: this needs to be changed to a per-mds lru to be useful.
1384 */
1385void ceph_dentry_lru_add(struct dentry *dn)
1386{
1387 struct ceph_dentry_info *di = ceph_dentry(dn);
1388 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001389
Al Viroa4555892014-10-21 20:11:25 -04001390 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001391 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1392 spin_lock(&mdsc->dentry_lru_lock);
1393 list_add_tail(&di->lru, &mdsc->dentry_lru);
1394 mdsc->num_dentry++;
1395 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001396}
1397
1398void ceph_dentry_lru_touch(struct dentry *dn)
1399{
1400 struct ceph_dentry_info *di = ceph_dentry(dn);
1401 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001402
Al Viroa4555892014-10-21 20:11:25 -04001403 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1404 di->offset);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001405 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1406 spin_lock(&mdsc->dentry_lru_lock);
1407 list_move_tail(&di->lru, &mdsc->dentry_lru);
1408 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001409}
1410
1411void ceph_dentry_lru_del(struct dentry *dn)
1412{
1413 struct ceph_dentry_info *di = ceph_dentry(dn);
1414 struct ceph_mds_client *mdsc;
1415
Al Viroa4555892014-10-21 20:11:25 -04001416 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001417 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1418 spin_lock(&mdsc->dentry_lru_lock);
1419 list_del_init(&di->lru);
1420 mdsc->num_dentry--;
1421 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001422}
1423
Sage Weil6c0f3af2010-11-16 11:14:34 -08001424/*
1425 * Return name hash for a given dentry. This is dependent on
1426 * the parent directory's hash function.
1427 */
Sage Weile5f86dc2011-07-26 11:30:55 -07001428unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
Sage Weil6c0f3af2010-11-16 11:14:34 -08001429{
Sage Weil6c0f3af2010-11-16 11:14:34 -08001430 struct ceph_inode_info *dci = ceph_inode(dir);
1431
1432 switch (dci->i_dir_layout.dl_dir_hash) {
1433 case 0: /* for backward compat */
1434 case CEPH_STR_HASH_LINUX:
1435 return dn->d_name.hash;
1436
1437 default:
1438 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1439 dn->d_name.name, dn->d_name.len);
1440 }
1441}
1442
Sage Weil2817b002009-10-06 11:31:08 -07001443const struct file_operations ceph_dir_fops = {
1444 .read = ceph_read_dir,
Al Viro77acfa22013-05-17 16:52:26 -04001445 .iterate = ceph_readdir,
Sage Weil2817b002009-10-06 11:31:08 -07001446 .llseek = ceph_dir_llseek,
1447 .open = ceph_open,
1448 .release = ceph_release,
1449 .unlocked_ioctl = ceph_ioctl,
Yan, Zhengda819c82015-05-27 11:19:34 +08001450 .fsync = ceph_fsync,
Sage Weil2817b002009-10-06 11:31:08 -07001451};
1452
Yan, Zheng38c48b52015-01-14 13:46:04 +08001453const struct file_operations ceph_snapdir_fops = {
1454 .iterate = ceph_readdir,
1455 .llseek = ceph_dir_llseek,
1456 .open = ceph_open,
1457 .release = ceph_release,
1458};
1459
Sage Weil2817b002009-10-06 11:31:08 -07001460const struct inode_operations ceph_dir_iops = {
1461 .lookup = ceph_lookup,
1462 .permission = ceph_permission,
1463 .getattr = ceph_getattr,
1464 .setattr = ceph_setattr,
1465 .setxattr = ceph_setxattr,
1466 .getxattr = ceph_getxattr,
1467 .listxattr = ceph_listxattr,
1468 .removexattr = ceph_removexattr,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001469 .get_acl = ceph_get_acl,
Sage Weil72466d02014-01-29 06:22:25 -08001470 .set_acl = ceph_set_acl,
Sage Weil2817b002009-10-06 11:31:08 -07001471 .mknod = ceph_mknod,
1472 .symlink = ceph_symlink,
1473 .mkdir = ceph_mkdir,
1474 .link = ceph_link,
1475 .unlink = ceph_unlink,
1476 .rmdir = ceph_unlink,
1477 .rename = ceph_rename,
1478 .create = ceph_create,
Miklos Szeredi2d83bde2012-06-05 15:10:25 +02001479 .atomic_open = ceph_atomic_open,
Sage Weil2817b002009-10-06 11:31:08 -07001480};
1481
Yan, Zheng38c48b52015-01-14 13:46:04 +08001482const struct inode_operations ceph_snapdir_iops = {
1483 .lookup = ceph_lookup,
1484 .permission = ceph_permission,
1485 .getattr = ceph_getattr,
1486 .mkdir = ceph_mkdir,
1487 .rmdir = ceph_unlink,
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001488 .rename = ceph_rename,
Yan, Zheng38c48b52015-01-14 13:46:04 +08001489};
1490
Sage Weil52dfb8a2010-08-03 10:25:30 -07001491const struct dentry_operations ceph_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001492 .d_revalidate = ceph_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001493 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001494 .d_prune = ceph_d_prune,
Sage Weil2817b002009-10-06 11:31:08 -07001495};
1496
Sage Weil52dfb8a2010-08-03 10:25:30 -07001497const struct dentry_operations ceph_snapdir_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001498 .d_revalidate = ceph_snapdir_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001499 .d_release = ceph_d_release,
Sage Weil2817b002009-10-06 11:31:08 -07001500};
1501
Sage Weil52dfb8a2010-08-03 10:25:30 -07001502const struct dentry_operations ceph_snap_dentry_ops = {
Sage Weil147851d2011-03-15 14:57:41 -07001503 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001504 .d_prune = ceph_d_prune,
Sage Weil2817b002009-10-06 11:31:08 -07001505};