blob: 9fde02cd5568d88854025b4be84c69baef2c57fa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil2817b002009-10-06 11:31:08 -07003
4#include <linux/spinlock.h>
5#include <linux/fs_struct.h>
6#include <linux/namei.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Sage Weil2817b002009-10-06 11:31:08 -07008#include <linux/sched.h>
Andreas Gruenbacher2cdeb1e2016-04-14 00:30:17 +02009#include <linux/xattr.h>
Sage Weil2817b002009-10-06 11:31:08 -070010
11#include "super.h"
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070012#include "mds_client.h"
Sage Weil2817b002009-10-06 11:31:08 -070013
14/*
15 * Directory operations: readdir, lookup, create, link, unlink,
16 * rename, etc.
17 */
18
19/*
20 * Ceph MDS operations are specified in terms of a base ino and
21 * relative path. Thus, the client can specify an operation on a
22 * specific inode (e.g., a getattr due to fstat(2)), or as a path
23 * relative to, say, the root directory.
24 *
25 * Normally, we limit ourselves to strict inode ops (no path component)
26 * or dentry operations (a single path component relative to an ino). The
27 * exception to this is open_root_dentry(), which will open the mount
28 * point by name.
29 */
30
Sage Weil52dfb8a2010-08-03 10:25:30 -070031const struct dentry_operations ceph_dentry_ops;
Sage Weil2817b002009-10-06 11:31:08 -070032
33/*
34 * Initialize ceph dentry state.
35 */
Al Viroad5cb122016-10-28 22:05:13 -040036static int ceph_d_init(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -070037{
38 struct ceph_dentry_info *di;
39
Geliang Tang99ec2692016-03-13 15:26:29 +080040 di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -070041 if (!di)
42 return -ENOMEM; /* oh well */
43
Sage Weil2817b002009-10-06 11:31:08 -070044 di->dentry = dentry;
45 di->lease_session = NULL;
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +020046 di->time = jiffies;
Sage Weil48d0cbd2011-07-26 11:30:15 -070047 dentry->d_fsdata = di;
Sage Weil2817b002009-10-06 11:31:08 -070048 ceph_dentry_lru_add(dentry);
Sage Weil2817b002009-10-06 11:31:08 -070049 return 0;
50}
51
Sage Weil2817b002009-10-06 11:31:08 -070052/*
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080053 * for f_pos for readdir:
54 * - hash order:
55 * (0xff << 52) | ((24 bits hash) << 28) |
56 * (the nth entry has hash collision);
57 * - frag+name order;
58 * ((frag value) << 28) | (the nth entry in frag);
Sage Weil2817b002009-10-06 11:31:08 -070059 */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080060#define OFFSET_BITS 28
61#define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
62#define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
63loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
64{
65 loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
66 if (hash_order)
67 fpos |= HASH_ORDER;
68 return fpos;
69}
70
71static bool is_hash_order(loff_t p)
72{
73 return (p & HASH_ORDER) == HASH_ORDER;
74}
75
Sage Weil2817b002009-10-06 11:31:08 -070076static unsigned fpos_frag(loff_t p)
77{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080078 return p >> OFFSET_BITS;
Sage Weil2817b002009-10-06 11:31:08 -070079}
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080080
81static unsigned fpos_hash(loff_t p)
82{
83 return ceph_frag_value(fpos_frag(p));
84}
85
Sage Weil2817b002009-10-06 11:31:08 -070086static unsigned fpos_off(loff_t p)
87{
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +080088 return p & OFFSET_MASK;
Sage Weil2817b002009-10-06 11:31:08 -070089}
90
Yan, Zheng4d5f5df2014-02-13 19:40:26 +080091static int fpos_cmp(loff_t l, loff_t r)
92{
93 int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
94 if (v)
95 return v;
96 return (int)(fpos_off(l) - fpos_off(r));
97}
98
Sage Weil2817b002009-10-06 11:31:08 -070099/*
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800100 * make note of the last dentry we read, so we can
101 * continue at the same lexicographical point,
102 * regardless of what dir changes take place on the
103 * server.
104 */
105static int note_last_dentry(struct ceph_file_info *fi, const char *name,
106 int len, unsigned next_offset)
107{
108 char *buf = kmalloc(len+1, GFP_KERNEL);
109 if (!buf)
110 return -ENOMEM;
111 kfree(fi->last_name);
112 fi->last_name = buf;
113 memcpy(fi->last_name, name, len);
114 fi->last_name[len] = 0;
115 fi->next_offset = next_offset;
116 dout("note_last_dentry '%s'\n", fi->last_name);
117 return 0;
118}
119
Yan, Zhengc530cd22016-04-28 17:43:35 +0800120
121static struct dentry *
122__dcache_find_get_entry(struct dentry *parent, u64 idx,
123 struct ceph_readdir_cache_control *cache_ctl)
124{
125 struct inode *dir = d_inode(parent);
126 struct dentry *dentry;
127 unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
128 loff_t ptr_pos = idx * sizeof(struct dentry *);
129 pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
130
131 if (ptr_pos >= i_size_read(dir))
132 return NULL;
133
134 if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
135 ceph_readdir_cache_release(cache_ctl);
136 cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
137 if (!cache_ctl->page) {
138 dout(" page %lu not found\n", ptr_pgoff);
139 return ERR_PTR(-EAGAIN);
140 }
141 /* reading/filling the cache are serialized by
142 i_mutex, no need to use page lock */
143 unlock_page(cache_ctl->page);
144 cache_ctl->dentries = kmap(cache_ctl->page);
145 }
146
147 cache_ctl->index = idx & idx_mask;
148
149 rcu_read_lock();
150 spin_lock(&parent->d_lock);
151 /* check i_size again here, because empty directory can be
152 * marked as complete while not holding the i_mutex. */
153 if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
154 dentry = cache_ctl->dentries[cache_ctl->index];
155 else
156 dentry = NULL;
157 spin_unlock(&parent->d_lock);
158 if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
159 dentry = NULL;
160 rcu_read_unlock();
161 return dentry ? : ERR_PTR(-EAGAIN);
162}
163
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800164/*
Sage Weil2817b002009-10-06 11:31:08 -0700165 * When possible, we try to satisfy a readdir by peeking at the
166 * dcache. We make this work by carefully ordering dentries on
Al Viro946e51f2014-10-26 19:19:16 -0400167 * d_child when we initially get results back from the MDS, and
Sage Weil2817b002009-10-06 11:31:08 -0700168 * falling back to a "normal" sync readdir if any dentries in the dir
169 * are dropped.
170 *
Yan, Zheng2f276c52013-03-13 19:44:32 +0800171 * Complete dir indicates that we have all dentries in the dir. It is
Sage Weil2817b002009-10-06 11:31:08 -0700172 * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
173 * the MDS if/when the directory is modified).
174 */
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800175static int __dcache_readdir(struct file *file, struct dir_context *ctx,
176 u32 shared_gen)
Sage Weil2817b002009-10-06 11:31:08 -0700177{
Al Viro77acfa22013-05-17 16:52:26 -0400178 struct ceph_file_info *fi = file->private_data;
Al Virob5830432014-10-31 01:22:04 -0400179 struct dentry *parent = file->f_path.dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000180 struct inode *dir = d_inode(parent);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800181 struct dentry *dentry, *last = NULL;
Sage Weil2817b002009-10-06 11:31:08 -0700182 struct ceph_dentry_info *di;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800183 struct ceph_readdir_cache_control cache_ctl = {};
Yan, Zhengc530cd22016-04-28 17:43:35 +0800184 u64 idx = 0;
185 int err = 0;
Sage Weil2817b002009-10-06 11:31:08 -0700186
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800187 dout("__dcache_readdir %p v%u at %llx\n", dir, shared_gen, ctx->pos);
Sage Weil2817b002009-10-06 11:31:08 -0700188
Yan, Zhengc530cd22016-04-28 17:43:35 +0800189 /* search start position */
190 if (ctx->pos > 2) {
191 u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
192 while (count > 0) {
193 u64 step = count >> 1;
194 dentry = __dcache_find_get_entry(parent, idx + step,
195 &cache_ctl);
196 if (!dentry) {
197 /* use linar search */
198 idx = 0;
199 break;
200 }
201 if (IS_ERR(dentry)) {
202 err = PTR_ERR(dentry);
203 goto out;
204 }
205 di = ceph_dentry(dentry);
206 spin_lock(&dentry->d_lock);
207 if (fpos_cmp(di->offset, ctx->pos) < 0) {
208 idx += step + 1;
209 count -= step + 1;
210 } else {
211 count = step;
212 }
213 spin_unlock(&dentry->d_lock);
214 dput(dentry);
215 }
216
217 dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
Sage Weil2817b002009-10-06 11:31:08 -0700218 }
219
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800220
Yan, Zhengc530cd22016-04-28 17:43:35 +0800221 for (;;) {
222 bool emit_dentry = false;
223 dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
224 if (!dentry) {
Sage Weil9cfa1092011-07-26 11:26:18 -0700225 fi->flags |= CEPH_F_ATEND;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800226 err = 0;
227 break;
Sage Weil2817b002009-10-06 11:31:08 -0700228 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800229 if (IS_ERR(dentry)) {
230 err = PTR_ERR(dentry);
231 goto out;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800232 }
233
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800234 di = ceph_dentry(dentry);
235 spin_lock(&dentry->d_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800236 if (di->lease_shared_gen == shared_gen &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800237 d_really_is_positive(dentry) &&
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800238 fpos_cmp(ctx->pos, di->offset) <= 0) {
239 emit_dentry = true;
Sage Weil2817b002009-10-06 11:31:08 -0700240 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800241 spin_unlock(&dentry->d_lock);
242
243 if (emit_dentry) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800244 dout(" %llx dentry %p %pd %p\n", di->offset,
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800245 dentry, dentry, d_inode(dentry));
246 ctx->pos = di->offset;
247 if (!dir_emit(ctx, dentry->d_name.name,
248 dentry->d_name.len,
249 ceph_translate_ino(dentry->d_sb,
250 d_inode(dentry)->i_ino),
251 d_inode(dentry)->i_mode >> 12)) {
252 dput(dentry);
253 err = 0;
254 break;
255 }
256 ctx->pos++;
257
258 if (last)
259 dput(last);
260 last = dentry;
261 } else {
262 dput(dentry);
263 }
Sage Weil2817b002009-10-06 11:31:08 -0700264 }
Yan, Zhengc530cd22016-04-28 17:43:35 +0800265out:
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800266 ceph_readdir_cache_release(&cache_ctl);
267 if (last) {
268 int ret;
269 di = ceph_dentry(last);
270 ret = note_last_dentry(fi, last->d_name.name, last->d_name.len,
271 fpos_off(di->offset) + 1);
272 if (ret < 0)
273 err = ret;
Al Viro77acfa22013-05-17 16:52:26 -0400274 dput(last);
Yan, Zheng84583cfb2017-07-06 11:12:21 +0800275 /* last_name no longer match cache index */
276 if (fi->readdir_cache_idx >= 0) {
277 fi->readdir_cache_idx = -1;
278 fi->dir_release_count = 0;
279 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800280 }
Sage Weil2817b002009-10-06 11:31:08 -0700281 return err;
282}
283
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800284static bool need_send_readdir(struct ceph_file_info *fi, loff_t pos)
285{
286 if (!fi->last_readdir)
287 return true;
288 if (is_hash_order(pos))
289 return !ceph_frag_contains_value(fi->frag, fpos_hash(pos));
290 else
291 return fi->frag != fpos_frag(pos);
292}
293
Al Viro77acfa22013-05-17 16:52:26 -0400294static int ceph_readdir(struct file *file, struct dir_context *ctx)
Sage Weil2817b002009-10-06 11:31:08 -0700295{
Al Viro77acfa22013-05-17 16:52:26 -0400296 struct ceph_file_info *fi = file->private_data;
297 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -0700298 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700299 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
300 struct ceph_mds_client *mdsc = fsc->mdsc;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800301 int i;
Sage Weil2817b002009-10-06 11:31:08 -0700302 int err;
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800303 unsigned frag = -1;
Sage Weil2817b002009-10-06 11:31:08 -0700304 struct ceph_mds_reply_info_parsed *rinfo;
Sage Weil2817b002009-10-06 11:31:08 -0700305
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800306 dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
Sage Weil9cfa1092011-07-26 11:26:18 -0700307 if (fi->flags & CEPH_F_ATEND)
Sage Weil2817b002009-10-06 11:31:08 -0700308 return 0;
309
310 /* always start with . and .. */
Al Viro77acfa22013-05-17 16:52:26 -0400311 if (ctx->pos == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700312 dout("readdir off 0 -> '.'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400313 if (!dir_emit(ctx, ".", 1,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800314 ceph_translate_ino(inode->i_sb, inode->i_ino),
Al Viro77acfa22013-05-17 16:52:26 -0400315 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700316 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400317 ctx->pos = 1;
Sage Weil2817b002009-10-06 11:31:08 -0700318 }
Al Viro77acfa22013-05-17 16:52:26 -0400319 if (ctx->pos == 1) {
Al Virob5830432014-10-31 01:22:04 -0400320 ino_t ino = parent_ino(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700321 dout("readdir off 1 -> '..'\n");
Al Viro77acfa22013-05-17 16:52:26 -0400322 if (!dir_emit(ctx, "..", 2,
Yehuda Sadehad1fee92011-01-21 16:44:03 -0800323 ceph_translate_ino(inode->i_sb, ino),
Al Viro77acfa22013-05-17 16:52:26 -0400324 inode->i_mode >> 12))
Sage Weil2817b002009-10-06 11:31:08 -0700325 return 0;
Al Viro77acfa22013-05-17 16:52:26 -0400326 ctx->pos = 2;
Sage Weil2817b002009-10-06 11:31:08 -0700327 }
328
329 /* can we use the dcache? */
Sage Weilbe655592011-11-30 09:47:09 -0800330 spin_lock(&ci->i_ceph_lock);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800331 if (ceph_test_mount_opt(fsc, DCACHE) &&
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
Sage Weila0dff782010-07-22 13:47:21 -0700333 ceph_snap(inode) != CEPH_SNAPDIR &&
Yan, Zheng70db4f32014-10-21 18:09:56 -0700334 __ceph_dir_is_complete_ordered(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700335 __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) {
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800336 u32 shared_gen = ci->i_shared_gen;
Sage Weilbe655592011-11-30 09:47:09 -0800337 spin_unlock(&ci->i_ceph_lock);
Yan, Zhenga30be7c2014-04-06 14:10:04 +0800338 err = __dcache_readdir(file, ctx, shared_gen);
Sage Weilefa4c122010-10-18 14:04:31 -0700339 if (err != -EAGAIN)
Sage Weil2817b002009-10-06 11:31:08 -0700340 return err;
Sage Weilefa4c122010-10-18 14:04:31 -0700341 } else {
Sage Weilbe655592011-11-30 09:47:09 -0800342 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700343 }
Sage Weil2817b002009-10-06 11:31:08 -0700344
345 /* proceed with a normal readdir */
Sage Weil2817b002009-10-06 11:31:08 -0700346more:
347 /* do we have the correct frag content buffered? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800348 if (need_send_readdir(fi, ctx->pos)) {
Sage Weil2817b002009-10-06 11:31:08 -0700349 struct ceph_mds_request *req;
350 int op = ceph_snap(inode) == CEPH_SNAPDIR ?
351 CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
352
353 /* discard old result, if any */
Sage Weil393f6622010-03-10 12:03:32 -0800354 if (fi->last_readdir) {
Sage Weil2817b002009-10-06 11:31:08 -0700355 ceph_mdsc_put_request(fi->last_readdir);
Sage Weil393f6622010-03-10 12:03:32 -0800356 fi->last_readdir = NULL;
357 }
Sage Weil2817b002009-10-06 11:31:08 -0700358
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800359 if (is_hash_order(ctx->pos)) {
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800360 /* fragtree isn't always accurate. choose frag
361 * based on previous reply when possible. */
362 if (frag == (unsigned)-1)
363 frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
364 NULL, NULL);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800365 } else {
366 frag = fpos_frag(ctx->pos);
367 }
368
Sage Weil2817b002009-10-06 11:31:08 -0700369 dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
370 ceph_vinop(inode), frag, fi->last_name);
371 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
372 if (IS_ERR(req))
373 return PTR_ERR(req);
Yan, Zheng54008392014-03-29 13:41:15 +0800374 err = ceph_alloc_readdir_reply_buffer(req, inode);
375 if (err) {
376 ceph_mdsc_put_request(req);
377 return err;
378 }
Sage Weil2817b002009-10-06 11:31:08 -0700379 /* hints to request -> mds selection code */
380 req->r_direct_mode = USE_AUTH_MDS;
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800381 if (op == CEPH_MDS_OP_READDIR) {
382 req->r_direct_hash = ceph_frag_value(frag);
383 __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
Yan, Zheng87c91a92017-11-23 18:28:16 +0800384 req->r_inode_drop = CEPH_CAP_FILE_EXCL;
Yan, Zheng5d37ca12017-07-26 12:48:08 +0800385 }
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400386 if (fi->last_name) {
Yan, Zheng687265e2015-06-13 17:27:05 +0800387 req->r_path2 = kstrdup(fi->last_name, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400388 if (!req->r_path2) {
389 ceph_mdsc_put_request(req);
390 return -ENOMEM;
391 }
Yan, Zheng79162542017-04-05 12:54:05 -0400392 } else if (is_hash_order(ctx->pos)) {
393 req->r_args.readdir.offset_hash =
394 cpu_to_le32(fpos_hash(ctx->pos));
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400395 }
Yan, Zheng79162542017-04-05 12:54:05 -0400396
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800397 req->r_dir_release_cnt = fi->dir_release_count;
398 req->r_dir_ordered_cnt = fi->dir_ordered_count;
399 req->r_readdir_cache_idx = fi->readdir_cache_idx;
Sage Weil2817b002009-10-06 11:31:08 -0700400 req->r_readdir_offset = fi->next_offset;
401 req->r_args.readdir.frag = cpu_to_le32(frag);
Yan, Zheng956d39d2016-04-27 17:48:30 +0800402 req->r_args.readdir.flags =
403 cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400404
405 req->r_inode = inode;
406 ihold(inode);
407 req->r_dentry = dget(file->f_path.dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700408 err = ceph_mdsc_do_request(mdsc, NULL, req);
409 if (err < 0) {
410 ceph_mdsc_put_request(req);
411 return err;
412 }
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800413 dout("readdir got and parsed readdir result=%d on "
414 "frag %x, end=%d, complete=%d, hash_order=%d\n",
415 err, frag,
Sage Weil2817b002009-10-06 11:31:08 -0700416 (int)req->r_reply_info.dir_end,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800417 (int)req->r_reply_info.dir_complete,
418 (int)req->r_reply_info.hash_order);
Sage Weil2817b002009-10-06 11:31:08 -0700419
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800420 rinfo = &req->r_reply_info;
421 if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
422 frag = le32_to_cpu(rinfo->dir_dir->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800423 if (!rinfo->hash_order) {
424 fi->next_offset = req->r_readdir_offset;
425 /* adjust ctx->pos to beginning of frag */
426 ctx->pos = ceph_make_fpos(frag,
427 fi->next_offset,
428 false);
429 }
Yan, Zheng81c6aea2013-09-18 09:44:13 +0800430 }
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800431
Yan, Zhengf0494202014-02-27 16:26:24 +0800432 fi->frag = frag;
Sage Weil2817b002009-10-06 11:31:08 -0700433 fi->last_readdir = req;
434
Jeff Laytonbc2de10d2017-02-01 13:49:09 -0500435 if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800436 fi->readdir_cache_idx = req->r_readdir_cache_idx;
437 if (fi->readdir_cache_idx < 0) {
438 /* preclude from marking dir ordered */
439 fi->dir_ordered_count = 0;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800440 } else if (ceph_frag_is_leftmost(frag) &&
441 fi->next_offset == 2) {
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800442 /* note dir version at start of readdir so
443 * we can tell if any dentries get dropped */
444 fi->dir_release_count = req->r_dir_release_cnt;
445 fi->dir_ordered_count = req->r_dir_ordered_cnt;
446 }
447 } else {
448 dout("readdir !did_prepopulate");
449 /* disable readdir cache */
450 fi->readdir_cache_idx = -1;
451 /* preclude from marking dir complete */
452 fi->dir_release_count = 0;
453 }
454
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800455 /* note next offset and last dentry name */
456 if (rinfo->dir_nr > 0) {
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800457 struct ceph_mds_reply_dir_entry *rde =
458 rinfo->dir_entries + (rinfo->dir_nr-1);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800459 unsigned next_offset = req->r_reply_info.dir_end ?
460 2 : (fpos_off(rde->offset) + 1);
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800461 err = note_last_dentry(fi, rde->name, rde->name_len,
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800462 next_offset);
Sage Weil2817b002009-10-06 11:31:08 -0700463 if (err)
464 return err;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800465 } else if (req->r_reply_info.dir_end) {
466 fi->next_offset = 2;
467 /* keep last name */
Sage Weil2817b002009-10-06 11:31:08 -0700468 }
469 }
470
471 rinfo = &fi->last_readdir->r_reply_info;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800472 dout("readdir frag %x num %d pos %llx chunk first %llx\n",
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800473 fi->frag, rinfo->dir_nr, ctx->pos,
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800474 rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
Al Viro77acfa22013-05-17 16:52:26 -0400475
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800476 i = 0;
477 /* search start position */
478 if (rinfo->dir_nr > 0) {
479 int step, nr = rinfo->dir_nr;
480 while (nr > 0) {
481 step = nr >> 1;
482 if (rinfo->dir_entries[i + step].offset < ctx->pos) {
483 i += step + 1;
484 nr -= step + 1;
485 } else {
486 nr = step;
487 }
488 }
489 }
490 for (; i < rinfo->dir_nr; i++) {
491 struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
Sage Weil3105c192010-11-18 09:15:07 -0800492 struct ceph_vino vino;
493 ino_t ino;
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800494 u32 ftype;
Sage Weil3105c192010-11-18 09:15:07 -0800495
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800496 BUG_ON(rde->offset < ctx->pos);
497
498 ctx->pos = rde->offset;
499 dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
500 i, rinfo->dir_nr, ctx->pos,
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800501 rde->name_len, rde->name, &rde->inode.in);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800502
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800503 BUG_ON(!rde->inode.in);
504 ftype = le32_to_cpu(rde->inode.in->mode) >> 12;
505 vino.ino = le64_to_cpu(rde->inode.in->ino);
506 vino.snap = le64_to_cpu(rde->inode.in->snapid);
Sage Weil3105c192010-11-18 09:15:07 -0800507 ino = ceph_vino_to_ino(vino);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800508
Yan, Zheng2a5beea2016-04-28 09:37:39 +0800509 if (!dir_emit(ctx, rde->name, rde->name_len,
510 ceph_translate_ino(inode->i_sb, ino), ftype)) {
Sage Weil2817b002009-10-06 11:31:08 -0700511 dout("filldir stopping us...\n");
512 return 0;
513 }
Al Viro77acfa22013-05-17 16:52:26 -0400514 ctx->pos++;
Sage Weil2817b002009-10-06 11:31:08 -0700515 }
516
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800517 ceph_mdsc_put_request(fi->last_readdir);
518 fi->last_readdir = NULL;
519
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800520 if (fi->next_offset > 2) {
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800521 frag = fi->frag;
Sage Weil2817b002009-10-06 11:31:08 -0700522 goto more;
523 }
524
525 /* more frags? */
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800526 if (!ceph_frag_is_rightmost(fi->frag)) {
Yan, Zhengb50c2de2017-04-24 11:56:50 +0800527 frag = ceph_frag_next(fi->frag);
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800528 if (is_hash_order(ctx->pos)) {
529 loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
530 fi->next_offset, true);
531 if (new_pos > ctx->pos)
532 ctx->pos = new_pos;
533 /* keep last_name */
534 } else {
535 ctx->pos = ceph_make_fpos(frag, fi->next_offset, false);
536 kfree(fi->last_name);
537 fi->last_name = NULL;
538 }
Sage Weil2817b002009-10-06 11:31:08 -0700539 dout("readdir next frag is %x\n", frag);
540 goto more;
541 }
Sage Weil9cfa1092011-07-26 11:26:18 -0700542 fi->flags |= CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700543
544 /*
545 * if dir_release_count still matches the dir, no dentries
546 * were released during the whole readdir, and we should have
547 * the complete dir contents in our cache.
548 */
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800549 if (atomic64_read(&ci->i_release_count) == fi->dir_release_count) {
550 spin_lock(&ci->i_ceph_lock);
551 if (fi->dir_ordered_count == atomic64_read(&ci->i_ordered_count)) {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700552 dout(" marking %p complete and ordered\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800553 /* use i_size to track number of entries in
554 * readdir cache */
555 BUG_ON(fi->readdir_cache_idx < 0);
556 i_size_write(inode, fi->readdir_cache_idx *
557 sizeof(struct dentry*));
558 } else {
Yan, Zheng70db4f32014-10-21 18:09:56 -0700559 dout(" marking %p complete\n", inode);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800560 }
Yan, Zheng70db4f32014-10-21 18:09:56 -0700561 __ceph_dir_set_complete(ci, fi->dir_release_count,
562 fi->dir_ordered_count);
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800563 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700564 }
Sage Weil2817b002009-10-06 11:31:08 -0700565
Al Viro77acfa22013-05-17 16:52:26 -0400566 dout("readdir %p file %p done.\n", inode, file);
Sage Weil2817b002009-10-06 11:31:08 -0700567 return 0;
568}
569
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800570static void reset_readdir(struct ceph_file_info *fi)
Sage Weil2817b002009-10-06 11:31:08 -0700571{
572 if (fi->last_readdir) {
573 ceph_mdsc_put_request(fi->last_readdir);
574 fi->last_readdir = NULL;
575 }
576 kfree(fi->last_name);
Sage Weila1629c32010-11-11 15:24:06 -0800577 fi->last_name = NULL;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800578 fi->dir_release_count = 0;
579 fi->readdir_cache_idx = -1;
Yan, Zhenga78600e2016-04-27 17:32:34 +0800580 fi->next_offset = 2; /* compensate for . and .. */
Sage Weil9cfa1092011-07-26 11:26:18 -0700581 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700582}
583
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800584/*
585 * discard buffered readdir content on seekdir(0), or seek to new frag,
586 * or seek prior to current chunk
587 */
588static bool need_reset_readdir(struct ceph_file_info *fi, loff_t new_pos)
589{
590 struct ceph_mds_reply_info_parsed *rinfo;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800591 loff_t chunk_offset;
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800592 if (new_pos == 0)
593 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800594 if (is_hash_order(new_pos)) {
595 /* no need to reset last_name for a forward seek when
596 * dentries are sotred in hash order */
Nicolas Iooss0f5aa882016-08-28 18:47:12 +0200597 } else if (fi->frag != fpos_frag(new_pos)) {
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800598 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800599 }
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800600 rinfo = fi->last_readdir ? &fi->last_readdir->r_reply_info : NULL;
601 if (!rinfo || !rinfo->dir_nr)
602 return true;
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800603 chunk_offset = rinfo->dir_entries[0].offset;
604 return new_pos < chunk_offset ||
605 is_hash_order(new_pos) != is_hash_order(chunk_offset);
Yan, Zheng8974eeb2016-04-28 15:17:40 +0800606}
607
Andrew Morton965c8e52012-12-17 15:59:39 -0800608static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
Sage Weil2817b002009-10-06 11:31:08 -0700609{
610 struct ceph_file_info *fi = file->private_data;
611 struct inode *inode = file->f_mapping->host;
Sage Weil2817b002009-10-06 11:31:08 -0700612 loff_t retval;
613
Al Viro59551022016-01-22 15:40:57 -0500614 inode_lock(inode);
Josef Bacik06222e42011-07-18 13:21:38 -0400615 retval = -EINVAL;
Andrew Morton965c8e52012-12-17 15:59:39 -0800616 switch (whence) {
Sage Weil2817b002009-10-06 11:31:08 -0700617 case SEEK_CUR:
618 offset += file->f_pos;
Josef Bacik06222e42011-07-18 13:21:38 -0400619 case SEEK_SET:
620 break;
Yan, Zhengfdd4e152015-06-16 20:48:56 +0800621 case SEEK_END:
622 retval = -EOPNOTSUPP;
Josef Bacik06222e42011-07-18 13:21:38 -0400623 default:
624 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700625 }
Josef Bacik06222e42011-07-18 13:21:38 -0400626
Yan, Zhengf0494202014-02-27 16:26:24 +0800627 if (offset >= 0) {
Yan, Zhengf3c4ebe2016-04-29 11:27:30 +0800628 if (need_reset_readdir(fi, offset)) {
629 dout("dir_llseek dropping %p content\n", file);
630 reset_readdir(fi);
631 } else if (is_hash_order(offset) && offset > file->f_pos) {
632 /* for hash offset, we don't know if a forward seek
633 * is within same frag */
634 fi->dir_release_count = 0;
635 fi->readdir_cache_idx = -1;
636 }
637
Sage Weil2817b002009-10-06 11:31:08 -0700638 if (offset != file->f_pos) {
639 file->f_pos = offset;
640 file->f_version = 0;
Sage Weil9cfa1092011-07-26 11:26:18 -0700641 fi->flags &= ~CEPH_F_ATEND;
Sage Weil2817b002009-10-06 11:31:08 -0700642 }
643 retval = offset;
Sage Weil2817b002009-10-06 11:31:08 -0700644 }
Josef Bacik06222e42011-07-18 13:21:38 -0400645out:
Al Viro59551022016-01-22 15:40:57 -0500646 inode_unlock(inode);
Sage Weil2817b002009-10-06 11:31:08 -0700647 return retval;
648}
649
650/*
Sage Weil468640e2011-07-26 11:28:11 -0700651 * Handle lookups for the hidden .snap directory.
Sage Weil2817b002009-10-06 11:31:08 -0700652 */
Sage Weil468640e2011-07-26 11:28:11 -0700653int ceph_handle_snapdir(struct ceph_mds_request *req,
654 struct dentry *dentry, int err)
Sage Weil2817b002009-10-06 11:31:08 -0700655{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700656 struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000657 struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
Sage Weil2817b002009-10-06 11:31:08 -0700658
659 /* .snap dir? */
660 if (err == -ENOENT &&
Sage Weil455cec02011-03-03 13:44:35 -0800661 ceph_snap(parent) == CEPH_NOSNAP &&
Sage Weil6b805182009-10-27 11:50:50 -0700662 strcmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700663 fsc->mount_options->snapdir_name) == 0) {
Sage Weil2817b002009-10-06 11:31:08 -0700664 struct inode *inode = ceph_get_snapdir(parent);
Al Viroa4555892014-10-21 20:11:25 -0400665 dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
666 dentry, dentry, inode);
Sage Weil9358c6d2010-03-30 13:54:41 -0700667 BUG_ON(!d_unhashed(dentry));
Sage Weil2817b002009-10-06 11:31:08 -0700668 d_add(dentry, inode);
669 err = 0;
670 }
Sage Weil468640e2011-07-26 11:28:11 -0700671 return err;
672}
Sage Weil2817b002009-10-06 11:31:08 -0700673
Sage Weil468640e2011-07-26 11:28:11 -0700674/*
675 * Figure out final result of a lookup/open request.
676 *
677 * Mainly, make sure we return the final req->r_dentry (if it already
678 * existed) in place of the original VFS-provided dentry when they
679 * differ.
680 *
681 * Gracefully handle the case where the MDS replies with -ENOENT and
682 * no trace (which it may do, at its discretion, e.g., if it doesn't
683 * care to issue a lease on the negative dentry).
684 */
685struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
686 struct dentry *dentry, int err)
687{
Sage Weil2817b002009-10-06 11:31:08 -0700688 if (err == -ENOENT) {
689 /* no trace? */
690 err = 0;
691 if (!req->r_reply_info.head->is_dentry) {
692 dout("ENOENT and no trace, dentry %p inode %p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000693 dentry, d_inode(dentry));
694 if (d_really_is_positive(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700695 d_drop(dentry);
696 err = -ENOENT;
697 } else {
698 d_add(dentry, NULL);
699 }
700 }
701 }
702 if (err)
703 dentry = ERR_PTR(err);
704 else if (dentry != req->r_dentry)
705 dentry = dget(req->r_dentry); /* we got spliced */
706 else
707 dentry = NULL;
708 return dentry;
709}
710
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -0400711static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
Sage Weil1d1de9162009-12-02 11:54:25 -0800712{
713 return ceph_ino(inode) == CEPH_INO_ROOT &&
714 strncmp(dentry->d_name.name, ".ceph", 5) == 0;
715}
716
Sage Weil2817b002009-10-06 11:31:08 -0700717/*
718 * Look up a single dir entry. If there is a lookup intent, inform
719 * the MDS so that it gets our 'caps wanted' value in a single op.
720 */
721static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400722 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -0700723{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700724 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
725 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700726 struct ceph_mds_request *req;
727 int op;
Yan, Zheng315f2402016-03-07 10:34:50 +0800728 int mask;
Sage Weil2817b002009-10-06 11:31:08 -0700729 int err;
730
Al Viroa4555892014-10-21 20:11:25 -0400731 dout("lookup %p dentry %p '%pd'\n",
732 dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700733
734 if (dentry->d_name.len > NAME_MAX)
735 return ERR_PTR(-ENAMETOOLONG);
736
Sage Weil2817b002009-10-06 11:31:08 -0700737 /* can we conclude ENOENT locally? */
David Howells2b0143b2015-03-17 22:25:59 +0000738 if (d_really_is_negative(dentry)) {
Sage Weil2817b002009-10-06 11:31:08 -0700739 struct ceph_inode_info *ci = ceph_inode(dir);
740 struct ceph_dentry_info *di = ceph_dentry(dentry);
741
Sage Weilbe655592011-11-30 09:47:09 -0800742 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700743 dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags);
744 if (strncmp(dentry->d_name.name,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700745 fsc->mount_options->snapdir_name,
Sage Weil2817b002009-10-06 11:31:08 -0700746 dentry->d_name.len) &&
Sage Weil1d1de9162009-12-02 11:54:25 -0800747 !is_root_ceph_dentry(dir, dentry) &&
Yan, Zhenge2c3de02015-03-04 16:05:04 +0800748 ceph_test_mount_opt(fsc, DCACHE) &&
Yan, Zheng2f276c52013-03-13 19:44:32 +0800749 __ceph_dir_is_complete(ci) &&
Sage Weil2817b002009-10-06 11:31:08 -0700750 (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) {
Sage Weilbe655592011-11-30 09:47:09 -0800751 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700752 dout(" dir %p complete, -ENOENT\n", dir);
753 d_add(dentry, NULL);
754 di->lease_shared_gen = ci->i_shared_gen;
755 return NULL;
756 }
Sage Weilbe655592011-11-30 09:47:09 -0800757 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -0700758 }
759
760 op = ceph_snap(dir) == CEPH_SNAPDIR ?
761 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
762 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
763 if (IS_ERR(req))
Julia Lawall7e34bc52010-05-22 12:01:14 +0200764 return ERR_CAST(req);
Sage Weil2817b002009-10-06 11:31:08 -0700765 req->r_dentry = dget(dentry);
766 req->r_num_caps = 2;
Yan, Zheng315f2402016-03-07 10:34:50 +0800767
768 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
769 if (ceph_security_xattr_wanted(dir))
770 mask |= CEPH_CAP_XATTR_SHARED;
771 req->r_args.getattr.mask = cpu_to_le32(mask);
772
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500773 req->r_parent = dir;
774 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700775 err = ceph_mdsc_do_request(mdsc, NULL, req);
Sage Weil468640e2011-07-26 11:28:11 -0700776 err = ceph_handle_snapdir(req, dentry, err);
Sage Weil2817b002009-10-06 11:31:08 -0700777 dentry = ceph_finish_lookup(req, dentry, err);
778 ceph_mdsc_put_request(req); /* will dput(dentry) */
779 dout("lookup result=%p\n", dentry);
780 return dentry;
781}
782
783/*
784 * If we do a create but get no trace back from the MDS, follow up with
785 * a lookup (the VFS expects us to link up the provided dentry).
786 */
787int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
788{
Al Viro00cd8dd2012-06-10 17:13:09 -0400789 struct dentry *result = ceph_lookup(dir, dentry, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700790
791 if (result && !IS_ERR(result)) {
792 /*
793 * We created the item, then did a lookup, and found
794 * it was already linked to another inode we already
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800795 * had in our cache (and thus got spliced). To not
796 * confuse VFS (especially when inode is a directory),
797 * we don't link our dentry to that inode, return an
798 * error instead.
799 *
800 * This event should be rare and it happens only when
801 * we talk to old MDS. Recent MDS does not send traceless
802 * reply for request that creates new inode.
Sage Weil2817b002009-10-06 11:31:08 -0700803 */
Yan, Zheng5cba3722015-02-02 11:27:56 +0800804 d_drop(result);
Yan, Zheng4d41cef2015-02-04 15:10:48 +0800805 return -ESTALE;
Sage Weil2817b002009-10-06 11:31:08 -0700806 }
807 return PTR_ERR(result);
808}
809
810static int ceph_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400811 umode_t mode, dev_t rdev)
Sage Weil2817b002009-10-06 11:31:08 -0700812{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700813 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
814 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700815 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800816 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700817 int err;
818
819 if (ceph_snap(dir) != CEPH_NOSNAP)
820 return -EROFS;
821
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800822 err = ceph_pre_init_acls(dir, &mode, &acls);
823 if (err < 0)
824 return err;
825
Al Viro1a67aaf2011-07-26 01:52:52 -0400826 dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
Sage Weil2817b002009-10-06 11:31:08 -0700827 dir, dentry, mode, rdev);
828 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
829 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800830 err = PTR_ERR(req);
831 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700832 }
833 req->r_dentry = dget(dentry);
834 req->r_num_caps = 2;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500835 req->r_parent = dir;
836 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700837 req->r_args.mknod.mode = cpu_to_le32(mode);
838 req->r_args.mknod.rdev = cpu_to_le32(rdev);
Yan, Zheng222b7f92017-11-23 17:47:15 +0800839 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
Sage Weil2817b002009-10-06 11:31:08 -0700840 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800841 if (acls.pagelist) {
842 req->r_pagelist = acls.pagelist;
843 acls.pagelist = NULL;
844 }
Sage Weil2817b002009-10-06 11:31:08 -0700845 err = ceph_mdsc_do_request(mdsc, dir, req);
846 if (!err && !req->r_reply_info.head->is_dentry)
847 err = ceph_handle_notrace_create(dir, dentry);
848 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800849out:
Guangliang Zhao7221fe42013-11-11 15:18:03 +0800850 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000851 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800852 else
Sage Weil2817b002009-10-06 11:31:08 -0700853 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800854 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700855 return err;
856}
857
Al Viro4acdaf22011-07-26 01:42:34 -0400858static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400859 bool excl)
Sage Weil2817b002009-10-06 11:31:08 -0700860{
Miklos Szeredi2d83bde2012-06-05 15:10:25 +0200861 return ceph_mknod(dir, dentry, mode, 0);
Sage Weil2817b002009-10-06 11:31:08 -0700862}
863
864static int ceph_symlink(struct inode *dir, struct dentry *dentry,
865 const char *dest)
866{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700867 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
868 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700869 struct ceph_mds_request *req;
870 int err;
871
872 if (ceph_snap(dir) != CEPH_NOSNAP)
873 return -EROFS;
874
875 dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
876 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
877 if (IS_ERR(req)) {
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800878 err = PTR_ERR(req);
879 goto out;
Sage Weil2817b002009-10-06 11:31:08 -0700880 }
Yan, Zheng687265e2015-06-13 17:27:05 +0800881 req->r_path2 = kstrdup(dest, GFP_KERNEL);
Sanidhya Kashyapa149bb92015-03-21 12:54:58 -0400882 if (!req->r_path2) {
883 err = -ENOMEM;
884 ceph_mdsc_put_request(req);
885 goto out;
886 }
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500887 req->r_parent = dir;
888 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700889 req->r_dentry = dget(dentry);
890 req->r_num_caps = 2;
Yan, Zheng222b7f92017-11-23 17:47:15 +0800891 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
Sage Weil2817b002009-10-06 11:31:08 -0700892 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
893 err = ceph_mdsc_do_request(mdsc, dir, req);
894 if (!err && !req->r_reply_info.head->is_dentry)
895 err = ceph_handle_notrace_create(dir, dentry);
896 ceph_mdsc_put_request(req);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800897out:
898 if (err)
Sage Weil2817b002009-10-06 11:31:08 -0700899 d_drop(dentry);
900 return err;
901}
902
Al Viro18bb1db2011-07-26 01:41:39 -0400903static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Sage Weil2817b002009-10-06 11:31:08 -0700904{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700905 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
906 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700907 struct ceph_mds_request *req;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800908 struct ceph_acls_info acls = {};
Sage Weil2817b002009-10-06 11:31:08 -0700909 int err = -EROFS;
910 int op;
911
912 if (ceph_snap(dir) == CEPH_SNAPDIR) {
913 /* mkdir .snap/foo is a MKSNAP */
914 op = CEPH_MDS_OP_MKSNAP;
Al Viroa4555892014-10-21 20:11:25 -0400915 dout("mksnap dir %p snap '%pd' dn %p\n", dir,
916 dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -0700917 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
Al Viro18bb1db2011-07-26 01:41:39 -0400918 dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
Sage Weil2817b002009-10-06 11:31:08 -0700919 op = CEPH_MDS_OP_MKDIR;
920 } else {
921 goto out;
922 }
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800923
924 mode |= S_IFDIR;
925 err = ceph_pre_init_acls(dir, &mode, &acls);
926 if (err < 0)
927 goto out;
928
Sage Weil2817b002009-10-06 11:31:08 -0700929 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
930 if (IS_ERR(req)) {
931 err = PTR_ERR(req);
932 goto out;
933 }
934
935 req->r_dentry = dget(dentry);
936 req->r_num_caps = 2;
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500937 req->r_parent = dir;
938 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700939 req->r_args.mkdir.mode = cpu_to_le32(mode);
Yan, Zheng222b7f92017-11-23 17:47:15 +0800940 req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
Sage Weil2817b002009-10-06 11:31:08 -0700941 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800942 if (acls.pagelist) {
943 req->r_pagelist = acls.pagelist;
944 acls.pagelist = NULL;
945 }
Sage Weil2817b002009-10-06 11:31:08 -0700946 err = ceph_mdsc_do_request(mdsc, dir, req);
Yan, Zheng275dd192014-12-10 16:17:31 +0800947 if (!err &&
948 !req->r_reply_info.head->is_target &&
949 !req->r_reply_info.head->is_dentry)
Sage Weil2817b002009-10-06 11:31:08 -0700950 err = ceph_handle_notrace_create(dir, dentry);
951 ceph_mdsc_put_request(req);
952out:
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800953 if (!err)
David Howells2b0143b2015-03-17 22:25:59 +0000954 ceph_init_inode_acls(d_inode(dentry), &acls);
Yan, Zhengb20a95a2014-02-11 12:55:05 +0800955 else
Sage Weil2817b002009-10-06 11:31:08 -0700956 d_drop(dentry);
Yan, Zhengb1ee94a2014-09-16 20:35:17 +0800957 ceph_release_acls_info(&acls);
Sage Weil2817b002009-10-06 11:31:08 -0700958 return err;
959}
960
961static int ceph_link(struct dentry *old_dentry, struct inode *dir,
962 struct dentry *dentry)
963{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700964 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
965 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -0700966 struct ceph_mds_request *req;
967 int err;
968
969 if (ceph_snap(dir) != CEPH_NOSNAP)
970 return -EROFS;
971
972 dout("link in dir %p old_dentry %p dentry %p\n", dir,
973 old_dentry, dentry);
974 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
975 if (IS_ERR(req)) {
976 d_drop(dentry);
977 return PTR_ERR(req);
978 }
979 req->r_dentry = dget(dentry);
980 req->r_num_caps = 2;
Sage Weil4b58c9b192013-02-05 13:41:23 -0800981 req->r_old_dentry = dget(old_dentry);
Jeff Layton3dd69aa2017-01-31 10:28:26 -0500982 req->r_parent = dir;
983 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -0700984 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
985 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
Yan, Zhengad88f232013-07-21 20:25:25 +0800986 /* release LINK_SHARED on source inode (mds will lock it) */
Yan, Zhengd19a0b52017-11-23 17:59:13 +0800987 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
Sage Weil2817b002009-10-06 11:31:08 -0700988 err = ceph_mdsc_do_request(mdsc, dir, req);
Sage Weil70b666c2011-05-27 09:24:26 -0700989 if (err) {
Sage Weil2817b002009-10-06 11:31:08 -0700990 d_drop(dentry);
Sage Weil70b666c2011-05-27 09:24:26 -0700991 } else if (!req->r_reply_info.head->is_dentry) {
David Howells2b0143b2015-03-17 22:25:59 +0000992 ihold(d_inode(old_dentry));
993 d_instantiate(dentry, d_inode(old_dentry));
Sage Weil70b666c2011-05-27 09:24:26 -0700994 }
Sage Weil2817b002009-10-06 11:31:08 -0700995 ceph_mdsc_put_request(req);
996 return err;
997}
998
999/*
1000 * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it
1001 * looks like the link count will hit 0, drop any other caps (other
1002 * than PIN) we don't specifically want (due to the file still being
1003 * open).
1004 */
1005static int drop_caps_for_unlink(struct inode *inode)
1006{
1007 struct ceph_inode_info *ci = ceph_inode(inode);
1008 int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1009
Sage Weilbe655592011-11-30 09:47:09 -08001010 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001011 if (inode->i_nlink == 1) {
1012 drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN);
1013 ci->i_ceph_flags |= CEPH_I_NODELAY;
1014 }
Sage Weilbe655592011-11-30 09:47:09 -08001015 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001016 return drop;
1017}
1018
1019/*
1020 * rmdir and unlink are differ only by the metadata op code
1021 */
1022static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1023{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001024 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1025 struct ceph_mds_client *mdsc = fsc->mdsc;
David Howells2b0143b2015-03-17 22:25:59 +00001026 struct inode *inode = d_inode(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001027 struct ceph_mds_request *req;
1028 int err = -EROFS;
1029 int op;
1030
1031 if (ceph_snap(dir) == CEPH_SNAPDIR) {
1032 /* rmdir .snap/foo is RMSNAP */
Al Viroa4555892014-10-21 20:11:25 -04001033 dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001034 op = CEPH_MDS_OP_RMSNAP;
1035 } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1036 dout("unlink/rmdir dir %p dn %p inode %p\n",
1037 dir, dentry, inode);
David Howellse36cb0b2015-01-29 12:02:35 +00001038 op = d_is_dir(dentry) ?
Sage Weil2817b002009-10-06 11:31:08 -07001039 CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1040 } else
1041 goto out;
1042 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1043 if (IS_ERR(req)) {
1044 err = PTR_ERR(req);
1045 goto out;
1046 }
1047 req->r_dentry = dget(dentry);
1048 req->r_num_caps = 2;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001049 req->r_parent = dir;
1050 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -07001051 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1052 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1053 req->r_inode_drop = drop_caps_for_unlink(inode);
1054 err = ceph_mdsc_do_request(mdsc, dir, req);
1055 if (!err && !req->r_reply_info.head->is_dentry)
1056 d_delete(dentry);
1057 ceph_mdsc_put_request(req);
1058out:
1059 return err;
1060}
1061
1062static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001063 struct inode *new_dir, struct dentry *new_dentry,
1064 unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001065{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001066 struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb);
1067 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001068 struct ceph_mds_request *req;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001069 int op = CEPH_MDS_OP_RENAME;
Sage Weil2817b002009-10-06 11:31:08 -07001070 int err;
1071
Miklos Szeredi1cd66c92016-09-27 11:03:58 +02001072 if (flags)
1073 return -EINVAL;
1074
Sage Weil2817b002009-10-06 11:31:08 -07001075 if (ceph_snap(old_dir) != ceph_snap(new_dir))
1076 return -EXDEV;
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001077 if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1078 if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1079 op = CEPH_MDS_OP_RENAMESNAP;
1080 else
1081 return -EROFS;
1082 }
Sage Weil2817b002009-10-06 11:31:08 -07001083 dout("rename dir %p dentry %p to dir %p dentry %p\n",
1084 old_dir, old_dentry, new_dir, new_dentry);
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001085 req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
Sage Weil2817b002009-10-06 11:31:08 -07001086 if (IS_ERR(req))
1087 return PTR_ERR(req);
Sage Weil180061a2013-02-05 13:36:05 -08001088 ihold(old_dir);
Sage Weil2817b002009-10-06 11:31:08 -07001089 req->r_dentry = dget(new_dentry);
1090 req->r_num_caps = 2;
1091 req->r_old_dentry = dget(old_dentry);
Sage Weil180061a2013-02-05 13:36:05 -08001092 req->r_old_dentry_dir = old_dir;
Jeff Layton3dd69aa2017-01-31 10:28:26 -05001093 req->r_parent = new_dir;
1094 set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
Sage Weil2817b002009-10-06 11:31:08 -07001095 req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1096 req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1097 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1098 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1099 /* release LINK_RDCACHE on source inode (mds will lock it) */
Yan, Zhengd19a0b52017-11-23 17:59:13 +08001100 req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
David Howells2b0143b2015-03-17 22:25:59 +00001101 if (d_really_is_positive(new_dentry))
1102 req->r_inode_drop = drop_caps_for_unlink(d_inode(new_dentry));
Sage Weil2817b002009-10-06 11:31:08 -07001103 err = ceph_mdsc_do_request(mdsc, old_dir, req);
1104 if (!err && !req->r_reply_info.head->is_dentry) {
1105 /*
1106 * Normally d_move() is done by fill_trace (called by
1107 * do_request, above). If there is no trace, we need
1108 * to do it here.
1109 */
Sage Weilea1409f2010-04-28 16:12:06 -07001110
Yan, Zhengfdd4e152015-06-16 20:48:56 +08001111 /* d_move screws up sibling dentries' offsets */
1112 ceph_dir_clear_complete(old_dir);
1113 ceph_dir_clear_complete(new_dir);
1114
Sage Weil2817b002009-10-06 11:31:08 -07001115 d_move(old_dentry, new_dentry);
Sage Weilea1409f2010-04-28 16:12:06 -07001116
1117 /* ensure target dentry is invalidated, despite
1118 rehashing bug in vfs_rename_dir */
Sage Weil81a6cf22010-05-14 09:35:38 -07001119 ceph_invalidate_dentry_lease(new_dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001120 }
1121 ceph_mdsc_put_request(req);
1122 return err;
1123}
1124
Sage Weil81a6cf22010-05-14 09:35:38 -07001125/*
1126 * Ensure a dentry lease will no longer revalidate.
1127 */
1128void ceph_invalidate_dentry_lease(struct dentry *dentry)
1129{
1130 spin_lock(&dentry->d_lock);
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02001131 ceph_dentry(dentry)->time = jiffies;
Sage Weil81a6cf22010-05-14 09:35:38 -07001132 ceph_dentry(dentry)->lease_shared_gen = 0;
1133 spin_unlock(&dentry->d_lock);
1134}
Sage Weil2817b002009-10-06 11:31:08 -07001135
1136/*
1137 * Check if dentry lease is valid. If not, delete the lease. Try to
1138 * renew if the least is more than half up.
1139 */
Jeff Layton14fb9c92016-07-01 09:39:20 -04001140static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags,
1141 struct inode *dir)
Sage Weil2817b002009-10-06 11:31:08 -07001142{
1143 struct ceph_dentry_info *di;
1144 struct ceph_mds_session *s;
1145 int valid = 0;
1146 u32 gen;
1147 unsigned long ttl;
1148 struct ceph_mds_session *session = NULL;
Sage Weil2817b002009-10-06 11:31:08 -07001149 u32 seq = 0;
1150
1151 spin_lock(&dentry->d_lock);
1152 di = ceph_dentry(dentry);
Jeff Layton14fb9c92016-07-01 09:39:20 -04001153 if (di && di->lease_session) {
Sage Weil2817b002009-10-06 11:31:08 -07001154 s = di->lease_session;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001155 spin_lock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001156 gen = s->s_cap_gen;
1157 ttl = s->s_cap_ttl;
Alex Elderd8fb02a2012-01-12 17:48:10 -08001158 spin_unlock(&s->s_gen_ttl_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001159
1160 if (di->lease_gen == gen &&
Miklos Szeredi9b16f03c2016-06-22 16:35:04 +02001161 time_before(jiffies, di->time) &&
Sage Weil2817b002009-10-06 11:31:08 -07001162 time_before(jiffies, ttl)) {
1163 valid = 1;
1164 if (di->lease_renew_after &&
1165 time_after(jiffies, di->lease_renew_after)) {
Jeff Layton14fb9c92016-07-01 09:39:20 -04001166 /*
1167 * We should renew. If we're in RCU walk mode
1168 * though, we can't do that so just return
1169 * -ECHILD.
1170 */
1171 if (flags & LOOKUP_RCU) {
1172 valid = -ECHILD;
1173 } else {
1174 session = ceph_get_mds_session(s);
1175 seq = di->lease_seq;
1176 di->lease_renew_after = 0;
1177 di->lease_renew_from = jiffies;
1178 }
Sage Weil2817b002009-10-06 11:31:08 -07001179 }
Sage Weil2817b002009-10-06 11:31:08 -07001180 }
1181 }
1182 spin_unlock(&dentry->d_lock);
1183
1184 if (session) {
1185 ceph_mdsc_lease_send_msg(session, dir, dentry,
1186 CEPH_MDS_LEASE_RENEW, seq);
1187 ceph_put_mds_session(session);
1188 }
1189 dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1190 return valid;
1191}
1192
1193/*
1194 * Check if directory-wide content lease/cap is valid.
1195 */
1196static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry)
1197{
1198 struct ceph_inode_info *ci = ceph_inode(dir);
1199 struct ceph_dentry_info *di = ceph_dentry(dentry);
1200 int valid = 0;
1201
Sage Weilbe655592011-11-30 09:47:09 -08001202 spin_lock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001203 if (ci->i_shared_gen == di->lease_shared_gen)
1204 valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
Sage Weilbe655592011-11-30 09:47:09 -08001205 spin_unlock(&ci->i_ceph_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001206 dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n",
1207 dir, (unsigned)ci->i_shared_gen, dentry,
1208 (unsigned)di->lease_shared_gen, valid);
1209 return valid;
1210}
1211
1212/*
1213 * Check if cached dentry can be trusted.
1214 */
Al Viro0b728e12012-06-10 16:03:43 -04001215static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
Sage Weil2817b002009-10-06 11:31:08 -07001216{
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001217 int valid = 0;
Yan, Zheng641235d2016-03-16 16:40:23 +08001218 struct dentry *parent;
Nick Piggin34286d62011-01-07 17:49:57 +11001219 struct inode *dir;
1220
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001221 if (flags & LOOKUP_RCU) {
Seraphime Kirkovski52953d52016-12-26 10:26:34 +01001222 parent = READ_ONCE(dentry->d_parent);
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001223 dir = d_inode_rcu(parent);
1224 if (!dir)
1225 return -ECHILD;
1226 } else {
1227 parent = dget_parent(dentry);
1228 dir = d_inode(parent);
1229 }
Nick Piggin34286d62011-01-07 17:49:57 +11001230
Al Viroa4555892014-10-21 20:11:25 -04001231 dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001232 dentry, d_inode(dentry), ceph_dentry(dentry)->offset);
Sage Weil2817b002009-10-06 11:31:08 -07001233
1234 /* always trust cached snapped dentries, snapdir dentry */
1235 if (ceph_snap(dir) != CEPH_NOSNAP) {
Al Viroa4555892014-10-21 20:11:25 -04001236 dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
David Howells2b0143b2015-03-17 22:25:59 +00001237 dentry, d_inode(dentry));
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001238 valid = 1;
David Howells2b0143b2015-03-17 22:25:59 +00001239 } else if (d_really_is_positive(dentry) &&
1240 ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001241 valid = 1;
Jeff Layton14fb9c92016-07-01 09:39:20 -04001242 } else {
1243 valid = dentry_lease_is_valid(dentry, flags, dir);
1244 if (valid == -ECHILD)
1245 return valid;
1246 if (valid || dir_lease_is_valid(dir, dentry)) {
1247 if (d_really_is_positive(dentry))
1248 valid = ceph_is_any_caps(d_inode(dentry));
1249 else
1250 valid = 1;
1251 }
Sage Weil2817b002009-10-06 11:31:08 -07001252 }
Sage Weil2817b002009-10-06 11:31:08 -07001253
Yan, Zheng200fd272016-03-17 14:41:59 +08001254 if (!valid) {
1255 struct ceph_mds_client *mdsc =
1256 ceph_sb_to_client(dir->i_sb)->mdsc;
1257 struct ceph_mds_request *req;
Jeff Layton10976802017-01-12 14:42:38 -05001258 int op, err;
1259 u32 mask;
Yan, Zheng200fd272016-03-17 14:41:59 +08001260
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001261 if (flags & LOOKUP_RCU)
1262 return -ECHILD;
1263
Yan, Zheng200fd272016-03-17 14:41:59 +08001264 op = ceph_snap(dir) == CEPH_SNAPDIR ?
Jeff Layton5eb9f602017-01-30 09:47:25 -05001265 CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
Yan, Zheng200fd272016-03-17 14:41:59 +08001266 req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1267 if (!IS_ERR(req)) {
1268 req->r_dentry = dget(dentry);
Jeff Layton5eb9f602017-01-30 09:47:25 -05001269 req->r_num_caps = 2;
1270 req->r_parent = dir;
Yan, Zheng200fd272016-03-17 14:41:59 +08001271
1272 mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1273 if (ceph_security_xattr_wanted(dir))
1274 mask |= CEPH_CAP_XATTR_SHARED;
Jeff Layton10976802017-01-12 14:42:38 -05001275 req->r_args.getattr.mask = cpu_to_le32(mask);
Yan, Zheng200fd272016-03-17 14:41:59 +08001276
Yan, Zheng200fd272016-03-17 14:41:59 +08001277 err = ceph_mdsc_do_request(mdsc, NULL, req);
Jeff Laytonc3f46882016-11-30 15:56:46 -05001278 switch (err) {
1279 case 0:
1280 if (d_really_is_positive(dentry) &&
1281 d_inode(dentry) == req->r_target_inode)
1282 valid = 1;
1283 break;
1284 case -ENOENT:
1285 if (d_really_is_negative(dentry))
1286 valid = 1;
1287 /* Fallthrough */
1288 default:
1289 break;
Yan, Zheng200fd272016-03-17 14:41:59 +08001290 }
1291 ceph_mdsc_put_request(req);
1292 dout("d_revalidate %p lookup result=%d\n",
1293 dentry, err);
1294 }
1295 }
1296
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001297 dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
Yan, Zheng9215aee2013-11-30 12:47:41 +08001298 if (valid) {
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001299 ceph_dentry_lru_touch(dentry);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001300 } else {
1301 ceph_dir_clear_complete(dir);
Yan, Zheng9215aee2013-11-30 12:47:41 +08001302 }
Yan, Zheng641235d2016-03-16 16:40:23 +08001303
Jeff Laytonf49d1e02016-07-01 09:39:21 -04001304 if (!(flags & LOOKUP_RCU))
1305 dput(parent);
Sage Weilbf1c6ac2011-07-26 11:30:43 -07001306 return valid;
Sage Weil2817b002009-10-06 11:31:08 -07001307}
1308
1309/*
Sage Weil147851d2011-03-15 14:57:41 -07001310 * Release our ceph_dentry_info.
Sage Weil2817b002009-10-06 11:31:08 -07001311 */
Sage Weil147851d2011-03-15 14:57:41 -07001312static void ceph_d_release(struct dentry *dentry)
Sage Weil2817b002009-10-06 11:31:08 -07001313{
1314 struct ceph_dentry_info *di = ceph_dentry(dentry);
Sage Weil2817b002009-10-06 11:31:08 -07001315
Sage Weil147851d2011-03-15 14:57:41 -07001316 dout("d_release %p\n", dentry);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001317 ceph_dentry_lru_del(dentry);
Jeff Layton5b484a52016-07-01 09:39:20 -04001318
1319 spin_lock(&dentry->d_lock);
1320 dentry->d_fsdata = NULL;
1321 spin_unlock(&dentry->d_lock);
1322
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001323 if (di->lease_session)
1324 ceph_put_mds_session(di->lease_session);
1325 kmem_cache_free(ceph_dentry_cachep, di);
Sage Weil2817b002009-10-06 11:31:08 -07001326}
1327
Sage Weilb58dc412011-03-15 15:53:40 -07001328/*
1329 * When the VFS prunes a dentry from the cache, we need to clear the
1330 * complete flag on the parent directory.
1331 *
1332 * Called under dentry->d_lock.
1333 */
1334static void ceph_d_prune(struct dentry *dentry)
1335{
Sage Weil774ac212011-11-11 09:48:08 -08001336 dout("ceph_d_prune %p\n", dentry);
Sage Weilb58dc412011-03-15 15:53:40 -07001337
1338 /* do we have a valid parent? */
Sage Weil8842b3b2012-06-07 13:43:35 -07001339 if (IS_ROOT(dentry))
Sage Weilb58dc412011-03-15 15:53:40 -07001340 return;
1341
Yan, Zheng2f276c52013-03-13 19:44:32 +08001342 /* if we are not hashed, we don't affect dir's completeness */
Sage Weilb58dc412011-03-15 15:53:40 -07001343 if (d_unhashed(dentry))
1344 return;
1345
Al Viro18fc8ab2016-10-28 21:52:50 -04001346 if (ceph_snap(d_inode(dentry->d_parent)) == CEPH_SNAPDIR)
1347 return;
1348
Sage Weilb58dc412011-03-15 15:53:40 -07001349 /*
1350 * we hold d_lock, so d_parent is stable, and d_fsdata is never
1351 * cleared until d_release
1352 */
David Howells2b0143b2015-03-17 22:25:59 +00001353 ceph_dir_clear_complete(d_inode(dentry->d_parent));
Sage Weilb58dc412011-03-15 15:53:40 -07001354}
Sage Weil2817b002009-10-06 11:31:08 -07001355
1356/*
1357 * read() on a dir. This weird interface hack only works if mounted
1358 * with '-o dirstat'.
1359 */
1360static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1361 loff_t *ppos)
1362{
1363 struct ceph_file_info *cf = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05001364 struct inode *inode = file_inode(file);
Sage Weil2817b002009-10-06 11:31:08 -07001365 struct ceph_inode_info *ci = ceph_inode(inode);
1366 int left;
Sage Weilae598082011-05-12 14:28:05 -07001367 const int bufsize = 1024;
Sage Weil2817b002009-10-06 11:31:08 -07001368
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001369 if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
Sage Weil2817b002009-10-06 11:31:08 -07001370 return -EISDIR;
1371
1372 if (!cf->dir_info) {
Yan, Zheng687265e2015-06-13 17:27:05 +08001373 cf->dir_info = kmalloc(bufsize, GFP_KERNEL);
Sage Weil2817b002009-10-06 11:31:08 -07001374 if (!cf->dir_info)
1375 return -ENOMEM;
1376 cf->dir_info_len =
Sage Weilae598082011-05-12 14:28:05 -07001377 snprintf(cf->dir_info, bufsize,
Sage Weil2817b002009-10-06 11:31:08 -07001378 "entries: %20lld\n"
1379 " files: %20lld\n"
1380 " subdirs: %20lld\n"
1381 "rentries: %20lld\n"
1382 " rfiles: %20lld\n"
1383 " rsubdirs: %20lld\n"
1384 "rbytes: %20lld\n"
1385 "rctime: %10ld.%09ld\n",
1386 ci->i_files + ci->i_subdirs,
1387 ci->i_files,
1388 ci->i_subdirs,
1389 ci->i_rfiles + ci->i_rsubdirs,
1390 ci->i_rfiles,
1391 ci->i_rsubdirs,
1392 ci->i_rbytes,
1393 (long)ci->i_rctime.tv_sec,
1394 (long)ci->i_rctime.tv_nsec);
1395 }
1396
1397 if (*ppos >= cf->dir_info_len)
1398 return 0;
1399 size = min_t(unsigned, size, cf->dir_info_len-*ppos);
1400 left = copy_to_user(buf, cf->dir_info + *ppos, size);
1401 if (left == size)
1402 return -EFAULT;
1403 *ppos += (size - left);
1404 return size - left;
1405}
1406
1407/*
Sage Weil2817b002009-10-06 11:31:08 -07001408 * We maintain a private dentry LRU.
1409 *
1410 * FIXME: this needs to be changed to a per-mds lru to be useful.
1411 */
1412void ceph_dentry_lru_add(struct dentry *dn)
1413{
1414 struct ceph_dentry_info *di = ceph_dentry(dn);
1415 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001416
Al Viroa4555892014-10-21 20:11:25 -04001417 dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001418 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1419 spin_lock(&mdsc->dentry_lru_lock);
1420 list_add_tail(&di->lru, &mdsc->dentry_lru);
1421 mdsc->num_dentry++;
1422 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001423}
1424
1425void ceph_dentry_lru_touch(struct dentry *dn)
1426{
1427 struct ceph_dentry_info *di = ceph_dentry(dn);
1428 struct ceph_mds_client *mdsc;
Sage Weil2817b002009-10-06 11:31:08 -07001429
Al Viroa4555892014-10-21 20:11:25 -04001430 dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn,
1431 di->offset);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001432 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1433 spin_lock(&mdsc->dentry_lru_lock);
1434 list_move_tail(&di->lru, &mdsc->dentry_lru);
1435 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001436}
1437
1438void ceph_dentry_lru_del(struct dentry *dn)
1439{
1440 struct ceph_dentry_info *di = ceph_dentry(dn);
1441 struct ceph_mds_client *mdsc;
1442
Al Viroa4555892014-10-21 20:11:25 -04001443 dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn);
Sage Weil3d8eb7a2011-11-11 09:48:53 -08001444 mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1445 spin_lock(&mdsc->dentry_lru_lock);
1446 list_del_init(&di->lru);
1447 mdsc->num_dentry--;
1448 spin_unlock(&mdsc->dentry_lru_lock);
Sage Weil2817b002009-10-06 11:31:08 -07001449}
1450
Sage Weil6c0f3af2010-11-16 11:14:34 -08001451/*
1452 * Return name hash for a given dentry. This is dependent on
1453 * the parent directory's hash function.
1454 */
Sage Weile5f86dc2011-07-26 11:30:55 -07001455unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
Sage Weil6c0f3af2010-11-16 11:14:34 -08001456{
Sage Weil6c0f3af2010-11-16 11:14:34 -08001457 struct ceph_inode_info *dci = ceph_inode(dir);
1458
1459 switch (dci->i_dir_layout.dl_dir_hash) {
1460 case 0: /* for backward compat */
1461 case CEPH_STR_HASH_LINUX:
1462 return dn->d_name.hash;
1463
1464 default:
1465 return ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1466 dn->d_name.name, dn->d_name.len);
1467 }
1468}
1469
Sage Weil2817b002009-10-06 11:31:08 -07001470const struct file_operations ceph_dir_fops = {
1471 .read = ceph_read_dir,
Al Viro77acfa22013-05-17 16:52:26 -04001472 .iterate = ceph_readdir,
Sage Weil2817b002009-10-06 11:31:08 -07001473 .llseek = ceph_dir_llseek,
1474 .open = ceph_open,
1475 .release = ceph_release,
1476 .unlocked_ioctl = ceph_ioctl,
Yan, Zhengda819c82015-05-27 11:19:34 +08001477 .fsync = ceph_fsync,
Sage Weil2817b002009-10-06 11:31:08 -07001478};
1479
Yan, Zheng38c48b52015-01-14 13:46:04 +08001480const struct file_operations ceph_snapdir_fops = {
1481 .iterate = ceph_readdir,
1482 .llseek = ceph_dir_llseek,
1483 .open = ceph_open,
1484 .release = ceph_release,
1485};
1486
Sage Weil2817b002009-10-06 11:31:08 -07001487const struct inode_operations ceph_dir_iops = {
1488 .lookup = ceph_lookup,
1489 .permission = ceph_permission,
1490 .getattr = ceph_getattr,
1491 .setattr = ceph_setattr,
Sage Weil2817b002009-10-06 11:31:08 -07001492 .listxattr = ceph_listxattr,
Guangliang Zhao7221fe42013-11-11 15:18:03 +08001493 .get_acl = ceph_get_acl,
Sage Weil72466d02014-01-29 06:22:25 -08001494 .set_acl = ceph_set_acl,
Sage Weil2817b002009-10-06 11:31:08 -07001495 .mknod = ceph_mknod,
1496 .symlink = ceph_symlink,
1497 .mkdir = ceph_mkdir,
1498 .link = ceph_link,
1499 .unlink = ceph_unlink,
1500 .rmdir = ceph_unlink,
1501 .rename = ceph_rename,
1502 .create = ceph_create,
Miklos Szeredi2d83bde2012-06-05 15:10:25 +02001503 .atomic_open = ceph_atomic_open,
Sage Weil2817b002009-10-06 11:31:08 -07001504};
1505
Yan, Zheng38c48b52015-01-14 13:46:04 +08001506const struct inode_operations ceph_snapdir_iops = {
1507 .lookup = ceph_lookup,
1508 .permission = ceph_permission,
1509 .getattr = ceph_getattr,
1510 .mkdir = ceph_mkdir,
1511 .rmdir = ceph_unlink,
Yan, Zheng0ea611a2015-04-07 15:36:32 +08001512 .rename = ceph_rename,
Yan, Zheng38c48b52015-01-14 13:46:04 +08001513};
1514
Sage Weil52dfb8a2010-08-03 10:25:30 -07001515const struct dentry_operations ceph_dentry_ops = {
Sage Weil2817b002009-10-06 11:31:08 -07001516 .d_revalidate = ceph_d_revalidate,
Sage Weil147851d2011-03-15 14:57:41 -07001517 .d_release = ceph_d_release,
Sage Weilb58dc412011-03-15 15:53:40 -07001518 .d_prune = ceph_d_prune,
Al Viroad5cb122016-10-28 22:05:13 -04001519 .d_init = ceph_d_init,
Sage Weil2817b002009-10-06 11:31:08 -07001520};