blob: 31a2cdf39210342e0a5085af62fb6ab63f1e48ac [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/*
72 * for readdir, we encode the directory frag and offset within that
73 * frag into f_pos.
74 */
75static unsigned fpos_frag(loff_t p)
76{
77 return p >> 32;
78}
79static unsigned fpos_off(loff_t p)
80{
81 return p & 0xffffffff;
82}
83
Yan, Zheng4d5f5df2014-02-13 19:40:26 +080084static int fpos_cmp(loff_t l, loff_t r)
85{
86 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
87 if (v)
88 return v;
89 return (int)(fpos_off(l) - fpos_off(r));
90}
91
Sage Weil2817b002009-10-06 11:31:08 -070092/*
Yan, Zhengfdd4e152015-06-16 20:48:56 +080093 * make note of the last dentry we read, so we can
94 * continue at the same lexicographical point,
95 * regardless of what dir changes take place on the
96 * server.
97 */
98static int note_last_dentry(struct ceph_file_info *fi, const char *name,
99 int len, unsigned next_offset)
100{
101 char *buf = kmalloc(len+1, GFP_KERNEL);
102 if (!buf)
103 return -ENOMEM;
104 kfree(fi->last_name);
105 fi->last_name = buf;
106 memcpy(fi->last_name, name, len);
107 fi->last_name[len] = 0;
108 fi->next_offset = next_offset;
109 dout("note_last_dentry '%s'\n", fi->last_name);
110 return 0;
111}
112
Yan, Zhengc530cd22016-04-28 17:43:35 +0800113
114static struct dentry *
115__dcache_find_get_entry(struct dentry *parent, u64 idx,
116 struct ceph_readdir_cache_control *cache_ctl)
117{
118 struct inode *dir = d_inode(parent);
119 struct dentry *dentry;
120 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
121 loff_t ptr_pos = idx * sizeof(struct dentry *);
122 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
123
124 if (ptr_pos >= i_size_read(dir))
125 return NULL;
126
127 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
128 ceph_readdir_cache_release(cache_ctl);
129 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
130 if (!cache_ctl->page) {
131 dout(" page %lu not found\n", ptr_pgoff);
132 return ERR_PTR(-EAGAIN);
133 }
134 /* reading/filling the cache are serialized by
135 i_mutex, no need to use page lock */
136 unlock_page(cache_ctl->page);
137 cache_ctl->dentries = kmap(cache_ctl->page);
138 }
139
140 cache_ctl->index = idx & idx_mask;
141
142 rcu_read_lock();
143 spin_lock(&parent->d_lock);
144 /* check i_size again here, because empty directory can be
145 * marked as complete while not holding the i_mutex. */
146 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
147 dentry = cache_ctl->dentries[cache_ctl->index];
148 else
149 dentry = NULL;
150 spin_unlock(&parent->d_lock);
151 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
152 dentry = NULL;
153 rcu_read_unlock();
154 return dentry ? : ERR_PTR(-EAGAIN);
155}
156
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800157/*
Sage Weil2817b002009-10-06 11:31:08 -0700158 * When possible, we try to satisfy a readdir by peeking at the
159 * dcache. We make this work by carefully ordering dentries on
Al Viro946e51f2014-10-26 19:19:16 -0400160 * d_child when we initially get results back from the MDS, and
Sage Weil2817b002009-10-06 11:31:08 -0700161 * falling back to a "normal" sync readdir if any dentries in the dir
162 * are dropped.
163 *
Yan, Zheng2f276c52013-03-13 19:44:32 +0800164 * Complete dir indicates that we have all dentries in the dir. It is
Sage Weil2817b002009-10-06 11:31:08 -0700165 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
166 * the MDS if/when the directory is modified).
167 */
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800168static int __dcache_readdir(struct file *file, struct dir_context *ctx,
169 u32 shared_gen)
Sage Weil2817b002009-10-06 11:31:08 -0700170{
Al Viro77acfa22013-05-17 16:52:26 -0400171 struct ceph_file_info *fi = file->private_data;
Al Virob5830432014-10-31 01:22:04 -0400172 struct dentry *parent = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000173 struct inode *dir = d_inode(parent);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800174 struct dentry *dentry, *last = NULL;
Sage Weil2817b002009-10-06 11:31:08 -0700175 struct ceph_dentry_info *di;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800176 struct ceph_readdir_cache_control cache_ctl = {};
Yan, Zhengc530cd22016-04-28 17:43:35 +0800177 u64 idx = 0;
178 int err = 0;
Sage Weil2817b002009-10-06 11:31:08 -0700179
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800180 dout("__dcache_readdir %p v%u at %llu\n", dir, shared_gen, ctx->pos);
Sage Weil2817b002009-10-06 11:31:08 -0700181
Yan, Zhengc530cd22016-04-28 17:43:35 +0800182 /* search start position */
183 if (ctx->pos > 2) {
184 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
185 while (count > 0) {
186 u64 step = count >> 1;
187 dentry = __dcache_find_get_entry(parent, idx + step,
188 &cache_ctl);
189 if (!dentry) {
190 /* use linar search */
191 idx = 0;
192 break;
193 }
194 if (IS_ERR(dentry)) {
195 err = PTR_ERR(dentry);
196 goto out;
197 }
198 di = ceph_dentry(dentry);
199 spin_lock(&dentry->d_lock);
200 if (fpos_cmp(di->offset, ctx->pos) < 0) {
201 idx += step + 1;
202 count -= step + 1;
203 } else {
204 count = step;
205 }
206 spin_unlock(&dentry->d_lock);
207 dput(dentry);
208 }
209
210 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
Sage Weil2817b002009-10-06 11:31:08 -0700211 }
212
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800213
Yan, Zhengc530cd22016-04-28 17:43:35 +0800214 for (;;) {
215 bool emit_dentry = false;
216 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
217 if (!dentry) {
Sage Weil9cfa1092011-07-26 11:26:18 -0700218 fi->flags |= CEPH_F_ATEND;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800219 err = 0;
220 break;
Sage Weil2817b002009-10-06 11:31:08 -0700221 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800222 if (IS_ERR(dentry)) {
223 err = PTR_ERR(dentry);
224 goto out;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800225 }
226
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800227 di = ceph_dentry(dentry);
228 spin_lock(&dentry->d_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800229 if (di->lease_shared_gen == shared_gen &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800230 d_really_is_positive(dentry) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800231 fpos_cmp(ctx->pos, di->offset) <= 0) {
232 emit_dentry = true;
Sage Weil2817b002009-10-06 11:31:08 -0700233 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800234 spin_unlock(&dentry->d_lock);
235
236 if (emit_dentry) {
237 dout(" %llu (%llu) dentry %p %pd %p\n", di->offset, ctx->pos,
238 dentry, dentry, d_inode(dentry));
239 ctx->pos = di->offset;
240 if (!dir_emit(ctx, dentry->d_name.name,
241 dentry->d_name.len,
242 ceph_translate_ino(dentry->d_sb,
243 d_inode(dentry)->i_ino),
244 d_inode(dentry)->i_mode >> 12)) {
245 dput(dentry);
246 err = 0;
247 break;
248 }
249 ctx->pos++;
250
251 if (last)
252 dput(last);
253 last = dentry;
254 } else {
255 dput(dentry);
256 }
Sage Weil2817b002009-10-06 11:31:08 -0700257 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800258out:
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800259 ceph_readdir_cache_release(&cache_ctl);
260 if (last) {
261 int ret;
262 di = ceph_dentry(last);
263 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
264 fpos_off(di->offset) + 1);
265 if (ret < 0)
266 err = ret;
Al Viro77acfa22013-05-17 16:52:26 -0400267 dput(last);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800268 }
Sage Weil2817b002009-10-06 11:31:08 -0700269 return err;
270}
271
Al Viro77acfa22013-05-17 16:52:26 -0400272static int ceph_readdir(struct file *file, struct dir_context *ctx)
Sage Weil2817b002009-10-06 11:31:08 -0700273{
Al Viro77acfa22013-05-17 16:52:26 -0400274 struct ceph_file_info *fi = file->private_data;
275 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -0700276 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700277 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
278 struct ceph_mds_client *mdsc = fsc->mdsc;
Al Viro77acfa22013-05-17 16:52:26 -0400279 unsigned frag = fpos_frag(ctx->pos);
280 int off = fpos_off(ctx->pos);
Sage Weil2817b002009-10-06 11:31:08 -0700281 int err;
282 u32 ftype;
283 struct ceph_mds_reply_info_parsed *rinfo;
Sage Weil2817b002009-10-06 11:31:08 -0700284
Al Viro77acfa22013-05-17 16:52:26 -0400285 dout("readdir %p file %p frag %u off %u\n", inode, file, frag, off);
Sage Weil9cfa1092011-07-26 11:26:18 -0700286 if (fi->flags & CEPH_F_ATEND)
Sage Weil2817b002009-10-06 11:31:08 -0700287 return 0;
288
289 /* always start with . and .. */
Al Viro77acfa22013-05-17 16:52:26 -0400290 if (ctx->pos == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700291 dout("readdir off 0 -> '.'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400292 if (!dir_emit(ctx, ".", 1,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800293 ceph_translate_ino(inode->i_sb, inode->i_ino),
Al Viro77acfa22013-05-17 16:52:26 -0400294 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700295 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400296 ctx->pos = 1;
Sage Weil2817b002009-10-06 11:31:08 -0700297 off = 1;
298 }
Al Viro77acfa22013-05-17 16:52:26 -0400299 if (ctx->pos == 1) {
Al Virob5830432014-10-31 01:22:04 -0400300 ino_t ino = parent_ino(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700301 dout("readdir off 1 -> '..'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400302 if (!dir_emit(ctx, "..", 2,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800303 ceph_translate_ino(inode->i_sb, ino),
Al Viro77acfa22013-05-17 16:52:26 -0400304 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700305 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400306 ctx->pos = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700307 off = 2;
308 }
309
310 /* can we use the dcache? */
Sage Weilbe655592011-11-30 09:47:09 -0800311 spin_lock(&ci->i_ceph_lock);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800312 if (ceph_test_mount_opt(fsc, DCACHE) &&
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700313 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
Sage Weila0dff782010-07-22 13:47:21 -0700314 ceph_snap(inode) != CEPH_SNAPDIR &&
Yan, Zheng70db4f32014-10-21 18:09:56 -0700315 __ceph_dir_is_complete_ordered(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700316 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800317 u32 shared_gen = ci->i_shared_gen;
Sage Weilbe655592011-11-30 09:47:09 -0800318 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800319 err = __dcache_readdir(file, ctx, shared_gen);
Sage Weilefa4c122010-10-18 14:04:31 -0700320 if (err != -EAGAIN)
Sage Weil2817b002009-10-06 11:31:08 -0700321 return err;
Yan, Zheng0081bd82014-04-08 21:42:59 +0800322 frag = fpos_frag(ctx->pos);
323 off = fpos_off(ctx->pos);
Sage Weilefa4c122010-10-18 14:04:31 -0700324 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800325 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700326 }
Sage Weil2817b002009-10-06 11:31:08 -0700327
328 /* proceed with a normal readdir */
Sage Weil2817b002009-10-06 11:31:08 -0700329more:
330 /* do we have the correct frag content buffered? */
331 if (fi->frag != frag || fi->last_readdir == NULL) {
332 struct ceph_mds_request *req;
333 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
334 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
335
336 /* discard old result, if any */
Sage Weil393f6622010-03-10 12:03:32 -0800337 if (fi->last_readdir) {
Sage Weil2817b002009-10-06 11:31:08 -0700338 ceph_mdsc_put_request(fi->last_readdir);
Sage Weil393f6622010-03-10 12:03:32 -0800339 fi->last_readdir = NULL;
340 }
Sage Weil2817b002009-10-06 11:31:08 -0700341
Sage Weil2817b002009-10-06 11:31:08 -0700342 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
343 ceph_vinop(inode), frag, fi->last_name);
344 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
345 if (IS_ERR(req))
346 return PTR_ERR(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800347 err = ceph_alloc_readdir_reply_buffer(req, inode);
348 if (err) {
349 ceph_mdsc_put_request(req);
350 return err;
351 }
Sage Weil2817b002009-10-06 11:31:08 -0700352 /* hints to request -> mds selection code */
353 req->r_direct_mode = USE_AUTH_MDS;
354 req->r_direct_hash = ceph_frag_value(frag);
355 req->r_direct_is_hash = true;
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400356 if (fi->last_name) {
Yan, Zheng687265e2015-06-13 17:27:05 +0800357 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400358 if (!req->r_path2) {
359 ceph_mdsc_put_request(req);
360 return -ENOMEM;
361 }
362 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800363 req->r_dir_release_cnt = fi->dir_release_count;
364 req->r_dir_ordered_cnt = fi->dir_ordered_count;
365 req->r_readdir_cache_idx = fi->readdir_cache_idx;
Sage Weil2817b002009-10-06 11:31:08 -0700366 req->r_readdir_offset = fi->next_offset;
367 req->r_args.readdir.frag = cpu_to_le32(frag);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400368
369 req->r_inode = inode;
370 ihold(inode);
371 req->r_dentry = dget(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700372 err = ceph_mdsc_do_request(mdsc, NULL, req);
373 if (err < 0) {
374 ceph_mdsc_put_request(req);
375 return err;
376 }
377 dout("readdir got and parsed readdir result=%d"
378 " on frag %x, end=%d, complete=%d\n", err, frag,
379 (int)req->r_reply_info.dir_end,
380 (int)req->r_reply_info.dir_complete);
381
Sage Weil2817b002009-10-06 11:31:08 -0700382
383 /* note next offset and last dentry name */
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800384 rinfo = &req->r_reply_info;
385 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
386 frag = le32_to_cpu(rinfo->dir_dir->frag);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800387 off = req->r_readdir_offset;
388 fi->next_offset = off;
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800389 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800390
Yan, Zhengf0494202014-02-27 16:26:24 +0800391 fi->frag = frag;
Sage Weil2817b002009-10-06 11:31:08 -0700392 fi->offset = fi->next_offset;
393 fi->last_readdir = req;
394
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800395 if (req->r_did_prepopulate) {
396 fi->readdir_cache_idx = req->r_readdir_cache_idx;
397 if (fi->readdir_cache_idx < 0) {
398 /* preclude from marking dir ordered */
399 fi->dir_ordered_count = 0;
400 } else if (ceph_frag_is_leftmost(frag) && off == 2) {
401 /* note dir version at start of readdir so
402 * we can tell if any dentries get dropped */
403 fi->dir_release_count = req->r_dir_release_cnt;
404 fi->dir_ordered_count = req->r_dir_ordered_cnt;
405 }
406 } else {
407 dout("readdir !did_prepopulate");
408 /* disable readdir cache */
409 fi->readdir_cache_idx = -1;
410 /* preclude from marking dir complete */
411 fi->dir_release_count = 0;
412 }
413
Sage Weil2817b002009-10-06 11:31:08 -0700414 if (req->r_reply_info.dir_end) {
415 kfree(fi->last_name);
416 fi->last_name = NULL;
Yan, Zhenga78600e2016-04-27 17:32:34 +0800417 fi->next_offset = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700418 } else {
Sage Weil2817b002009-10-06 11:31:08 -0700419 err = note_last_dentry(fi,
420 rinfo->dir_dname[rinfo->dir_nr-1],
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800421 rinfo->dir_dname_len[rinfo->dir_nr-1],
422 fi->next_offset + rinfo->dir_nr);
Sage Weil2817b002009-10-06 11:31:08 -0700423 if (err)
424 return err;
Sage Weil2817b002009-10-06 11:31:08 -0700425 }
426 }
427
428 rinfo = &fi->last_readdir->r_reply_info;
429 dout("readdir frag %x num %d off %d chunkoff %d\n", frag,
430 rinfo->dir_nr, off, fi->offset);
Al Viro77acfa22013-05-17 16:52:26 -0400431
432 ctx->pos = ceph_make_fpos(frag, off);
Sage Weilda398222011-05-12 15:28:11 -0700433 while (off >= fi->offset && off - fi->offset < rinfo->dir_nr) {
Sage Weil2817b002009-10-06 11:31:08 -0700434 struct ceph_mds_reply_inode *in =
435 rinfo->dir_in[off - fi->offset].in;
Sage Weil3105c192010-11-18 09:15:07 -0800436 struct ceph_vino vino;
437 ino_t ino;
438
Sage Weil2817b002009-10-06 11:31:08 -0700439 dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n",
Al Viro77acfa22013-05-17 16:52:26 -0400440 off, off - fi->offset, rinfo->dir_nr, ctx->pos,
Sage Weil2817b002009-10-06 11:31:08 -0700441 rinfo->dir_dname_len[off - fi->offset],
442 rinfo->dir_dname[off - fi->offset], in);
443 BUG_ON(!in);
444 ftype = le32_to_cpu(in->mode) >> 12;
Sage Weil3105c192010-11-18 09:15:07 -0800445 vino.ino = le64_to_cpu(in->ino);
446 vino.snap = le64_to_cpu(in->snapid);
447 ino = ceph_vino_to_ino(vino);
Al Viro77acfa22013-05-17 16:52:26 -0400448 if (!dir_emit(ctx,
Sage Weil2817b002009-10-06 11:31:08 -0700449 rinfo->dir_dname[off - fi->offset],
450 rinfo->dir_dname_len[off - fi->offset],
Al Viro77acfa22013-05-17 16:52:26 -0400451 ceph_translate_ino(inode->i_sb, ino), ftype)) {
Sage Weil2817b002009-10-06 11:31:08 -0700452 dout("filldir stopping us...\n");
453 return 0;
454 }
455 off++;
Al Viro77acfa22013-05-17 16:52:26 -0400456 ctx->pos++;
Sage Weil2817b002009-10-06 11:31:08 -0700457 }
458
459 if (fi->last_name) {
460 ceph_mdsc_put_request(fi->last_readdir);
461 fi->last_readdir = NULL;
462 goto more;
463 }
464
465 /* more frags? */
466 if (!ceph_frag_is_rightmost(frag)) {
467 frag = ceph_frag_next(frag);
Yan, Zhenga78600e2016-04-27 17:32:34 +0800468 off = 2;
Al Viro77acfa22013-05-17 16:52:26 -0400469 ctx->pos = ceph_make_fpos(frag, off);
Sage Weil2817b002009-10-06 11:31:08 -0700470 dout("readdir next frag is %x\n", frag);
471 goto more;
472 }
Sage Weil9cfa1092011-07-26 11:26:18 -0700473 fi->flags |= CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700474
475 /*
476 * if dir_release_count still matches the dir, no dentries
477 * were released during the whole readdir, and we should have
478 * the complete dir contents in our cache.
479 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800480 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
481 spin_lock(&ci->i_ceph_lock);
482 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700483 dout(" marking %p complete and ordered\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800484 /* use i_size to track number of entries in
485 * readdir cache */
486 BUG_ON(fi->readdir_cache_idx < 0);
487 i_size_write(inode, fi->readdir_cache_idx *
488 sizeof(struct dentry*));
489 } else {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700490 dout(" marking %p complete\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800491 }
Yan, Zheng70db4f32014-10-21 18:09:56 -0700492 __ceph_dir_set_complete(ci, fi->dir_release_count,
493 fi->dir_ordered_count);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800494 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700495 }
Sage Weil2817b002009-10-06 11:31:08 -0700496
Al Viro77acfa22013-05-17 16:52:26 -0400497 dout("readdir %p file %p done.\n", inode, file);
Sage Weil2817b002009-10-06 11:31:08 -0700498 return 0;
499}
500
Yan, Zhengdcd3cc02014-02-28 16:36:09 +0800501static void reset_readdir(struct ceph_file_info *fi, unsigned frag)
Sage Weil2817b002009-10-06 11:31:08 -0700502{
503 if (fi->last_readdir) {
504 ceph_mdsc_put_request(fi->last_readdir);
505 fi->last_readdir = NULL;
506 }
507 kfree(fi->last_name);
Sage Weila1629c32010-11-11 15:24:06 -0800508 fi->last_name = NULL;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800509 fi->dir_release_count = 0;
510 fi->readdir_cache_idx = -1;
Yan, Zhenga78600e2016-04-27 17:32:34 +0800511 fi->next_offset = 2; /* compensate for . and .. */
Sage Weil9cfa1092011-07-26 11:26:18 -0700512 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700513}
514
Andrew Morton965c8e52012-12-17 15:59:39 -0800515static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
Sage Weil2817b002009-10-06 11:31:08 -0700516{
517 struct ceph_file_info *fi = file->private_data;
518 struct inode *inode = file->f_mapping->host;
Yan, Zhengf0494202014-02-27 16:26:24 +0800519 loff_t old_offset = ceph_make_fpos(fi->frag, fi->next_offset);
Sage Weil2817b002009-10-06 11:31:08 -0700520 loff_t retval;
521
Al Viro59551022016-01-22 15:40:57 -0500522 inode_lock(inode);
Josef Bacik06222e42011-07-18 13:21:38 -0400523 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800524 switch (whence) {
Sage Weil2817b002009-10-06 11:31:08 -0700525 case SEEK_CUR:
526 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -0400527 case SEEK_SET:
528 break;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800529 case SEEK_END:
530 retval = -EOPNOTSUPP;
Josef Bacik06222e42011-07-18 13:21:38 -0400531 default:
532 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700533 }
Josef Bacik06222e42011-07-18 13:21:38 -0400534
Yan, Zhengf0494202014-02-27 16:26:24 +0800535 if (offset >= 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700536 if (offset != file->f_pos) {
537 file->f_pos = offset;
538 file->f_version = 0;
Sage Weil9cfa1092011-07-26 11:26:18 -0700539 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700540 }
541 retval = offset;
542
Sage Weil2817b002009-10-06 11:31:08 -0700543 if (offset == 0 ||
Yan, Zhengf0494202014-02-27 16:26:24 +0800544 fpos_frag(offset) != fi->frag ||
Sage Weil2817b002009-10-06 11:31:08 -0700545 fpos_off(offset) < fi->offset) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800546 /* discard buffered readdir content on seekdir(0), or
547 * seek to new frag, or seek prior to current chunk */
Sage Weil2817b002009-10-06 11:31:08 -0700548 dout("dir_llseek dropping %p content\n", file);
Yan, Zhengdcd3cc02014-02-28 16:36:09 +0800549 reset_readdir(fi, fpos_frag(offset));
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800550 } else if (fpos_cmp(offset, old_offset) > 0) {
551 /* reset dir_release_count if we did a forward seek */
552 fi->dir_release_count = 0;
553 fi->readdir_cache_idx = -1;
Sage Weil2817b002009-10-06 11:31:08 -0700554 }
Sage Weil2817b002009-10-06 11:31:08 -0700555 }
Josef Bacik06222e42011-07-18 13:21:38 -0400556out:
Al Viro59551022016-01-22 15:40:57 -0500557 inode_unlock(inode);
Sage Weil2817b002009-10-06 11:31:08 -0700558 return retval;
559}
560
561/*
Sage Weil468640e2011-07-26 11:28:11 -0700562 * Handle lookups for the hidden .snap directory.
Sage Weil2817b002009-10-06 11:31:08 -0700563 */
Sage Weil468640e2011-07-26 11:28:11 -0700564int ceph_handle_snapdir(struct ceph_mds_request *req,
565 struct dentry *dentry, int err)
Sage Weil2817b002009-10-06 11:31:08 -0700566{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700567 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000568 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
Sage Weil2817b002009-10-06 11:31:08 -0700569
570 /* .snap dir? */
571 if (err == -ENOENT &&
Sage Weil455cec02011-03-03 13:44:35 -0800572 ceph_snap(parent) == CEPH_NOSNAP &&
Sage Weil6b805182009-10-27 11:50:50 -0700573 strcmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700574 fsc->mount_options->snapdir_name) == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700575 struct inode *inode = ceph_get_snapdir(parent);
Al Viroa4555892014-10-21 20:11:25 -0400576 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
577 dentry, dentry, inode);
Sage Weil9358c6d2010-03-30 13:54:41 -0700578 BUG_ON(!d_unhashed(dentry));
Sage Weil2817b002009-10-06 11:31:08 -0700579 d_add(dentry, inode);
580 err = 0;
581 }
Sage Weil468640e2011-07-26 11:28:11 -0700582 return err;
583}
Sage Weil2817b002009-10-06 11:31:08 -0700584
Sage Weil468640e2011-07-26 11:28:11 -0700585/*
586 * Figure out final result of a lookup/open request.
587 *
588 * Mainly, make sure we return the final req->r_dentry (if it already
589 * existed) in place of the original VFS-provided dentry when they
590 * differ.
591 *
592 * Gracefully handle the case where the MDS replies with -ENOENT and
593 * no trace (which it may do, at its discretion, e.g., if it doesn't
594 * care to issue a lease on the negative dentry).
595 */
596struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
597 struct dentry *dentry, int err)
598{
Sage Weil2817b002009-10-06 11:31:08 -0700599 if (err == -ENOENT) {
600 /* no trace? */
601 err = 0;
602 if (!req->r_reply_info.head->is_dentry) {
603 dout("ENOENT and no trace, dentry %p inode %p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000604 dentry, d_inode(dentry));
605 if (d_really_is_positive(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700606 d_drop(dentry);
607 err = -ENOENT;
608 } else {
609 d_add(dentry, NULL);
610 }
611 }
612 }
613 if (err)
614 dentry = ERR_PTR(err);
615 else if (dentry != req->r_dentry)
616 dentry = dget(req->r_dentry); /* we got spliced */
617 else
618 dentry = NULL;
619 return dentry;
620}
621
Sage Weil1d1de9162009-12-02 11:54:25 -0800622static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
623{
624 return ceph_ino(inode) == CEPH_INO_ROOT &&
625 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
626}
627
Sage Weil2817b002009-10-06 11:31:08 -0700628/*
629 * Look up a single dir entry. If there is a lookup intent, inform
630 * the MDS so that it gets our 'caps wanted' value in a single op.
631 */
632static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400633 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -0700634{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700635 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
636 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700637 struct ceph_mds_request *req;
638 int op;
Yan, Zheng315f2402016-03-07 10:34:50 +0800639 int mask;
Sage Weil2817b002009-10-06 11:31:08 -0700640 int err;
641
Al Viroa4555892014-10-21 20:11:25 -0400642 dout("lookup %p dentry %p '%pd'\n",
643 dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700644
645 if (dentry->d_name.len > NAME_MAX)
646 return ERR_PTR(-ENAMETOOLONG);
647
648 err = ceph_init_dentry(dentry);
649 if (err < 0)
650 return ERR_PTR(err);
651
Sage Weil2817b002009-10-06 11:31:08 -0700652 /* can we conclude ENOENT locally? */
David Howells2b0143b2015-03-17 22:25:59 +0000653 if (d_really_is_negative(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700654 struct ceph_inode_info *ci = ceph_inode(dir);
655 struct ceph_dentry_info *di = ceph_dentry(dentry);
656
Sage Weilbe655592011-11-30 09:47:09 -0800657 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700658 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
659 if (strncmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700660 fsc->mount_options->snapdir_name,
Sage Weil2817b002009-10-06 11:31:08 -0700661 dentry->d_name.len) &&
Sage Weil1d1de9162009-12-02 11:54:25 -0800662 !is_root_ceph_dentry(dir, dentry) &&
Yan, Zhenge2c3de02015-03-04 16:05:04 +0800663 ceph_test_mount_opt(fsc, DCACHE) &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800664 __ceph_dir_is_complete(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700665 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800666 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700667 dout(" dir %p complete, -ENOENT\n", dir);
668 d_add(dentry, NULL);
669 di->lease_shared_gen = ci->i_shared_gen;
670 return NULL;
671 }
Sage Weilbe655592011-11-30 09:47:09 -0800672 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700673 }
674
675 op = ceph_snap(dir) == CEPH_SNAPDIR ?
676 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
677 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
678 if (IS_ERR(req))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200679 return ERR_CAST(req);
Sage Weil2817b002009-10-06 11:31:08 -0700680 req->r_dentry = dget(dentry);
681 req->r_num_caps = 2;
Yan, Zheng315f2402016-03-07 10:34:50 +0800682
683 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
684 if (ceph_security_xattr_wanted(dir))
685 mask |= CEPH_CAP_XATTR_SHARED;
686 req->r_args.getattr.mask = cpu_to_le32(mask);
687
Sage Weil2817b002009-10-06 11:31:08 -0700688 req->r_locked_dir = dir;
689 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil468640e2011-07-26 11:28:11 -0700690 err = ceph_handle_snapdir(req, dentry, err);
Sage Weil2817b002009-10-06 11:31:08 -0700691 dentry = ceph_finish_lookup(req, dentry, err);
692 ceph_mdsc_put_request(req); /* will dput(dentry) */
693 dout("lookup result=%p\n", dentry);
694 return dentry;
695}
696
697/*
698 * If we do a create but get no trace back from the MDS, follow up with
699 * a lookup (the VFS expects us to link up the provided dentry).
700 */
701int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
702{
Al Viro00cd8dd2012-06-10 17:13:09 -0400703 struct dentry *result = ceph_lookup(dir, dentry, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700704
705 if (result && !IS_ERR(result)) {
706 /*
707 * We created the item, then did a lookup, and found
708 * it was already linked to another inode we already
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800709 * had in our cache (and thus got spliced). To not
710 * confuse VFS (especially when inode is a directory),
711 * we don't link our dentry to that inode, return an
712 * error instead.
713 *
714 * This event should be rare and it happens only when
715 * we talk to old MDS. Recent MDS does not send traceless
716 * reply for request that creates new inode.
Sage Weil2817b002009-10-06 11:31:08 -0700717 */
Yan, Zheng5cba3722015-02-02 11:27:56 +0800718 d_drop(result);
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800719 return -ESTALE;
Sage Weil2817b002009-10-06 11:31:08 -0700720 }
721 return PTR_ERR(result);
722}
723
724static int ceph_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400725 umode_t mode, dev_t rdev)
Sage Weil2817b002009-10-06 11:31:08 -0700726{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700727 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
728 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700729 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800730 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700731 int err;
732
733 if (ceph_snap(dir) != CEPH_NOSNAP)
734 return -EROFS;
735
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800736 err = ceph_pre_init_acls(dir, &mode, &acls);
737 if (err < 0)
738 return err;
739
Al Viro1a67aaf2011-07-26 01:52:52 -0400740 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
Sage Weil2817b002009-10-06 11:31:08 -0700741 dir, dentry, mode, rdev);
742 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
743 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800744 err = PTR_ERR(req);
745 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700746 }
747 req->r_dentry = dget(dentry);
748 req->r_num_caps = 2;
749 req->r_locked_dir = dir;
750 req->r_args.mknod.mode = cpu_to_le32(mode);
751 req->r_args.mknod.rdev = cpu_to_le32(rdev);
752 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
753 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800754 if (acls.pagelist) {
755 req->r_pagelist = acls.pagelist;
756 acls.pagelist = NULL;
757 }
Sage Weil2817b002009-10-06 11:31:08 -0700758 err = ceph_mdsc_do_request(mdsc, dir, req);
759 if (!err && !req->r_reply_info.head->is_dentry)
760 err = ceph_handle_notrace_create(dir, dentry);
761 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800762out:
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800763 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000764 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800765 else
Sage Weil2817b002009-10-06 11:31:08 -0700766 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800767 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700768 return err;
769}
770
Al Viro4acdaf22011-07-26 01:42:34 -0400771static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400772 bool excl)
Sage Weil2817b002009-10-06 11:31:08 -0700773{
Miklos Szeredi2d83bde2012-06-05 15:10:25 +0200774 return ceph_mknod(dir, dentry, mode, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700775}
776
777static int ceph_symlink(struct inode *dir, struct dentry *dentry,
778 const char *dest)
779{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700780 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
781 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700782 struct ceph_mds_request *req;
783 int err;
784
785 if (ceph_snap(dir) != CEPH_NOSNAP)
786 return -EROFS;
787
788 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
789 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
790 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800791 err = PTR_ERR(req);
792 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700793 }
Yan, Zheng687265e2015-06-13 17:27:05 +0800794 req->r_path2 = kstrdup(dest, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400795 if (!req->r_path2) {
796 err = -ENOMEM;
797 ceph_mdsc_put_request(req);
798 goto out;
799 }
800 req->r_locked_dir = dir;
Sage Weil2817b002009-10-06 11:31:08 -0700801 req->r_dentry = dget(dentry);
802 req->r_num_caps = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700803 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
804 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
805 err = ceph_mdsc_do_request(mdsc, dir, req);
806 if (!err && !req->r_reply_info.head->is_dentry)
807 err = ceph_handle_notrace_create(dir, dentry);
808 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800809out:
810 if (err)
Sage Weil2817b002009-10-06 11:31:08 -0700811 d_drop(dentry);
812 return err;
813}
814
Al Viro18bb1db2011-07-26 01:41:39 -0400815static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Sage Weil2817b002009-10-06 11:31:08 -0700816{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700817 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
818 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700819 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800820 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700821 int err = -EROFS;
822 int op;
823
824 if (ceph_snap(dir) == CEPH_SNAPDIR) {
825 /* mkdir .snap/foo is a MKSNAP */
826 op = CEPH_MDS_OP_MKSNAP;
Al Viroa4555892014-10-21 20:11:25 -0400827 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
828 dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700829 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
Al Viro18bb1db2011-07-26 01:41:39 -0400830 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
Sage Weil2817b002009-10-06 11:31:08 -0700831 op = CEPH_MDS_OP_MKDIR;
832 } else {
833 goto out;
834 }
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800835
836 mode |= S_IFDIR;
837 err = ceph_pre_init_acls(dir, &mode, &acls);
838 if (err < 0)
839 goto out;
840
Sage Weil2817b002009-10-06 11:31:08 -0700841 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
842 if (IS_ERR(req)) {
843 err = PTR_ERR(req);
844 goto out;
845 }
846
847 req->r_dentry = dget(dentry);
848 req->r_num_caps = 2;
849 req->r_locked_dir = dir;
850 req->r_args.mkdir.mode = cpu_to_le32(mode);
851 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
852 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800853 if (acls.pagelist) {
854 req->r_pagelist = acls.pagelist;
855 acls.pagelist = NULL;
856 }
Sage Weil2817b002009-10-06 11:31:08 -0700857 err = ceph_mdsc_do_request(mdsc, dir, req);
Yan, Zheng275dd192014-12-10 16:17:31 +0800858 if (!err &&
859 !req->r_reply_info.head->is_target &&
860 !req->r_reply_info.head->is_dentry)
Sage Weil2817b002009-10-06 11:31:08 -0700861 err = ceph_handle_notrace_create(dir, dentry);
862 ceph_mdsc_put_request(req);
863out:
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800864 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000865 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800866 else
Sage Weil2817b002009-10-06 11:31:08 -0700867 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800868 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700869 return err;
870}
871
872static int ceph_link(struct dentry *old_dentry, struct inode *dir,
873 struct dentry *dentry)
874{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700875 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
876 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700877 struct ceph_mds_request *req;
878 int err;
879
880 if (ceph_snap(dir) != CEPH_NOSNAP)
881 return -EROFS;
882
883 dout("link in dir %p old_dentry %p dentry %p\n", dir,
884 old_dentry, dentry);
885 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
886 if (IS_ERR(req)) {
887 d_drop(dentry);
888 return PTR_ERR(req);
889 }
890 req->r_dentry = dget(dentry);
891 req->r_num_caps = 2;
Sage Weil4b58c9b2013-02-05 13:41:23 -0800892 req->r_old_dentry = dget(old_dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700893 req->r_locked_dir = dir;
894 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
895 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengad88f232013-07-21 20:25:25 +0800896 /* release LINK_SHARED on source inode (mds will lock it) */
897 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
Sage Weil2817b002009-10-06 11:31:08 -0700898 err = ceph_mdsc_do_request(mdsc, dir, req);
Sage Weil70b666c2011-05-27 09:24:26 -0700899 if (err) {
Sage Weil2817b002009-10-06 11:31:08 -0700900 d_drop(dentry);
Sage Weil70b666c2011-05-27 09:24:26 -0700901 } else if (!req->r_reply_info.head->is_dentry) {
David Howells2b0143b2015-03-17 22:25:59 +0000902 ihold(d_inode(old_dentry));
903 d_instantiate(dentry, d_inode(old_dentry));
Sage Weil70b666c2011-05-27 09:24:26 -0700904 }
Sage Weil2817b002009-10-06 11:31:08 -0700905 ceph_mdsc_put_request(req);
906 return err;
907}
908
909/*
910 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
911 * looks like the link count will hit 0, drop any other caps (other
912 * than PIN) we don't specifically want (due to the file still being
913 * open).
914 */
915static int drop_caps_for_unlink(struct inode *inode)
916{
917 struct ceph_inode_info *ci = ceph_inode(inode);
918 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
919
Sage Weilbe655592011-11-30 09:47:09 -0800920 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700921 if (inode->i_nlink == 1) {
922 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
923 ci->i_ceph_flags |= CEPH_I_NODELAY;
924 }
Sage Weilbe655592011-11-30 09:47:09 -0800925 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700926 return drop;
927}
928
929/*
930 * rmdir and unlink are differ only by the metadata op code
931 */
932static int ceph_unlink(struct inode *dir, struct dentry *dentry)
933{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700934 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
935 struct ceph_mds_client *mdsc = fsc->mdsc;
David Howells2b0143b2015-03-17 22:25:59 +0000936 struct inode *inode = d_inode(dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700937 struct ceph_mds_request *req;
938 int err = -EROFS;
939 int op;
940
941 if (ceph_snap(dir) == CEPH_SNAPDIR) {
942 /* rmdir .snap/foo is RMSNAP */
Al Viroa4555892014-10-21 20:11:25 -0400943 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700944 op = CEPH_MDS_OP_RMSNAP;
945 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
946 dout("unlink/rmdir dir %p dn %p inode %p\n",
947 dir, dentry, inode);
David Howellse36cb0b2015-01-29 12:02:35 +0000948 op = d_is_dir(dentry) ?
Sage Weil2817b002009-10-06 11:31:08 -0700949 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
950 } else
951 goto out;
952 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
953 if (IS_ERR(req)) {
954 err = PTR_ERR(req);
955 goto out;
956 }
957 req->r_dentry = dget(dentry);
958 req->r_num_caps = 2;
959 req->r_locked_dir = dir;
960 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
961 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
962 req->r_inode_drop = drop_caps_for_unlink(inode);
963 err = ceph_mdsc_do_request(mdsc, dir, req);
964 if (!err && !req->r_reply_info.head->is_dentry)
965 d_delete(dentry);
966 ceph_mdsc_put_request(req);
967out:
968 return err;
969}
970
971static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
972 struct inode *new_dir, struct dentry *new_dentry)
973{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700974 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
975 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700976 struct ceph_mds_request *req;
Yan, Zheng0ea611a2015-04-07 15:36:32 +0800977 int op = CEPH_MDS_OP_RENAME;
Sage Weil2817b002009-10-06 11:31:08 -0700978 int err;
979
980 if (ceph_snap(old_dir) != ceph_snap(new_dir))
981 return -EXDEV;
Yan, Zheng0ea611a2015-04-07 15:36:32 +0800982 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
983 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
984 op = CEPH_MDS_OP_RENAMESNAP;
985 else
986 return -EROFS;
987 }
Sage Weil2817b002009-10-06 11:31:08 -0700988 dout("rename dir %p dentry %p to dir %p dentry %p\n",
989 old_dir, old_dentry, new_dir, new_dentry);
Yan, Zheng0ea611a2015-04-07 15:36:32 +0800990 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Sage Weil2817b002009-10-06 11:31:08 -0700991 if (IS_ERR(req))
992 return PTR_ERR(req);
Sage Weil180061a2013-02-05 13:36:05 -0800993 ihold(old_dir);
Sage Weil2817b002009-10-06 11:31:08 -0700994 req->r_dentry = dget(new_dentry);
995 req->r_num_caps = 2;
996 req->r_old_dentry = dget(old_dentry);
Sage Weil180061a2013-02-05 13:36:05 -0800997 req->r_old_dentry_dir = old_dir;
Sage Weil2817b002009-10-06 11:31:08 -0700998 req->r_locked_dir = new_dir;
999 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1000 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1001 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1002 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1003 /* release LINK_RDCACHE on source inode (mds will lock it) */
1004 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
David Howells2b0143b2015-03-17 22:25:59 +00001005 if (d_really_is_positive(new_dentry))
1006 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
Sage Weil2817b002009-10-06 11:31:08 -07001007 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1008 if (!err && !req->r_reply_info.head->is_dentry) {
1009 /*
1010 * Normally d_move() is done by fill_trace (called by
1011 * do_request, above). If there is no trace, we need
1012 * to do it here.
1013 */
Sage Weilea1409f2010-04-28 16:12:06 -07001014
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001015 /* d_move screws up sibling dentries' offsets */
1016 ceph_dir_clear_complete(old_dir);
1017 ceph_dir_clear_complete(new_dir);
1018
Sage Weil2817b002009-10-06 11:31:08 -07001019 d_move(old_dentry, new_dentry);
Sage Weilea1409f2010-04-28 16:12:06 -07001020
1021 /* ensure target dentry is invalidated, despite
1022 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001023 ceph_invalidate_dentry_lease(new_dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001024 }
1025 ceph_mdsc_put_request(req);
1026 return err;
1027}
1028
Sage Weil81a6cf22010-05-14 09:35:38 -07001029/*
1030 * Ensure a dentry lease will no longer revalidate.
1031 */
1032void ceph_invalidate_dentry_lease(struct dentry *dentry)
1033{
1034 spin_lock(&dentry->d_lock);
1035 dentry->d_time = jiffies;
1036 ceph_dentry(dentry)->lease_shared_gen = 0;
1037 spin_unlock(&dentry->d_lock);
1038}
Sage Weil2817b002009-10-06 11:31:08 -07001039
1040/*
1041 * Check if dentry lease is valid. If not, delete the lease. Try to
1042 * renew if the least is more than half up.
1043 */
1044static int dentry_lease_is_valid(struct dentry *dentry)
1045{
1046 struct ceph_dentry_info *di;
1047 struct ceph_mds_session *s;
1048 int valid = 0;
1049 u32 gen;
1050 unsigned long ttl;
1051 struct ceph_mds_session *session = NULL;
1052 struct inode *dir = NULL;
1053 u32 seq = 0;
1054
1055 spin_lock(&dentry->d_lock);
1056 di = ceph_dentry(dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001057 if (di->lease_session) {
Sage Weil2817b002009-10-06 11:31:08 -07001058 s = di->lease_session;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001059 spin_lock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001060 gen = s->s_cap_gen;
1061 ttl = s->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001062 spin_unlock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001063
1064 if (di->lease_gen == gen &&
1065 time_before(jiffies, dentry->d_time) &&
1066 time_before(jiffies, ttl)) {
1067 valid = 1;
1068 if (di->lease_renew_after &&
1069 time_after(jiffies, di->lease_renew_after)) {
1070 /* we should renew */
David Howells2b0143b2015-03-17 22:25:59 +00001071 dir = d_inode(dentry->d_parent);
Sage Weil2817b002009-10-06 11:31:08 -07001072 session = ceph_get_mds_session(s);
1073 seq = di->lease_seq;
1074 di->lease_renew_after = 0;
1075 di->lease_renew_from = jiffies;
1076 }
Sage Weil2817b002009-10-06 11:31:08 -07001077 }
1078 }
1079 spin_unlock(&dentry->d_lock);
1080
1081 if (session) {
1082 ceph_mdsc_lease_send_msg(session, dir, dentry,
1083 CEPH_MDS_LEASE_RENEW, seq);
1084 ceph_put_mds_session(session);
1085 }
1086 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1087 return valid;
1088}
1089
1090/*
1091 * Check if directory-wide content lease/cap is valid.
1092 */
1093static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1094{
1095 struct ceph_inode_info *ci = ceph_inode(dir);
1096 struct ceph_dentry_info *di = ceph_dentry(dentry);
1097 int valid = 0;
1098
Sage Weilbe655592011-11-30 09:47:09 -08001099 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001100 if (ci->i_shared_gen == di->lease_shared_gen)
1101 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
Sage Weilbe655592011-11-30 09:47:09 -08001102 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001103 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1104 dir, (unsigned)ci->i_shared_gen, dentry,
1105 (unsigned)di->lease_shared_gen, valid);
1106 return valid;
1107}
1108
1109/*
1110 * Check if cached dentry can be trusted.
1111 */
Al Viro0b728e12012-06-10 16:03:43 -04001112static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001113{
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001114 int valid = 0;
Yan, Zheng641235d2016-03-16 16:40:23 +08001115 struct dentry *parent;
Nick Piggin34286d62011-01-07 17:49:57 +11001116 struct inode *dir;
1117
Al Viro0b728e12012-06-10 16:03:43 -04001118 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001119 return -ECHILD;
1120
Al Viroa4555892014-10-21 20:11:25 -04001121 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001122 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
Sage Weil2817b002009-10-06 11:31:08 -07001123
Yan, Zheng641235d2016-03-16 16:40:23 +08001124 parent = dget_parent(dentry);
1125 dir = d_inode(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001126
Sage Weil2817b002009-10-06 11:31:08 -07001127 /* always trust cached snapped dentries, snapdir dentry */
1128 if (ceph_snap(dir) != CEPH_NOSNAP) {
Al Viroa4555892014-10-21 20:11:25 -04001129 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001130 dentry, d_inode(dentry));
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001131 valid = 1;
David Howells2b0143b2015-03-17 22:25:59 +00001132 } else if (d_really_is_positive(dentry) &&
1133 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001134 valid = 1;
1135 } else if (dentry_lease_is_valid(dentry) ||
1136 dir_lease_is_valid(dir, dentry)) {
David Howells2b0143b2015-03-17 22:25:59 +00001137 if (d_really_is_positive(dentry))
1138 valid = ceph_is_any_caps(d_inode(dentry));
Yan, Zheng9215aee2013-11-30 12:47:41 +08001139 else
1140 valid = 1;
Sage Weil2817b002009-10-06 11:31:08 -07001141 }
Sage Weil2817b002009-10-06 11:31:08 -07001142
Yan, Zheng200fd272016-03-17 14:41:59 +08001143 if (!valid) {
1144 struct ceph_mds_client *mdsc =
1145 ceph_sb_to_client(dir->i_sb)->mdsc;
1146 struct ceph_mds_request *req;
1147 int op, mask, err;
1148
1149 op = ceph_snap(dir) == CEPH_SNAPDIR ?
1150 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1151 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1152 if (!IS_ERR(req)) {
1153 req->r_dentry = dget(dentry);
1154 req->r_num_caps = 2;
1155
1156 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1157 if (ceph_security_xattr_wanted(dir))
1158 mask |= CEPH_CAP_XATTR_SHARED;
1159 req->r_args.getattr.mask = mask;
1160
1161 req->r_locked_dir = dir;
1162 err = ceph_mdsc_do_request(mdsc, NULL, req);
1163 if (err == 0 || err == -ENOENT) {
1164 if (dentry == req->r_dentry) {
1165 valid = !d_unhashed(dentry);
1166 } else {
1167 d_invalidate(req->r_dentry);
1168 err = -EAGAIN;
1169 }
1170 }
1171 ceph_mdsc_put_request(req);
1172 dout("d_revalidate %p lookup result=%d\n",
1173 dentry, err);
1174 }
1175 }
1176
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001177 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
Yan, Zheng9215aee2013-11-30 12:47:41 +08001178 if (valid) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001179 ceph_dentry_lru_touch(dentry);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001180 } else {
1181 ceph_dir_clear_complete(dir);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001182 }
Yan, Zheng641235d2016-03-16 16:40:23 +08001183
1184 dput(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001185 return valid;
Sage Weil2817b002009-10-06 11:31:08 -07001186}
1187
1188/*
Sage Weil147851d2011-03-15 14:57:41 -07001189 * Release our ceph_dentry_info.
Sage Weil2817b002009-10-06 11:31:08 -07001190 */
Sage Weil147851d2011-03-15 14:57:41 -07001191static void ceph_d_release(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -07001192{
1193 struct ceph_dentry_info *di = ceph_dentry(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001194
Sage Weil147851d2011-03-15 14:57:41 -07001195 dout("d_release %p\n", dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001196 ceph_dentry_lru_del(dentry);
1197 if (di->lease_session)
1198 ceph_put_mds_session(di->lease_session);
1199 kmem_cache_free(ceph_dentry_cachep, di);
1200 dentry->d_fsdata = NULL;
Sage Weil2817b002009-10-06 11:31:08 -07001201}
1202
1203static int ceph_snapdir_d_revalidate(struct dentry *dentry,
Al Viro0b728e12012-06-10 16:03:43 -04001204 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001205{
1206 /*
1207 * Eventually, we'll want to revalidate snapped metadata
1208 * too... probably...
1209 */
1210 return 1;
1211}
1212
Sage Weilb58dc412011-03-15 15:53:40 -07001213/*
1214 * When the VFS prunes a dentry from the cache, we need to clear the
1215 * complete flag on the parent directory.
1216 *
1217 * Called under dentry->d_lock.
1218 */
1219static void ceph_d_prune(struct dentry *dentry)
1220{
Sage Weil774ac212011-11-11 09:48:08 -08001221 dout("ceph_d_prune %p\n", dentry);
Sage Weilb58dc412011-03-15 15:53:40 -07001222
1223 /* do we have a valid parent? */
Sage Weil8842b3b2012-06-07 13:43:35 -07001224 if (IS_ROOT(dentry))
Sage Weilb58dc412011-03-15 15:53:40 -07001225 return;
1226
Yan, Zheng2f276c52013-03-13 19:44:32 +08001227 /* if we are not hashed, we don't affect dir's completeness */
Sage Weilb58dc412011-03-15 15:53:40 -07001228 if (d_unhashed(dentry))
1229 return;
1230
1231 /*
1232 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1233 * cleared until d_release
1234 */
David Howells2b0143b2015-03-17 22:25:59 +00001235 ceph_dir_clear_complete(d_inode(dentry->d_parent));
Sage Weilb58dc412011-03-15 15:53:40 -07001236}
Sage Weil2817b002009-10-06 11:31:08 -07001237
1238/*
1239 * read() on a dir. This weird interface hack only works if mounted
1240 * with '-o dirstat'.
1241 */
1242static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1243 loff_t *ppos)
1244{
1245 struct ceph_file_info *cf = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05001246 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -07001247 struct ceph_inode_info *ci = ceph_inode(inode);
1248 int left;
Sage Weilae598082011-05-12 14:28:05 -07001249 const int bufsize = 1024;
Sage Weil2817b002009-10-06 11:31:08 -07001250
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001251 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
Sage Weil2817b002009-10-06 11:31:08 -07001252 return -EISDIR;
1253
1254 if (!cf->dir_info) {
Yan, Zheng687265e2015-06-13 17:27:05 +08001255 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -07001256 if (!cf->dir_info)
1257 return -ENOMEM;
1258 cf->dir_info_len =
Sage Weilae598082011-05-12 14:28:05 -07001259 snprintf(cf->dir_info, bufsize,
Sage Weil2817b002009-10-06 11:31:08 -07001260 "entries: %20lld\n"
1261 " files: %20lld\n"
1262 " subdirs: %20lld\n"
1263 "rentries: %20lld\n"
1264 " rfiles: %20lld\n"
1265 " rsubdirs: %20lld\n"
1266 "rbytes: %20lld\n"
1267 "rctime: %10ld.%09ld\n",
1268 ci->i_files + ci->i_subdirs,
1269 ci->i_files,
1270 ci->i_subdirs,
1271 ci->i_rfiles + ci->i_rsubdirs,
1272 ci->i_rfiles,
1273 ci->i_rsubdirs,
1274 ci->i_rbytes,
1275 (long)ci->i_rctime.tv_sec,
1276 (long)ci->i_rctime.tv_nsec);
1277 }
1278
1279 if (*ppos >= cf->dir_info_len)
1280 return 0;
1281 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1282 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1283 if (left == size)
1284 return -EFAULT;
1285 *ppos += (size - left);
1286 return size - left;
1287}
1288
1289/*
Sage Weil2817b002009-10-06 11:31:08 -07001290 * We maintain a private dentry LRU.
1291 *
1292 * FIXME: this needs to be changed to a per-mds lru to be useful.
1293 */
1294void ceph_dentry_lru_add(struct dentry *dn)
1295{
1296 struct ceph_dentry_info *di = ceph_dentry(dn);
1297 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001298
Al Viroa4555892014-10-21 20:11:25 -04001299 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001300 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1301 spin_lock(&mdsc->dentry_lru_lock);
1302 list_add_tail(&di->lru, &mdsc->dentry_lru);
1303 mdsc->num_dentry++;
1304 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001305}
1306
1307void ceph_dentry_lru_touch(struct dentry *dn)
1308{
1309 struct ceph_dentry_info *di = ceph_dentry(dn);
1310 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001311
Al Viroa4555892014-10-21 20:11:25 -04001312 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1313 di->offset);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001314 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1315 spin_lock(&mdsc->dentry_lru_lock);
1316 list_move_tail(&di->lru, &mdsc->dentry_lru);
1317 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001318}
1319
1320void ceph_dentry_lru_del(struct dentry *dn)
1321{
1322 struct ceph_dentry_info *di = ceph_dentry(dn);
1323 struct ceph_mds_client *mdsc;
1324
Al Viroa4555892014-10-21 20:11:25 -04001325 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001326 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1327 spin_lock(&mdsc->dentry_lru_lock);
1328 list_del_init(&di->lru);
1329 mdsc->num_dentry--;
1330 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001331}
1332
Sage Weil6c0f3af2010-11-16 11:14:34 -08001333/*
1334 * Return name hash for a given dentry. This is dependent on
1335 * the parent directory's hash function.
1336 */
Sage Weile5f86dc2011-07-26 11:30:55 -07001337unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
Sage Weil6c0f3af2010-11-16 11:14:34 -08001338{
Sage Weil6c0f3af2010-11-16 11:14:34 -08001339 struct ceph_inode_info *dci = ceph_inode(dir);
1340
1341 switch (dci->i_dir_layout.dl_dir_hash) {
1342 case 0: /* for backward compat */
1343 case CEPH_STR_HASH_LINUX:
1344 return dn->d_name.hash;
1345
1346 default:
1347 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1348 dn->d_name.name, dn->d_name.len);
1349 }
1350}
1351
Sage Weil2817b002009-10-06 11:31:08 -07001352const struct file_operations ceph_dir_fops = {
1353 .read = ceph_read_dir,
Al Viro77acfa22013-05-17 16:52:26 -04001354 .iterate = ceph_readdir,
Sage Weil2817b002009-10-06 11:31:08 -07001355 .llseek = ceph_dir_llseek,
1356 .open = ceph_open,
1357 .release = ceph_release,
1358 .unlocked_ioctl = ceph_ioctl,
Yan, Zhengda819c82015-05-27 11:19:34 +08001359 .fsync = ceph_fsync,
Sage Weil2817b002009-10-06 11:31:08 -07001360};
1361
Yan, Zheng38c48b52015-01-14 13:46:04 +08001362const struct file_operations ceph_snapdir_fops = {
1363 .iterate = ceph_readdir,
1364 .llseek = ceph_dir_llseek,
1365 .open = ceph_open,
1366 .release = ceph_release,
1367};
1368
Sage Weil2817b002009-10-06 11:31:08 -07001369const struct inode_operations ceph_dir_iops = {
1370 .lookup = ceph_lookup,
1371 .permission = ceph_permission,
1372 .getattr = ceph_getattr,
1373 .setattr = ceph_setattr,
1374 .setxattr = ceph_setxattr,
1375 .getxattr = ceph_getxattr,
1376 .listxattr = ceph_listxattr,
1377 .removexattr = ceph_removexattr,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001378 .get_acl = ceph_get_acl,
Sage Weil72466d02014-01-29 06:22:25 -08001379 .set_acl = ceph_set_acl,
Sage Weil2817b002009-10-06 11:31:08 -07001380 .mknod = ceph_mknod,
1381 .symlink = ceph_symlink,
1382 .mkdir = ceph_mkdir,
1383 .link = ceph_link,
1384 .unlink = ceph_unlink,
1385 .rmdir = ceph_unlink,
1386 .rename = ceph_rename,
1387 .create = ceph_create,
Miklos Szeredi2d83bde2012-06-05 15:10:25 +02001388 .atomic_open = ceph_atomic_open,
Sage Weil2817b002009-10-06 11:31:08 -07001389};
1390
Yan, Zheng38c48b52015-01-14 13:46:04 +08001391const struct inode_operations ceph_snapdir_iops = {
1392 .lookup = ceph_lookup,
1393 .permission = ceph_permission,
1394 .getattr = ceph_getattr,
1395 .mkdir = ceph_mkdir,
1396 .rmdir = ceph_unlink,
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001397 .rename = ceph_rename,
Yan, Zheng38c48b52015-01-14 13:46:04 +08001398};
1399
Sage Weil52dfb8a2010-08-03 10:25:30 -07001400const struct dentry_operations ceph_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001401 .d_revalidate = ceph_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001402 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001403 .d_prune = ceph_d_prune,
Sage Weil2817b002009-10-06 11:31:08 -07001404};
1405
Sage Weil52dfb8a2010-08-03 10:25:30 -07001406const struct dentry_operations ceph_snapdir_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001407 .d_revalidate = ceph_snapdir_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001408 .d_release = ceph_d_release,
Sage Weil2817b002009-10-06 11:31:08 -07001409};
1410
Sage Weil52dfb8a2010-08-03 10:25:30 -07001411const struct dentry_operations ceph_snap_dentry_ops = {
Sage Weil147851d2011-03-15 14:57:41 -07001412 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001413 .d_prune = ceph_d_prune,
Sage Weil2817b002009-10-06 11:31:08 -07001414};