blob: 019c2036d36f3e15b86a4da732b5cfd862e4670a [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2817b002009-10-06 11:31:08 -07002
3#include <linux/spinlock.h>
4#include <linux/fs_struct.h>
5#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Sage Weil2817b002009-10-06 11:31:08 -07007#include <linux/sched.h>
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02008#include <linux/xattr.h>
Sage Weil2817b002009-10-06 11:31:08 -07009
10#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070011#include "mds_client.h"
Sage Weil2817b002009-10-06 11:31:08 -070012
13/*
14 * Directory operations: readdir, lookup, create, link, unlink,
15 * rename, etc.
16 */
17
18/*
19 * Ceph MDS operations are specified in terms of a base ino and
20 * relative path. Thus, the client can specify an operation on a
21 * specific inode (e.g., a getattr due to fstat(2)), or as a path
22 * relative to, say, the root directory.
23 *
24 * Normally, we limit ourselves to strict inode ops (no path component)
25 * or dentry operations (a single path component relative to an ino). The
26 * exception to this is open_root_dentry(), which will open the mount
27 * point by name.
28 */
29
Sage Weil52dfb8a2010-08-03 10:25:30 -070030const struct dentry_operations ceph_dentry_ops;
Sage Weil2817b002009-10-06 11:31:08 -070031
32/*
33 * Initialize ceph dentry state.
34 */
Al Viroad5cb122016-10-28 22:05:13 -040035static int ceph_d_init(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -070036{
37 struct ceph_dentry_info *di;
38
Geliang Tang99ec2692016-03-13 15:26:29 +080039 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -070040 if (!di)
41 return -ENOMEM; /* oh well */
42
Sage Weil2817b002009-10-06 11:31:08 -070043 di->dentry = dentry;
44 di->lease_session = NULL;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +020045 di->time = jiffies;
Sage Weil48d0cbd2011-07-26 11:30:15 -070046 dentry->d_fsdata = di;
Sage Weil2817b002009-10-06 11:31:08 -070047 ceph_dentry_lru_add(dentry);
Sage Weil2817b002009-10-06 11:31:08 -070048 return 0;
49}
50
Sage Weil2817b002009-10-06 11:31:08 -070051/*
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080052 * for f_pos for readdir:
53 * - hash order:
54 * (0xff << 52) | ((24 bits hash) << 28) |
55 * (the nth entry has hash collision);
56 * - frag+name order;
57 * ((frag value) << 28) | (the nth entry in frag);
Sage Weil2817b002009-10-06 11:31:08 -070058 */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080059#define OFFSET_BITS 28
60#define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
61#define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
62loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
63{
64 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
65 if (hash_order)
66 fpos |= HASH_ORDER;
67 return fpos;
68}
69
70static bool is_hash_order(loff_t p)
71{
72 return (p & HASH_ORDER) == HASH_ORDER;
73}
74
Sage Weil2817b002009-10-06 11:31:08 -070075static unsigned fpos_frag(loff_t p)
76{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080077 return p >> OFFSET_BITS;
Sage Weil2817b002009-10-06 11:31:08 -070078}
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080079
80static unsigned fpos_hash(loff_t p)
81{
82 return ceph_frag_value(fpos_frag(p));
83}
84
Sage Weil2817b002009-10-06 11:31:08 -070085static unsigned fpos_off(loff_t p)
86{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080087 return p & OFFSET_MASK;
Sage Weil2817b002009-10-06 11:31:08 -070088}
89
Yan, Zheng4d5f5df2014-02-13 19:40:26 +080090static int fpos_cmp(loff_t l, loff_t r)
91{
92 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
93 if (v)
94 return v;
95 return (int)(fpos_off(l) - fpos_off(r));
96}
97
Sage Weil2817b002009-10-06 11:31:08 -070098/*
Yan, Zhengfdd4e152015-06-16 20:48:56 +080099 * make note of the last dentry we read, so we can
100 * continue at the same lexicographical point,
101 * regardless of what dir changes take place on the
102 * server.
103 */
104static int note_last_dentry(struct ceph_file_info *fi, const char *name,
105 int len, unsigned next_offset)
106{
107 char *buf = kmalloc(len+1, GFP_KERNEL);
108 if (!buf)
109 return -ENOMEM;
110 kfree(fi->last_name);
111 fi->last_name = buf;
112 memcpy(fi->last_name, name, len);
113 fi->last_name[len] = 0;
114 fi->next_offset = next_offset;
115 dout("note_last_dentry '%s'\n", fi->last_name);
116 return 0;
117}
118
Yan, Zhengc530cd22016-04-28 17:43:35 +0800119
120static struct dentry *
121__dcache_find_get_entry(struct dentry *parent, u64 idx,
122 struct ceph_readdir_cache_control *cache_ctl)
123{
124 struct inode *dir = d_inode(parent);
125 struct dentry *dentry;
126 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
127 loff_t ptr_pos = idx * sizeof(struct dentry *);
128 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
129
130 if (ptr_pos >= i_size_read(dir))
131 return NULL;
132
133 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
134 ceph_readdir_cache_release(cache_ctl);
135 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
136 if (!cache_ctl->page) {
137 dout(" page %lu not found\n", ptr_pgoff);
138 return ERR_PTR(-EAGAIN);
139 }
140 /* reading/filling the cache are serialized by
141 i_mutex, no need to use page lock */
142 unlock_page(cache_ctl->page);
143 cache_ctl->dentries = kmap(cache_ctl->page);
144 }
145
146 cache_ctl->index = idx & idx_mask;
147
148 rcu_read_lock();
149 spin_lock(&parent->d_lock);
150 /* check i_size again here, because empty directory can be
151 * marked as complete while not holding the i_mutex. */
152 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
153 dentry = cache_ctl->dentries[cache_ctl->index];
154 else
155 dentry = NULL;
156 spin_unlock(&parent->d_lock);
157 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
158 dentry = NULL;
159 rcu_read_unlock();
160 return dentry ? : ERR_PTR(-EAGAIN);
161}
162
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800163/*
Sage Weil2817b002009-10-06 11:31:08 -0700164 * When possible, we try to satisfy a readdir by peeking at the
165 * dcache. We make this work by carefully ordering dentries on
Al Viro946e51f2014-10-26 19:19:16 -0400166 * d_child when we initially get results back from the MDS, and
Sage Weil2817b002009-10-06 11:31:08 -0700167 * falling back to a "normal" sync readdir if any dentries in the dir
168 * are dropped.
169 *
Yan, Zheng2f276c52013-03-13 19:44:32 +0800170 * Complete dir indicates that we have all dentries in the dir. It is
Sage Weil2817b002009-10-06 11:31:08 -0700171 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
172 * the MDS if/when the directory is modified).
173 */
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800174static int __dcache_readdir(struct file *file, struct dir_context *ctx,
175 u32 shared_gen)
Sage Weil2817b002009-10-06 11:31:08 -0700176{
Al Viro77acfa22013-05-17 16:52:26 -0400177 struct ceph_file_info *fi = file->private_data;
Al Virob5830432014-10-31 01:22:04 -0400178 struct dentry *parent = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000179 struct inode *dir = d_inode(parent);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800180 struct dentry *dentry, *last = NULL;
Sage Weil2817b002009-10-06 11:31:08 -0700181 struct ceph_dentry_info *di;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800182 struct ceph_readdir_cache_control cache_ctl = {};
Yan, Zhengc530cd22016-04-28 17:43:35 +0800183 u64 idx = 0;
184 int err = 0;
Sage Weil2817b002009-10-06 11:31:08 -0700185
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800186 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
Sage Weil2817b002009-10-06 11:31:08 -0700187
Yan, Zhengc530cd22016-04-28 17:43:35 +0800188 /* search start position */
189 if (ctx->pos > 2) {
190 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
191 while (count > 0) {
192 u64 step = count >> 1;
193 dentry = __dcache_find_get_entry(parent, idx + step,
194 &cache_ctl);
195 if (!dentry) {
196 /* use linar search */
197 idx = 0;
198 break;
199 }
200 if (IS_ERR(dentry)) {
201 err = PTR_ERR(dentry);
202 goto out;
203 }
204 di = ceph_dentry(dentry);
205 spin_lock(&dentry->d_lock);
206 if (fpos_cmp(di->offset, ctx->pos) < 0) {
207 idx += step + 1;
208 count -= step + 1;
209 } else {
210 count = step;
211 }
212 spin_unlock(&dentry->d_lock);
213 dput(dentry);
214 }
215
216 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
Sage Weil2817b002009-10-06 11:31:08 -0700217 }
218
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800219
Yan, Zhengc530cd22016-04-28 17:43:35 +0800220 for (;;) {
221 bool emit_dentry = false;
222 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
223 if (!dentry) {
Sage Weil9cfa1092011-07-26 11:26:18 -0700224 fi->flags |= CEPH_F_ATEND;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800225 err = 0;
226 break;
Sage Weil2817b002009-10-06 11:31:08 -0700227 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800228 if (IS_ERR(dentry)) {
229 err = PTR_ERR(dentry);
230 goto out;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800231 }
232
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800233 di = ceph_dentry(dentry);
234 spin_lock(&dentry->d_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800235 if (di->lease_shared_gen == shared_gen &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800236 d_really_is_positive(dentry) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800237 fpos_cmp(ctx->pos, di->offset) <= 0) {
238 emit_dentry = true;
Sage Weil2817b002009-10-06 11:31:08 -0700239 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800240 spin_unlock(&dentry->d_lock);
241
242 if (emit_dentry) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800243 dout(" %llx dentry %p %pd %p\n", di->offset,
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800244 dentry, dentry, d_inode(dentry));
245 ctx->pos = di->offset;
246 if (!dir_emit(ctx, dentry->d_name.name,
247 dentry->d_name.len,
248 ceph_translate_ino(dentry->d_sb,
249 d_inode(dentry)->i_ino),
250 d_inode(dentry)->i_mode >> 12)) {
251 dput(dentry);
252 err = 0;
253 break;
254 }
255 ctx->pos++;
256
257 if (last)
258 dput(last);
259 last = dentry;
260 } else {
261 dput(dentry);
262 }
Sage Weil2817b002009-10-06 11:31:08 -0700263 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800264out:
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800265 ceph_readdir_cache_release(&cache_ctl);
266 if (last) {
267 int ret;
268 di = ceph_dentry(last);
269 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
270 fpos_off(di->offset) + 1);
271 if (ret < 0)
272 err = ret;
Al Viro77acfa22013-05-17 16:52:26 -0400273 dput(last);
Yan, Zheng84583cfb2017-07-06 11:12:21 +0800274 /* last_name no longer match cache index */
275 if (fi->readdir_cache_idx >= 0) {
276 fi->readdir_cache_idx = -1;
277 fi->dir_release_count = 0;
278 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800279 }
Sage Weil2817b002009-10-06 11:31:08 -0700280 return err;
281}
282
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800283static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
284{
285 if (!fi->last_readdir)
286 return true;
287 if (is_hash_order(pos))
288 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
289 else
290 return fi->frag != fpos_frag(pos);
291}
292
Al Viro77acfa22013-05-17 16:52:26 -0400293static int ceph_readdir(struct file *file, struct dir_context *ctx)
Sage Weil2817b002009-10-06 11:31:08 -0700294{
Al Viro77acfa22013-05-17 16:52:26 -0400295 struct ceph_file_info *fi = file->private_data;
296 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -0700297 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700298 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
299 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800300 int i;
Sage Weil2817b002009-10-06 11:31:08 -0700301 int err;
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800302 unsigned frag = -1;
Sage Weil2817b002009-10-06 11:31:08 -0700303 struct ceph_mds_reply_info_parsed *rinfo;
Sage Weil2817b002009-10-06 11:31:08 -0700304
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800305 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
Sage Weil9cfa1092011-07-26 11:26:18 -0700306 if (fi->flags & CEPH_F_ATEND)
Sage Weil2817b002009-10-06 11:31:08 -0700307 return 0;
308
309 /* always start with . and .. */
Al Viro77acfa22013-05-17 16:52:26 -0400310 if (ctx->pos == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700311 dout("readdir off 0 -> '.'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400312 if (!dir_emit(ctx, ".", 1,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800313 ceph_translate_ino(inode->i_sb, inode->i_ino),
Al Viro77acfa22013-05-17 16:52:26 -0400314 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700315 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400316 ctx->pos = 1;
Sage Weil2817b002009-10-06 11:31:08 -0700317 }
Al Viro77acfa22013-05-17 16:52:26 -0400318 if (ctx->pos == 1) {
Al Virob5830432014-10-31 01:22:04 -0400319 ino_t ino = parent_ino(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700320 dout("readdir off 1 -> '..'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400321 if (!dir_emit(ctx, "..", 2,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800322 ceph_translate_ino(inode->i_sb, ino),
Al Viro77acfa22013-05-17 16:52:26 -0400323 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700324 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400325 ctx->pos = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700326 }
327
328 /* can we use the dcache? */
Sage Weilbe655592011-11-30 09:47:09 -0800329 spin_lock(&ci->i_ceph_lock);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800330 if (ceph_test_mount_opt(fsc, DCACHE) &&
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700331 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
Sage Weila0dff782010-07-22 13:47:21 -0700332 ceph_snap(inode) != CEPH_SNAPDIR &&
Yan, Zheng70db4f32014-10-21 18:09:56 -0700333 __ceph_dir_is_complete_ordered(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700334 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800335 u32 shared_gen = ci->i_shared_gen;
Sage Weilbe655592011-11-30 09:47:09 -0800336 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800337 err = __dcache_readdir(file, ctx, shared_gen);
Sage Weilefa4c122010-10-18 14:04:31 -0700338 if (err != -EAGAIN)
Sage Weil2817b002009-10-06 11:31:08 -0700339 return err;
Sage Weilefa4c122010-10-18 14:04:31 -0700340 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800341 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700342 }
Sage Weil2817b002009-10-06 11:31:08 -0700343
344 /* proceed with a normal readdir */
Sage Weil2817b002009-10-06 11:31:08 -0700345more:
346 /* do we have the correct frag content buffered? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800347 if (need_send_readdir(fi, ctx->pos)) {
Sage Weil2817b002009-10-06 11:31:08 -0700348 struct ceph_mds_request *req;
349 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
350 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
351
352 /* discard old result, if any */
Sage Weil393f6622010-03-10 12:03:32 -0800353 if (fi->last_readdir) {
Sage Weil2817b002009-10-06 11:31:08 -0700354 ceph_mdsc_put_request(fi->last_readdir);
Sage Weil393f6622010-03-10 12:03:32 -0800355 fi->last_readdir = NULL;
356 }
Sage Weil2817b002009-10-06 11:31:08 -0700357
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800358 if (is_hash_order(ctx->pos)) {
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800359 /* fragtree isn't always accurate. choose frag
360 * based on previous reply when possible. */
361 if (frag == (unsigned)-1)
362 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
363 NULL, NULL);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800364 } else {
365 frag = fpos_frag(ctx->pos);
366 }
367
Sage Weil2817b002009-10-06 11:31:08 -0700368 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
369 ceph_vinop(inode), frag, fi->last_name);
370 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
371 if (IS_ERR(req))
372 return PTR_ERR(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800373 err = ceph_alloc_readdir_reply_buffer(req, inode);
374 if (err) {
375 ceph_mdsc_put_request(req);
376 return err;
377 }
Sage Weil2817b002009-10-06 11:31:08 -0700378 /* hints to request -> mds selection code */
379 req->r_direct_mode = USE_AUTH_MDS;
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800380 if (op == CEPH_MDS_OP_READDIR) {
381 req->r_direct_hash = ceph_frag_value(frag);
382 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
383 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400384 if (fi->last_name) {
Yan, Zheng687265e2015-06-13 17:27:05 +0800385 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400386 if (!req->r_path2) {
387 ceph_mdsc_put_request(req);
388 return -ENOMEM;
389 }
Yan, Zheng79162542017-04-05 12:54:05 -0400390 } else if (is_hash_order(ctx->pos)) {
391 req->r_args.readdir.offset_hash =
392 cpu_to_le32(fpos_hash(ctx->pos));
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400393 }
Yan, Zheng79162542017-04-05 12:54:05 -0400394
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800395 req->r_dir_release_cnt = fi->dir_release_count;
396 req->r_dir_ordered_cnt = fi->dir_ordered_count;
397 req->r_readdir_cache_idx = fi->readdir_cache_idx;
Sage Weil2817b002009-10-06 11:31:08 -0700398 req->r_readdir_offset = fi->next_offset;
399 req->r_args.readdir.frag = cpu_to_le32(frag);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800400 req->r_args.readdir.flags =
401 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400402
403 req->r_inode = inode;
404 ihold(inode);
405 req->r_dentry = dget(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700406 err = ceph_mdsc_do_request(mdsc, NULL, req);
407 if (err < 0) {
408 ceph_mdsc_put_request(req);
409 return err;
410 }
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800411 dout("readdir got and parsed readdir result=%d on "
412 "frag %x, end=%d, complete=%d, hash_order=%d\n",
413 err, frag,
Sage Weil2817b002009-10-06 11:31:08 -0700414 (int)req->r_reply_info.dir_end,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800415 (int)req->r_reply_info.dir_complete,
416 (int)req->r_reply_info.hash_order);
Sage Weil2817b002009-10-06 11:31:08 -0700417
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800418 rinfo = &req->r_reply_info;
419 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
420 frag = le32_to_cpu(rinfo->dir_dir->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800421 if (!rinfo->hash_order) {
422 fi->next_offset = req->r_readdir_offset;
423 /* adjust ctx->pos to beginning of frag */
424 ctx->pos = ceph_make_fpos(frag,
425 fi->next_offset,
426 false);
427 }
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800428 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800429
Yan, Zhengf0494202014-02-27 16:26:24 +0800430 fi->frag = frag;
Sage Weil2817b002009-10-06 11:31:08 -0700431 fi->last_readdir = req;
432
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500433 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800434 fi->readdir_cache_idx = req->r_readdir_cache_idx;
435 if (fi->readdir_cache_idx < 0) {
436 /* preclude from marking dir ordered */
437 fi->dir_ordered_count = 0;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800438 } else if (ceph_frag_is_leftmost(frag) &&
439 fi->next_offset == 2) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800440 /* note dir version at start of readdir so
441 * we can tell if any dentries get dropped */
442 fi->dir_release_count = req->r_dir_release_cnt;
443 fi->dir_ordered_count = req->r_dir_ordered_cnt;
444 }
445 } else {
446 dout("readdir !did_prepopulate");
447 /* disable readdir cache */
448 fi->readdir_cache_idx = -1;
449 /* preclude from marking dir complete */
450 fi->dir_release_count = 0;
451 }
452
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800453 /* note next offset and last dentry name */
454 if (rinfo->dir_nr > 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800455 struct ceph_mds_reply_dir_entry *rde =
456 rinfo->dir_entries + (rinfo->dir_nr-1);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800457 unsigned next_offset = req->r_reply_info.dir_end ?
458 2 : (fpos_off(rde->offset) + 1);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800459 err = note_last_dentry(fi, rde->name, rde->name_len,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800460 next_offset);
Sage Weil2817b002009-10-06 11:31:08 -0700461 if (err)
462 return err;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800463 } else if (req->r_reply_info.dir_end) {
464 fi->next_offset = 2;
465 /* keep last name */
Sage Weil2817b002009-10-06 11:31:08 -0700466 }
467 }
468
469 rinfo = &fi->last_readdir->r_reply_info;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800470 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800471 fi->frag, rinfo->dir_nr, ctx->pos,
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800472 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
Al Viro77acfa22013-05-17 16:52:26 -0400473
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800474 i = 0;
475 /* search start position */
476 if (rinfo->dir_nr > 0) {
477 int step, nr = rinfo->dir_nr;
478 while (nr > 0) {
479 step = nr >> 1;
480 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
481 i += step + 1;
482 nr -= step + 1;
483 } else {
484 nr = step;
485 }
486 }
487 }
488 for (; i < rinfo->dir_nr; i++) {
489 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
Sage Weil3105c192010-11-18 09:15:07 -0800490 struct ceph_vino vino;
491 ino_t ino;
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800492 u32 ftype;
Sage Weil3105c192010-11-18 09:15:07 -0800493
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800494 BUG_ON(rde->offset < ctx->pos);
495
496 ctx->pos = rde->offset;
497 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
498 i, rinfo->dir_nr, ctx->pos,
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800499 rde->name_len, rde->name, &rde->inode.in);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800500
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800501 BUG_ON(!rde->inode.in);
502 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
503 vino.ino = le64_to_cpu(rde->inode.in->ino);
504 vino.snap = le64_to_cpu(rde->inode.in->snapid);
Sage Weil3105c192010-11-18 09:15:07 -0800505 ino = ceph_vino_to_ino(vino);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800506
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800507 if (!dir_emit(ctx, rde->name, rde->name_len,
508 ceph_translate_ino(inode->i_sb, ino), ftype)) {
Sage Weil2817b002009-10-06 11:31:08 -0700509 dout("filldir stopping us...\n");
510 return 0;
511 }
Al Viro77acfa22013-05-17 16:52:26 -0400512 ctx->pos++;
Sage Weil2817b002009-10-06 11:31:08 -0700513 }
514
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800515 ceph_mdsc_put_request(fi->last_readdir);
516 fi->last_readdir = NULL;
517
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800518 if (fi->next_offset > 2) {
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800519 frag = fi->frag;
Sage Weil2817b002009-10-06 11:31:08 -0700520 goto more;
521 }
522
523 /* more frags? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800524 if (!ceph_frag_is_rightmost(fi->frag)) {
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800525 frag = ceph_frag_next(fi->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800526 if (is_hash_order(ctx->pos)) {
527 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
528 fi->next_offset, true);
529 if (new_pos > ctx->pos)
530 ctx->pos = new_pos;
531 /* keep last_name */
532 } else {
533 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
534 kfree(fi->last_name);
535 fi->last_name = NULL;
536 }
Sage Weil2817b002009-10-06 11:31:08 -0700537 dout("readdir next frag is %x\n", frag);
538 goto more;
539 }
Sage Weil9cfa1092011-07-26 11:26:18 -0700540 fi->flags |= CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700541
542 /*
543 * if dir_release_count still matches the dir, no dentries
544 * were released during the whole readdir, and we should have
545 * the complete dir contents in our cache.
546 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800547 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
548 spin_lock(&ci->i_ceph_lock);
549 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700550 dout(" marking %p complete and ordered\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800551 /* use i_size to track number of entries in
552 * readdir cache */
553 BUG_ON(fi->readdir_cache_idx < 0);
554 i_size_write(inode, fi->readdir_cache_idx *
555 sizeof(struct dentry*));
556 } else {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700557 dout(" marking %p complete\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800558 }
Yan, Zheng70db4f32014-10-21 18:09:56 -0700559 __ceph_dir_set_complete(ci, fi->dir_release_count,
560 fi->dir_ordered_count);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800561 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700562 }
Sage Weil2817b002009-10-06 11:31:08 -0700563
Al Viro77acfa22013-05-17 16:52:26 -0400564 dout("readdir %p file %p done.\n", inode, file);
Sage Weil2817b002009-10-06 11:31:08 -0700565 return 0;
566}
567
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800568static void reset_readdir(struct ceph_file_info *fi)
Sage Weil2817b002009-10-06 11:31:08 -0700569{
570 if (fi->last_readdir) {
571 ceph_mdsc_put_request(fi->last_readdir);
572 fi->last_readdir = NULL;
573 }
574 kfree(fi->last_name);
Sage Weila1629c32010-11-11 15:24:06 -0800575 fi->last_name = NULL;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800576 fi->dir_release_count = 0;
577 fi->readdir_cache_idx = -1;
Yan, Zhenga78600e2016-04-27 17:32:34 +0800578 fi->next_offset = 2; /* compensate for . and .. */
Sage Weil9cfa1092011-07-26 11:26:18 -0700579 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700580}
581
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800582/*
583 * discard buffered readdir content on seekdir(0), or seek to new frag,
584 * or seek prior to current chunk
585 */
586static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
587{
588 struct ceph_mds_reply_info_parsed *rinfo;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800589 loff_t chunk_offset;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800590 if (new_pos == 0)
591 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800592 if (is_hash_order(new_pos)) {
593 /* no need to reset last_name for a forward seek when
594 * dentries are sotred in hash order */
Nicolas Iooss0f5aa882016-08-28 18:47:12 +0200595 } else if (fi->frag != fpos_frag(new_pos)) {
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800596 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800597 }
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800598 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
599 if (!rinfo || !rinfo->dir_nr)
600 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800601 chunk_offset = rinfo->dir_entries[0].offset;
602 return new_pos < chunk_offset ||
603 is_hash_order(new_pos) != is_hash_order(chunk_offset);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800604}
605
Andrew Morton965c8e52012-12-17 15:59:39 -0800606static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
Sage Weil2817b002009-10-06 11:31:08 -0700607{
608 struct ceph_file_info *fi = file->private_data;
609 struct inode *inode = file->f_mapping->host;
Sage Weil2817b002009-10-06 11:31:08 -0700610 loff_t retval;
611
Al Viro59551022016-01-22 15:40:57 -0500612 inode_lock(inode);
Josef Bacik06222e42011-07-18 13:21:38 -0400613 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800614 switch (whence) {
Sage Weil2817b002009-10-06 11:31:08 -0700615 case SEEK_CUR:
616 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -0400617 case SEEK_SET:
618 break;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800619 case SEEK_END:
620 retval = -EOPNOTSUPP;
Josef Bacik06222e42011-07-18 13:21:38 -0400621 default:
622 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700623 }
Josef Bacik06222e42011-07-18 13:21:38 -0400624
Yan, Zhengf0494202014-02-27 16:26:24 +0800625 if (offset >= 0) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800626 if (need_reset_readdir(fi, offset)) {
627 dout("dir_llseek dropping %p content\n", file);
628 reset_readdir(fi);
629 } else if (is_hash_order(offset) && offset > file->f_pos) {
630 /* for hash offset, we don't know if a forward seek
631 * is within same frag */
632 fi->dir_release_count = 0;
633 fi->readdir_cache_idx = -1;
634 }
635
Sage Weil2817b002009-10-06 11:31:08 -0700636 if (offset != file->f_pos) {
637 file->f_pos = offset;
638 file->f_version = 0;
Sage Weil9cfa1092011-07-26 11:26:18 -0700639 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700640 }
641 retval = offset;
Sage Weil2817b002009-10-06 11:31:08 -0700642 }
Josef Bacik06222e42011-07-18 13:21:38 -0400643out:
Al Viro59551022016-01-22 15:40:57 -0500644 inode_unlock(inode);
Sage Weil2817b002009-10-06 11:31:08 -0700645 return retval;
646}
647
648/*
Sage Weil468640e2011-07-26 11:28:11 -0700649 * Handle lookups for the hidden .snap directory.
Sage Weil2817b002009-10-06 11:31:08 -0700650 */
Sage Weil468640e2011-07-26 11:28:11 -0700651int ceph_handle_snapdir(struct ceph_mds_request *req,
652 struct dentry *dentry, int err)
Sage Weil2817b002009-10-06 11:31:08 -0700653{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700654 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000655 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
Sage Weil2817b002009-10-06 11:31:08 -0700656
657 /* .snap dir? */
658 if (err == -ENOENT &&
Sage Weil455cec02011-03-03 13:44:35 -0800659 ceph_snap(parent) == CEPH_NOSNAP &&
Sage Weil6b805182009-10-27 11:50:50 -0700660 strcmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700661 fsc->mount_options->snapdir_name) == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700662 struct inode *inode = ceph_get_snapdir(parent);
Al Viroa4555892014-10-21 20:11:25 -0400663 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
664 dentry, dentry, inode);
Sage Weil9358c6d2010-03-30 13:54:41 -0700665 BUG_ON(!d_unhashed(dentry));
Sage Weil2817b002009-10-06 11:31:08 -0700666 d_add(dentry, inode);
667 err = 0;
668 }
Sage Weil468640e2011-07-26 11:28:11 -0700669 return err;
670}
Sage Weil2817b002009-10-06 11:31:08 -0700671
Sage Weil468640e2011-07-26 11:28:11 -0700672/*
673 * Figure out final result of a lookup/open request.
674 *
675 * Mainly, make sure we return the final req->r_dentry (if it already
676 * existed) in place of the original VFS-provided dentry when they
677 * differ.
678 *
679 * Gracefully handle the case where the MDS replies with -ENOENT and
680 * no trace (which it may do, at its discretion, e.g., if it doesn't
681 * care to issue a lease on the negative dentry).
682 */
683struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
684 struct dentry *dentry, int err)
685{
Sage Weil2817b002009-10-06 11:31:08 -0700686 if (err == -ENOENT) {
687 /* no trace? */
688 err = 0;
689 if (!req->r_reply_info.head->is_dentry) {
690 dout("ENOENT and no trace, dentry %p inode %p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000691 dentry, d_inode(dentry));
692 if (d_really_is_positive(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700693 d_drop(dentry);
694 err = -ENOENT;
695 } else {
696 d_add(dentry, NULL);
697 }
698 }
699 }
700 if (err)
701 dentry = ERR_PTR(err);
702 else if (dentry != req->r_dentry)
703 dentry = dget(req->r_dentry); /* we got spliced */
704 else
705 dentry = NULL;
706 return dentry;
707}
708
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -0400709static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
Sage Weil1d1de9162009-12-02 11:54:25 -0800710{
711 return ceph_ino(inode) == CEPH_INO_ROOT &&
712 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
713}
714
Sage Weil2817b002009-10-06 11:31:08 -0700715/*
716 * Look up a single dir entry. If there is a lookup intent, inform
717 * the MDS so that it gets our 'caps wanted' value in a single op.
718 */
719static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400720 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -0700721{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700722 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
723 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700724 struct ceph_mds_request *req;
725 int op;
Yan, Zheng315f2402016-03-07 10:34:50 +0800726 int mask;
Sage Weil2817b002009-10-06 11:31:08 -0700727 int err;
728
Al Viroa4555892014-10-21 20:11:25 -0400729 dout("lookup %p dentry %p '%pd'\n",
730 dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700731
732 if (dentry->d_name.len > NAME_MAX)
733 return ERR_PTR(-ENAMETOOLONG);
734
Sage Weil2817b002009-10-06 11:31:08 -0700735 /* can we conclude ENOENT locally? */
David Howells2b0143b2015-03-17 22:25:59 +0000736 if (d_really_is_negative(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700737 struct ceph_inode_info *ci = ceph_inode(dir);
738 struct ceph_dentry_info *di = ceph_dentry(dentry);
739
Sage Weilbe655592011-11-30 09:47:09 -0800740 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700741 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
742 if (strncmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700743 fsc->mount_options->snapdir_name,
Sage Weil2817b002009-10-06 11:31:08 -0700744 dentry->d_name.len) &&
Sage Weil1d1de9162009-12-02 11:54:25 -0800745 !is_root_ceph_dentry(dir, dentry) &&
Yan, Zhenge2c3de02015-03-04 16:05:04 +0800746 ceph_test_mount_opt(fsc, DCACHE) &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800747 __ceph_dir_is_complete(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700748 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800749 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700750 dout(" dir %p complete, -ENOENT\n", dir);
751 d_add(dentry, NULL);
752 di->lease_shared_gen = ci->i_shared_gen;
753 return NULL;
754 }
Sage Weilbe655592011-11-30 09:47:09 -0800755 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700756 }
757
758 op = ceph_snap(dir) == CEPH_SNAPDIR ?
759 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
760 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
761 if (IS_ERR(req))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200762 return ERR_CAST(req);
Sage Weil2817b002009-10-06 11:31:08 -0700763 req->r_dentry = dget(dentry);
764 req->r_num_caps = 2;
Yan, Zheng315f2402016-03-07 10:34:50 +0800765
766 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
767 if (ceph_security_xattr_wanted(dir))
768 mask |= CEPH_CAP_XATTR_SHARED;
769 req->r_args.getattr.mask = cpu_to_le32(mask);
770
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500771 req->r_parent = dir;
772 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700773 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil468640e2011-07-26 11:28:11 -0700774 err = ceph_handle_snapdir(req, dentry, err);
Sage Weil2817b002009-10-06 11:31:08 -0700775 dentry = ceph_finish_lookup(req, dentry, err);
776 ceph_mdsc_put_request(req); /* will dput(dentry) */
777 dout("lookup result=%p\n", dentry);
778 return dentry;
779}
780
781/*
782 * If we do a create but get no trace back from the MDS, follow up with
783 * a lookup (the VFS expects us to link up the provided dentry).
784 */
785int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
786{
Al Viro00cd8dd2012-06-10 17:13:09 -0400787 struct dentry *result = ceph_lookup(dir, dentry, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700788
789 if (result && !IS_ERR(result)) {
790 /*
791 * We created the item, then did a lookup, and found
792 * it was already linked to another inode we already
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800793 * had in our cache (and thus got spliced). To not
794 * confuse VFS (especially when inode is a directory),
795 * we don't link our dentry to that inode, return an
796 * error instead.
797 *
798 * This event should be rare and it happens only when
799 * we talk to old MDS. Recent MDS does not send traceless
800 * reply for request that creates new inode.
Sage Weil2817b002009-10-06 11:31:08 -0700801 */
Yan, Zheng5cba3722015-02-02 11:27:56 +0800802 d_drop(result);
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800803 return -ESTALE;
Sage Weil2817b002009-10-06 11:31:08 -0700804 }
805 return PTR_ERR(result);
806}
807
808static int ceph_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400809 umode_t mode, dev_t rdev)
Sage Weil2817b002009-10-06 11:31:08 -0700810{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700811 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
812 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700813 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800814 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700815 int err;
816
817 if (ceph_snap(dir) != CEPH_NOSNAP)
818 return -EROFS;
819
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800820 err = ceph_pre_init_acls(dir, &mode, &acls);
821 if (err < 0)
822 return err;
823
Al Viro1a67aaf2011-07-26 01:52:52 -0400824 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
Sage Weil2817b002009-10-06 11:31:08 -0700825 dir, dentry, mode, rdev);
826 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
827 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800828 err = PTR_ERR(req);
829 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700830 }
831 req->r_dentry = dget(dentry);
832 req->r_num_caps = 2;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500833 req->r_parent = dir;
834 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700835 req->r_args.mknod.mode = cpu_to_le32(mode);
836 req->r_args.mknod.rdev = cpu_to_le32(rdev);
837 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
838 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800839 if (acls.pagelist) {
840 req->r_pagelist = acls.pagelist;
841 acls.pagelist = NULL;
842 }
Sage Weil2817b002009-10-06 11:31:08 -0700843 err = ceph_mdsc_do_request(mdsc, dir, req);
844 if (!err && !req->r_reply_info.head->is_dentry)
845 err = ceph_handle_notrace_create(dir, dentry);
846 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800847out:
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800848 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000849 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800850 else
Sage Weil2817b002009-10-06 11:31:08 -0700851 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800852 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700853 return err;
854}
855
Al Viro4acdaf22011-07-26 01:42:34 -0400856static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400857 bool excl)
Sage Weil2817b002009-10-06 11:31:08 -0700858{
Miklos Szeredi2d83bde2012-06-05 15:10:25 +0200859 return ceph_mknod(dir, dentry, mode, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700860}
861
862static int ceph_symlink(struct inode *dir, struct dentry *dentry,
863 const char *dest)
864{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700865 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
866 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700867 struct ceph_mds_request *req;
868 int err;
869
870 if (ceph_snap(dir) != CEPH_NOSNAP)
871 return -EROFS;
872
873 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
874 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
875 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800876 err = PTR_ERR(req);
877 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700878 }
Yan, Zheng687265e2015-06-13 17:27:05 +0800879 req->r_path2 = kstrdup(dest, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400880 if (!req->r_path2) {
881 err = -ENOMEM;
882 ceph_mdsc_put_request(req);
883 goto out;
884 }
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500885 req->r_parent = dir;
886 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700887 req->r_dentry = dget(dentry);
888 req->r_num_caps = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700889 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
890 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
891 err = ceph_mdsc_do_request(mdsc, dir, req);
892 if (!err && !req->r_reply_info.head->is_dentry)
893 err = ceph_handle_notrace_create(dir, dentry);
894 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800895out:
896 if (err)
Sage Weil2817b002009-10-06 11:31:08 -0700897 d_drop(dentry);
898 return err;
899}
900
Al Viro18bb1db2011-07-26 01:41:39 -0400901static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Sage Weil2817b002009-10-06 11:31:08 -0700902{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700903 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
904 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700905 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800906 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700907 int err = -EROFS;
908 int op;
909
910 if (ceph_snap(dir) == CEPH_SNAPDIR) {
911 /* mkdir .snap/foo is a MKSNAP */
912 op = CEPH_MDS_OP_MKSNAP;
Al Viroa4555892014-10-21 20:11:25 -0400913 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
914 dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700915 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
Al Viro18bb1db2011-07-26 01:41:39 -0400916 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
Sage Weil2817b002009-10-06 11:31:08 -0700917 op = CEPH_MDS_OP_MKDIR;
918 } else {
919 goto out;
920 }
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800921
922 mode |= S_IFDIR;
923 err = ceph_pre_init_acls(dir, &mode, &acls);
924 if (err < 0)
925 goto out;
926
Sage Weil2817b002009-10-06 11:31:08 -0700927 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
928 if (IS_ERR(req)) {
929 err = PTR_ERR(req);
930 goto out;
931 }
932
933 req->r_dentry = dget(dentry);
934 req->r_num_caps = 2;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500935 req->r_parent = dir;
936 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700937 req->r_args.mkdir.mode = cpu_to_le32(mode);
938 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
939 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800940 if (acls.pagelist) {
941 req->r_pagelist = acls.pagelist;
942 acls.pagelist = NULL;
943 }
Sage Weil2817b002009-10-06 11:31:08 -0700944 err = ceph_mdsc_do_request(mdsc, dir, req);
Yan, Zheng275dd192014-12-10 16:17:31 +0800945 if (!err &&
946 !req->r_reply_info.head->is_target &&
947 !req->r_reply_info.head->is_dentry)
Sage Weil2817b002009-10-06 11:31:08 -0700948 err = ceph_handle_notrace_create(dir, dentry);
949 ceph_mdsc_put_request(req);
950out:
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800951 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000952 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800953 else
Sage Weil2817b002009-10-06 11:31:08 -0700954 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800955 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700956 return err;
957}
958
959static int ceph_link(struct dentry *old_dentry, struct inode *dir,
960 struct dentry *dentry)
961{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700962 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
963 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700964 struct ceph_mds_request *req;
965 int err;
966
967 if (ceph_snap(dir) != CEPH_NOSNAP)
968 return -EROFS;
969
970 dout("link in dir %p old_dentry %p dentry %p\n", dir,
971 old_dentry, dentry);
972 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
973 if (IS_ERR(req)) {
974 d_drop(dentry);
975 return PTR_ERR(req);
976 }
977 req->r_dentry = dget(dentry);
978 req->r_num_caps = 2;
Sage Weil4b58c9b2013-02-05 13:41:23 -0800979 req->r_old_dentry = dget(old_dentry);
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500980 req->r_parent = dir;
981 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700982 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
983 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengad88f232013-07-21 20:25:25 +0800984 /* release LINK_SHARED on source inode (mds will lock it) */
985 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
Sage Weil2817b002009-10-06 11:31:08 -0700986 err = ceph_mdsc_do_request(mdsc, dir, req);
Sage Weil70b666c2011-05-27 09:24:26 -0700987 if (err) {
Sage Weil2817b002009-10-06 11:31:08 -0700988 d_drop(dentry);
Sage Weil70b666c2011-05-27 09:24:26 -0700989 } else if (!req->r_reply_info.head->is_dentry) {
David Howells2b0143b2015-03-17 22:25:59 +0000990 ihold(d_inode(old_dentry));
991 d_instantiate(dentry, d_inode(old_dentry));
Sage Weil70b666c2011-05-27 09:24:26 -0700992 }
Sage Weil2817b002009-10-06 11:31:08 -0700993 ceph_mdsc_put_request(req);
994 return err;
995}
996
997/*
998 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
999 * looks like the link count will hit 0, drop any other caps (other
1000 * than PIN) we don't specifically want (due to the file still being
1001 * open).
1002 */
1003static int drop_caps_for_unlink(struct inode *inode)
1004{
1005 struct ceph_inode_info *ci = ceph_inode(inode);
1006 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1007
Sage Weilbe655592011-11-30 09:47:09 -08001008 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001009 if (inode->i_nlink == 1) {
1010 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1011 ci->i_ceph_flags |= CEPH_I_NODELAY;
1012 }
Sage Weilbe655592011-11-30 09:47:09 -08001013 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001014 return drop;
1015}
1016
1017/*
1018 * rmdir and unlink are differ only by the metadata op code
1019 */
1020static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1021{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001022 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1023 struct ceph_mds_client *mdsc = fsc->mdsc;
David Howells2b0143b2015-03-17 22:25:59 +00001024 struct inode *inode = d_inode(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001025 struct ceph_mds_request *req;
1026 int err = -EROFS;
1027 int op;
1028
1029 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1030 /* rmdir .snap/foo is RMSNAP */
Al Viroa4555892014-10-21 20:11:25 -04001031 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001032 op = CEPH_MDS_OP_RMSNAP;
1033 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1034 dout("unlink/rmdir dir %p dn %p inode %p\n",
1035 dir, dentry, inode);
David Howellse36cb0b2015-01-29 12:02:35 +00001036 op = d_is_dir(dentry) ?
Sage Weil2817b002009-10-06 11:31:08 -07001037 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1038 } else
1039 goto out;
1040 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1041 if (IS_ERR(req)) {
1042 err = PTR_ERR(req);
1043 goto out;
1044 }
1045 req->r_dentry = dget(dentry);
1046 req->r_num_caps = 2;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001047 req->r_parent = dir;
1048 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -07001049 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1050 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1051 req->r_inode_drop = drop_caps_for_unlink(inode);
1052 err = ceph_mdsc_do_request(mdsc, dir, req);
1053 if (!err && !req->r_reply_info.head->is_dentry)
1054 d_delete(dentry);
1055 ceph_mdsc_put_request(req);
1056out:
1057 return err;
1058}
1059
1060static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001061 struct inode *new_dir, struct dentry *new_dentry,
1062 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001063{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001064 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1065 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001066 struct ceph_mds_request *req;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001067 int op = CEPH_MDS_OP_RENAME;
Sage Weil2817b002009-10-06 11:31:08 -07001068 int err;
1069
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001070 if (flags)
1071 return -EINVAL;
1072
Sage Weil2817b002009-10-06 11:31:08 -07001073 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1074 return -EXDEV;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001075 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1076 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1077 op = CEPH_MDS_OP_RENAMESNAP;
1078 else
1079 return -EROFS;
1080 }
Sage Weil2817b002009-10-06 11:31:08 -07001081 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1082 old_dir, old_dentry, new_dir, new_dentry);
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001083 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Sage Weil2817b002009-10-06 11:31:08 -07001084 if (IS_ERR(req))
1085 return PTR_ERR(req);
Sage Weil180061a2013-02-05 13:36:05 -08001086 ihold(old_dir);
Sage Weil2817b002009-10-06 11:31:08 -07001087 req->r_dentry = dget(new_dentry);
1088 req->r_num_caps = 2;
1089 req->r_old_dentry = dget(old_dentry);
Sage Weil180061a2013-02-05 13:36:05 -08001090 req->r_old_dentry_dir = old_dir;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001091 req->r_parent = new_dir;
1092 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -07001093 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1094 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1095 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1096 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1097 /* release LINK_RDCACHE on source inode (mds will lock it) */
1098 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED;
David Howells2b0143b2015-03-17 22:25:59 +00001099 if (d_really_is_positive(new_dentry))
1100 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
Sage Weil2817b002009-10-06 11:31:08 -07001101 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1102 if (!err && !req->r_reply_info.head->is_dentry) {
1103 /*
1104 * Normally d_move() is done by fill_trace (called by
1105 * do_request, above). If there is no trace, we need
1106 * to do it here.
1107 */
Sage Weilea1409f2010-04-28 16:12:06 -07001108
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001109 /* d_move screws up sibling dentries' offsets */
1110 ceph_dir_clear_complete(old_dir);
1111 ceph_dir_clear_complete(new_dir);
1112
Sage Weil2817b002009-10-06 11:31:08 -07001113 d_move(old_dentry, new_dentry);
Sage Weilea1409f2010-04-28 16:12:06 -07001114
1115 /* ensure target dentry is invalidated, despite
1116 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001117 ceph_invalidate_dentry_lease(new_dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001118 }
1119 ceph_mdsc_put_request(req);
1120 return err;
1121}
1122
Sage Weil81a6cf22010-05-14 09:35:38 -07001123/*
1124 * Ensure a dentry lease will no longer revalidate.
1125 */
1126void ceph_invalidate_dentry_lease(struct dentry *dentry)
1127{
1128 spin_lock(&dentry->d_lock);
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02001129 ceph_dentry(dentry)->time = jiffies;
Sage Weil81a6cf22010-05-14 09:35:38 -07001130 ceph_dentry(dentry)->lease_shared_gen = 0;
1131 spin_unlock(&dentry->d_lock);
1132}
Sage Weil2817b002009-10-06 11:31:08 -07001133
1134/*
1135 * Check if dentry lease is valid. If not, delete the lease. Try to
1136 * renew if the least is more than half up.
1137 */
Jeff Layton14fb9c92016-07-01 09:39:20 -04001138static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1139 struct inode *dir)
Sage Weil2817b002009-10-06 11:31:08 -07001140{
1141 struct ceph_dentry_info *di;
1142 struct ceph_mds_session *s;
1143 int valid = 0;
1144 u32 gen;
1145 unsigned long ttl;
1146 struct ceph_mds_session *session = NULL;
Sage Weil2817b002009-10-06 11:31:08 -07001147 u32 seq = 0;
1148
1149 spin_lock(&dentry->d_lock);
1150 di = ceph_dentry(dentry);
Jeff Layton14fb9c92016-07-01 09:39:20 -04001151 if (di && di->lease_session) {
Sage Weil2817b002009-10-06 11:31:08 -07001152 s = di->lease_session;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001153 spin_lock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001154 gen = s->s_cap_gen;
1155 ttl = s->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001156 spin_unlock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001157
1158 if (di->lease_gen == gen &&
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02001159 time_before(jiffies, di->time) &&
Sage Weil2817b002009-10-06 11:31:08 -07001160 time_before(jiffies, ttl)) {
1161 valid = 1;
1162 if (di->lease_renew_after &&
1163 time_after(jiffies, di->lease_renew_after)) {
Jeff Layton14fb9c92016-07-01 09:39:20 -04001164 /*
1165 * We should renew. If we're in RCU walk mode
1166 * though, we can't do that so just return
1167 * -ECHILD.
1168 */
1169 if (flags & LOOKUP_RCU) {
1170 valid = -ECHILD;
1171 } else {
1172 session = ceph_get_mds_session(s);
1173 seq = di->lease_seq;
1174 di->lease_renew_after = 0;
1175 di->lease_renew_from = jiffies;
1176 }
Sage Weil2817b002009-10-06 11:31:08 -07001177 }
Sage Weil2817b002009-10-06 11:31:08 -07001178 }
1179 }
1180 spin_unlock(&dentry->d_lock);
1181
1182 if (session) {
1183 ceph_mdsc_lease_send_msg(session, dir, dentry,
1184 CEPH_MDS_LEASE_RENEW, seq);
1185 ceph_put_mds_session(session);
1186 }
1187 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1188 return valid;
1189}
1190
1191/*
1192 * Check if directory-wide content lease/cap is valid.
1193 */
1194static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1195{
1196 struct ceph_inode_info *ci = ceph_inode(dir);
1197 struct ceph_dentry_info *di = ceph_dentry(dentry);
1198 int valid = 0;
1199
Sage Weilbe655592011-11-30 09:47:09 -08001200 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001201 if (ci->i_shared_gen == di->lease_shared_gen)
1202 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
Sage Weilbe655592011-11-30 09:47:09 -08001203 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001204 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1205 dir, (unsigned)ci->i_shared_gen, dentry,
1206 (unsigned)di->lease_shared_gen, valid);
1207 return valid;
1208}
1209
1210/*
1211 * Check if cached dentry can be trusted.
1212 */
Al Viro0b728e12012-06-10 16:03:43 -04001213static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001214{
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001215 int valid = 0;
Yan, Zheng641235d2016-03-16 16:40:23 +08001216 struct dentry *parent;
Nick Piggin34286d62011-01-07 17:49:57 +11001217 struct inode *dir;
1218
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001219 if (flags & LOOKUP_RCU) {
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001220 parent = READ_ONCE(dentry->d_parent);
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001221 dir = d_inode_rcu(parent);
1222 if (!dir)
1223 return -ECHILD;
1224 } else {
1225 parent = dget_parent(dentry);
1226 dir = d_inode(parent);
1227 }
Nick Piggin34286d62011-01-07 17:49:57 +11001228
Al Viroa4555892014-10-21 20:11:25 -04001229 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001230 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
Sage Weil2817b002009-10-06 11:31:08 -07001231
1232 /* always trust cached snapped dentries, snapdir dentry */
1233 if (ceph_snap(dir) != CEPH_NOSNAP) {
Al Viroa4555892014-10-21 20:11:25 -04001234 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001235 dentry, d_inode(dentry));
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001236 valid = 1;
David Howells2b0143b2015-03-17 22:25:59 +00001237 } else if (d_really_is_positive(dentry) &&
1238 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001239 valid = 1;
Jeff Layton14fb9c92016-07-01 09:39:20 -04001240 } else {
1241 valid = dentry_lease_is_valid(dentry, flags, dir);
1242 if (valid == -ECHILD)
1243 return valid;
1244 if (valid || dir_lease_is_valid(dir, dentry)) {
1245 if (d_really_is_positive(dentry))
1246 valid = ceph_is_any_caps(d_inode(dentry));
1247 else
1248 valid = 1;
1249 }
Sage Weil2817b002009-10-06 11:31:08 -07001250 }
Sage Weil2817b002009-10-06 11:31:08 -07001251
Yan, Zheng200fd272016-03-17 14:41:59 +08001252 if (!valid) {
1253 struct ceph_mds_client *mdsc =
1254 ceph_sb_to_client(dir->i_sb)->mdsc;
1255 struct ceph_mds_request *req;
Jeff Layton10976802017-01-12 14:42:38 -05001256 int op, err;
1257 u32 mask;
Yan, Zheng200fd272016-03-17 14:41:59 +08001258
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001259 if (flags & LOOKUP_RCU)
1260 return -ECHILD;
1261
Yan, Zheng200fd272016-03-17 14:41:59 +08001262 op = ceph_snap(dir) == CEPH_SNAPDIR ?
Jeff Layton5eb9f602017-01-30 09:47:25 -05001263 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
Yan, Zheng200fd272016-03-17 14:41:59 +08001264 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1265 if (!IS_ERR(req)) {
1266 req->r_dentry = dget(dentry);
Jeff Layton5eb9f602017-01-30 09:47:25 -05001267 req->r_num_caps = 2;
1268 req->r_parent = dir;
Yan, Zheng200fd272016-03-17 14:41:59 +08001269
1270 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1271 if (ceph_security_xattr_wanted(dir))
1272 mask |= CEPH_CAP_XATTR_SHARED;
Jeff Layton10976802017-01-12 14:42:38 -05001273 req->r_args.getattr.mask = cpu_to_le32(mask);
Yan, Zheng200fd272016-03-17 14:41:59 +08001274
Yan, Zheng200fd272016-03-17 14:41:59 +08001275 err = ceph_mdsc_do_request(mdsc, NULL, req);
Jeff Laytonc3f46882016-11-30 15:56:46 -05001276 switch (err) {
1277 case 0:
1278 if (d_really_is_positive(dentry) &&
1279 d_inode(dentry) == req->r_target_inode)
1280 valid = 1;
1281 break;
1282 case -ENOENT:
1283 if (d_really_is_negative(dentry))
1284 valid = 1;
1285 /* Fallthrough */
1286 default:
1287 break;
Yan, Zheng200fd272016-03-17 14:41:59 +08001288 }
1289 ceph_mdsc_put_request(req);
1290 dout("d_revalidate %p lookup result=%d\n",
1291 dentry, err);
1292 }
1293 }
1294
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001295 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
Yan, Zheng9215aee2013-11-30 12:47:41 +08001296 if (valid) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001297 ceph_dentry_lru_touch(dentry);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001298 } else {
1299 ceph_dir_clear_complete(dir);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001300 }
Yan, Zheng641235d2016-03-16 16:40:23 +08001301
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001302 if (!(flags & LOOKUP_RCU))
1303 dput(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001304 return valid;
Sage Weil2817b002009-10-06 11:31:08 -07001305}
1306
1307/*
Sage Weil147851d2011-03-15 14:57:41 -07001308 * Release our ceph_dentry_info.
Sage Weil2817b002009-10-06 11:31:08 -07001309 */
Sage Weil147851d2011-03-15 14:57:41 -07001310static void ceph_d_release(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -07001311{
1312 struct ceph_dentry_info *di = ceph_dentry(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001313
Sage Weil147851d2011-03-15 14:57:41 -07001314 dout("d_release %p\n", dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001315 ceph_dentry_lru_del(dentry);
Jeff Layton5b484a52016-07-01 09:39:20 -04001316
1317 spin_lock(&dentry->d_lock);
1318 dentry->d_fsdata = NULL;
1319 spin_unlock(&dentry->d_lock);
1320
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001321 if (di->lease_session)
1322 ceph_put_mds_session(di->lease_session);
1323 kmem_cache_free(ceph_dentry_cachep, di);
Sage Weil2817b002009-10-06 11:31:08 -07001324}
1325
Sage Weilb58dc412011-03-15 15:53:40 -07001326/*
1327 * When the VFS prunes a dentry from the cache, we need to clear the
1328 * complete flag on the parent directory.
1329 *
1330 * Called under dentry->d_lock.
1331 */
1332static void ceph_d_prune(struct dentry *dentry)
1333{
Sage Weil774ac212011-11-11 09:48:08 -08001334 dout("ceph_d_prune %p\n", dentry);
Sage Weilb58dc412011-03-15 15:53:40 -07001335
1336 /* do we have a valid parent? */
Sage Weil8842b3b2012-06-07 13:43:35 -07001337 if (IS_ROOT(dentry))
Sage Weilb58dc412011-03-15 15:53:40 -07001338 return;
1339
Yan, Zheng2f276c52013-03-13 19:44:32 +08001340 /* if we are not hashed, we don't affect dir's completeness */
Sage Weilb58dc412011-03-15 15:53:40 -07001341 if (d_unhashed(dentry))
1342 return;
1343
Al Viro18fc8ab2016-10-28 21:52:50 -04001344 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
1345 return;
1346
Sage Weilb58dc412011-03-15 15:53:40 -07001347 /*
1348 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1349 * cleared until d_release
1350 */
David Howells2b0143b2015-03-17 22:25:59 +00001351 ceph_dir_clear_complete(d_inode(dentry->d_parent));
Sage Weilb58dc412011-03-15 15:53:40 -07001352}
Sage Weil2817b002009-10-06 11:31:08 -07001353
1354/*
1355 * read() on a dir. This weird interface hack only works if mounted
1356 * with '-o dirstat'.
1357 */
1358static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1359 loff_t *ppos)
1360{
1361 struct ceph_file_info *cf = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05001362 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -07001363 struct ceph_inode_info *ci = ceph_inode(inode);
1364 int left;
Sage Weilae598082011-05-12 14:28:05 -07001365 const int bufsize = 1024;
Sage Weil2817b002009-10-06 11:31:08 -07001366
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001367 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
Sage Weil2817b002009-10-06 11:31:08 -07001368 return -EISDIR;
1369
1370 if (!cf->dir_info) {
Yan, Zheng687265e2015-06-13 17:27:05 +08001371 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -07001372 if (!cf->dir_info)
1373 return -ENOMEM;
1374 cf->dir_info_len =
Sage Weilae598082011-05-12 14:28:05 -07001375 snprintf(cf->dir_info, bufsize,
Sage Weil2817b002009-10-06 11:31:08 -07001376 "entries: %20lld\n"
1377 " files: %20lld\n"
1378 " subdirs: %20lld\n"
1379 "rentries: %20lld\n"
1380 " rfiles: %20lld\n"
1381 " rsubdirs: %20lld\n"
1382 "rbytes: %20lld\n"
1383 "rctime: %10ld.%09ld\n",
1384 ci->i_files + ci->i_subdirs,
1385 ci->i_files,
1386 ci->i_subdirs,
1387 ci->i_rfiles + ci->i_rsubdirs,
1388 ci->i_rfiles,
1389 ci->i_rsubdirs,
1390 ci->i_rbytes,
1391 (long)ci->i_rctime.tv_sec,
1392 (long)ci->i_rctime.tv_nsec);
1393 }
1394
1395 if (*ppos >= cf->dir_info_len)
1396 return 0;
1397 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1398 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1399 if (left == size)
1400 return -EFAULT;
1401 *ppos += (size - left);
1402 return size - left;
1403}
1404
1405/*
Sage Weil2817b002009-10-06 11:31:08 -07001406 * We maintain a private dentry LRU.
1407 *
1408 * FIXME: this needs to be changed to a per-mds lru to be useful.
1409 */
1410void ceph_dentry_lru_add(struct dentry *dn)
1411{
1412 struct ceph_dentry_info *di = ceph_dentry(dn);
1413 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001414
Al Viroa4555892014-10-21 20:11:25 -04001415 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001416 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1417 spin_lock(&mdsc->dentry_lru_lock);
1418 list_add_tail(&di->lru, &mdsc->dentry_lru);
1419 mdsc->num_dentry++;
1420 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001421}
1422
1423void ceph_dentry_lru_touch(struct dentry *dn)
1424{
1425 struct ceph_dentry_info *di = ceph_dentry(dn);
1426 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001427
Al Viroa4555892014-10-21 20:11:25 -04001428 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1429 di->offset);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001430 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1431 spin_lock(&mdsc->dentry_lru_lock);
1432 list_move_tail(&di->lru, &mdsc->dentry_lru);
1433 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001434}
1435
1436void ceph_dentry_lru_del(struct dentry *dn)
1437{
1438 struct ceph_dentry_info *di = ceph_dentry(dn);
1439 struct ceph_mds_client *mdsc;
1440
Al Viroa4555892014-10-21 20:11:25 -04001441 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001442 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1443 spin_lock(&mdsc->dentry_lru_lock);
1444 list_del_init(&di->lru);
1445 mdsc->num_dentry--;
1446 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001447}
1448
Sage Weil6c0f3af2010-11-16 11:14:34 -08001449/*
1450 * Return name hash for a given dentry. This is dependent on
1451 * the parent directory's hash function.
1452 */
Sage Weile5f86dc2011-07-26 11:30:55 -07001453unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
Sage Weil6c0f3af2010-11-16 11:14:34 -08001454{
Sage Weil6c0f3af2010-11-16 11:14:34 -08001455 struct ceph_inode_info *dci = ceph_inode(dir);
1456
1457 switch (dci->i_dir_layout.dl_dir_hash) {
1458 case 0: /* for backward compat */
1459 case CEPH_STR_HASH_LINUX:
1460 return dn->d_name.hash;
1461
1462 default:
1463 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1464 dn->d_name.name, dn->d_name.len);
1465 }
1466}
1467
Sage Weil2817b002009-10-06 11:31:08 -07001468const struct file_operations ceph_dir_fops = {
1469 .read = ceph_read_dir,
Al Viro77acfa22013-05-17 16:52:26 -04001470 .iterate = ceph_readdir,
Sage Weil2817b002009-10-06 11:31:08 -07001471 .llseek = ceph_dir_llseek,
1472 .open = ceph_open,
1473 .release = ceph_release,
1474 .unlocked_ioctl = ceph_ioctl,
Yan, Zhengda819c82015-05-27 11:19:34 +08001475 .fsync = ceph_fsync,
Sage Weil2817b002009-10-06 11:31:08 -07001476};
1477
Yan, Zheng38c48b52015-01-14 13:46:04 +08001478const struct file_operations ceph_snapdir_fops = {
1479 .iterate = ceph_readdir,
1480 .llseek = ceph_dir_llseek,
1481 .open = ceph_open,
1482 .release = ceph_release,
1483};
1484
Sage Weil2817b002009-10-06 11:31:08 -07001485const struct inode_operations ceph_dir_iops = {
1486 .lookup = ceph_lookup,
1487 .permission = ceph_permission,
1488 .getattr = ceph_getattr,
1489 .setattr = ceph_setattr,
Sage Weil2817b002009-10-06 11:31:08 -07001490 .listxattr = ceph_listxattr,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001491 .get_acl = ceph_get_acl,
Sage Weil72466d02014-01-29 06:22:25 -08001492 .set_acl = ceph_set_acl,
Sage Weil2817b002009-10-06 11:31:08 -07001493 .mknod = ceph_mknod,
1494 .symlink = ceph_symlink,
1495 .mkdir = ceph_mkdir,
1496 .link = ceph_link,
1497 .unlink = ceph_unlink,
1498 .rmdir = ceph_unlink,
1499 .rename = ceph_rename,
1500 .create = ceph_create,
Miklos Szeredi2d83bde2012-06-05 15:10:25 +02001501 .atomic_open = ceph_atomic_open,
Sage Weil2817b002009-10-06 11:31:08 -07001502};
1503
Yan, Zheng38c48b52015-01-14 13:46:04 +08001504const struct inode_operations ceph_snapdir_iops = {
1505 .lookup = ceph_lookup,
1506 .permission = ceph_permission,
1507 .getattr = ceph_getattr,
1508 .mkdir = ceph_mkdir,
1509 .rmdir = ceph_unlink,
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001510 .rename = ceph_rename,
Yan, Zheng38c48b52015-01-14 13:46:04 +08001511};
1512
Sage Weil52dfb8a2010-08-03 10:25:30 -07001513const struct dentry_operations ceph_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001514 .d_revalidate = ceph_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001515 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001516 .d_prune = ceph_d_prune,
Al Viroad5cb122016-10-28 22:05:13 -04001517 .d_init = ceph_d_init,
Sage Weil2817b002009-10-06 11:31:08 -07001518};