1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <linux/ceph/ceph_debug.h>
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/spinlock.h>
5*4882a593Smuzhiyun #include <linux/namei.h>
6*4882a593Smuzhiyun #include <linux/slab.h>
7*4882a593Smuzhiyun #include <linux/sched.h>
8*4882a593Smuzhiyun #include <linux/xattr.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include "super.h"
11*4882a593Smuzhiyun #include "mds_client.h"
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /*
14*4882a593Smuzhiyun * Directory operations: readdir, lookup, create, link, unlink,
15*4882a593Smuzhiyun * rename, etc.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun * Ceph MDS operations are specified in terms of a base ino and
20*4882a593Smuzhiyun * relative path. Thus, the client can specify an operation on a
21*4882a593Smuzhiyun * specific inode (e.g., a getattr due to fstat(2)), or as a path
22*4882a593Smuzhiyun * relative to, say, the root directory.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Normally, we limit ourselves to strict inode ops (no path component)
25*4882a593Smuzhiyun * or dentry operations (a single path component relative to an ino). The
26*4882a593Smuzhiyun * exception to this is open_root_dentry(), which will open the mount
27*4882a593Smuzhiyun * point by name.
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun const struct dentry_operations ceph_dentry_ops;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun static bool __dentry_lease_is_valid(struct ceph_dentry_info *di);
33*4882a593Smuzhiyun static int __dir_lease_try_check(const struct dentry *dentry);
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * Initialize ceph dentry state.
37*4882a593Smuzhiyun */
ceph_d_init(struct dentry * dentry)38*4882a593Smuzhiyun static int ceph_d_init(struct dentry *dentry)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun struct ceph_dentry_info *di;
41*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dentry->d_sb);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL);
44*4882a593Smuzhiyun if (!di)
45*4882a593Smuzhiyun return -ENOMEM; /* oh well */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun di->dentry = dentry;
48*4882a593Smuzhiyun di->lease_session = NULL;
49*4882a593Smuzhiyun di->time = jiffies;
50*4882a593Smuzhiyun dentry->d_fsdata = di;
51*4882a593Smuzhiyun INIT_LIST_HEAD(&di->lease_list);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun atomic64_inc(&mdsc->metric.total_dentries);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun return 0;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * for f_pos for readdir:
60*4882a593Smuzhiyun * - hash order:
61*4882a593Smuzhiyun * (0xff << 52) | ((24 bits hash) << 28) |
62*4882a593Smuzhiyun * (the nth entry has hash collision);
63*4882a593Smuzhiyun * - frag+name order;
64*4882a593Smuzhiyun * ((frag value) << 28) | (the nth entry in frag);
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun #define OFFSET_BITS 28
67*4882a593Smuzhiyun #define OFFSET_MASK ((1 << OFFSET_BITS) - 1)
68*4882a593Smuzhiyun #define HASH_ORDER (0xffull << (OFFSET_BITS + 24))
ceph_make_fpos(unsigned high,unsigned off,bool hash_order)69*4882a593Smuzhiyun loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun loff_t fpos = ((loff_t)high << 28) | (loff_t)off;
72*4882a593Smuzhiyun if (hash_order)
73*4882a593Smuzhiyun fpos |= HASH_ORDER;
74*4882a593Smuzhiyun return fpos;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
is_hash_order(loff_t p)77*4882a593Smuzhiyun static bool is_hash_order(loff_t p)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun return (p & HASH_ORDER) == HASH_ORDER;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
fpos_frag(loff_t p)82*4882a593Smuzhiyun static unsigned fpos_frag(loff_t p)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun return p >> OFFSET_BITS;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
fpos_hash(loff_t p)87*4882a593Smuzhiyun static unsigned fpos_hash(loff_t p)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun return ceph_frag_value(fpos_frag(p));
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
fpos_off(loff_t p)92*4882a593Smuzhiyun static unsigned fpos_off(loff_t p)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun return p & OFFSET_MASK;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
fpos_cmp(loff_t l,loff_t r)97*4882a593Smuzhiyun static int fpos_cmp(loff_t l, loff_t r)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r));
100*4882a593Smuzhiyun if (v)
101*4882a593Smuzhiyun return v;
102*4882a593Smuzhiyun return (int)(fpos_off(l) - fpos_off(r));
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * make note of the last dentry we read, so we can
107*4882a593Smuzhiyun * continue at the same lexicographical point,
108*4882a593Smuzhiyun * regardless of what dir changes take place on the
109*4882a593Smuzhiyun * server.
110*4882a593Smuzhiyun */
note_last_dentry(struct ceph_dir_file_info * dfi,const char * name,int len,unsigned next_offset)111*4882a593Smuzhiyun static int note_last_dentry(struct ceph_dir_file_info *dfi, const char *name,
112*4882a593Smuzhiyun int len, unsigned next_offset)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun char *buf = kmalloc(len+1, GFP_KERNEL);
115*4882a593Smuzhiyun if (!buf)
116*4882a593Smuzhiyun return -ENOMEM;
117*4882a593Smuzhiyun kfree(dfi->last_name);
118*4882a593Smuzhiyun dfi->last_name = buf;
119*4882a593Smuzhiyun memcpy(dfi->last_name, name, len);
120*4882a593Smuzhiyun dfi->last_name[len] = 0;
121*4882a593Smuzhiyun dfi->next_offset = next_offset;
122*4882a593Smuzhiyun dout("note_last_dentry '%s'\n", dfi->last_name);
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun static struct dentry *
__dcache_find_get_entry(struct dentry * parent,u64 idx,struct ceph_readdir_cache_control * cache_ctl)128*4882a593Smuzhiyun __dcache_find_get_entry(struct dentry *parent, u64 idx,
129*4882a593Smuzhiyun struct ceph_readdir_cache_control *cache_ctl)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct inode *dir = d_inode(parent);
132*4882a593Smuzhiyun struct dentry *dentry;
133*4882a593Smuzhiyun unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1;
134*4882a593Smuzhiyun loff_t ptr_pos = idx * sizeof(struct dentry *);
135*4882a593Smuzhiyun pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun if (ptr_pos >= i_size_read(dir))
138*4882a593Smuzhiyun return NULL;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) {
141*4882a593Smuzhiyun ceph_readdir_cache_release(cache_ctl);
142*4882a593Smuzhiyun cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff);
143*4882a593Smuzhiyun if (!cache_ctl->page) {
144*4882a593Smuzhiyun dout(" page %lu not found\n", ptr_pgoff);
145*4882a593Smuzhiyun return ERR_PTR(-EAGAIN);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun /* reading/filling the cache are serialized by
148*4882a593Smuzhiyun i_mutex, no need to use page lock */
149*4882a593Smuzhiyun unlock_page(cache_ctl->page);
150*4882a593Smuzhiyun cache_ctl->dentries = kmap(cache_ctl->page);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun cache_ctl->index = idx & idx_mask;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun rcu_read_lock();
156*4882a593Smuzhiyun spin_lock(&parent->d_lock);
157*4882a593Smuzhiyun /* check i_size again here, because empty directory can be
158*4882a593Smuzhiyun * marked as complete while not holding the i_mutex. */
159*4882a593Smuzhiyun if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir))
160*4882a593Smuzhiyun dentry = cache_ctl->dentries[cache_ctl->index];
161*4882a593Smuzhiyun else
162*4882a593Smuzhiyun dentry = NULL;
163*4882a593Smuzhiyun spin_unlock(&parent->d_lock);
164*4882a593Smuzhiyun if (dentry && !lockref_get_not_dead(&dentry->d_lockref))
165*4882a593Smuzhiyun dentry = NULL;
166*4882a593Smuzhiyun rcu_read_unlock();
167*4882a593Smuzhiyun return dentry ? : ERR_PTR(-EAGAIN);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * When possible, we try to satisfy a readdir by peeking at the
172*4882a593Smuzhiyun * dcache. We make this work by carefully ordering dentries on
173*4882a593Smuzhiyun * d_child when we initially get results back from the MDS, and
174*4882a593Smuzhiyun * falling back to a "normal" sync readdir if any dentries in the dir
175*4882a593Smuzhiyun * are dropped.
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * Complete dir indicates that we have all dentries in the dir. It is
178*4882a593Smuzhiyun * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by
179*4882a593Smuzhiyun * the MDS if/when the directory is modified).
180*4882a593Smuzhiyun */
__dcache_readdir(struct file * file,struct dir_context * ctx,int shared_gen)181*4882a593Smuzhiyun static int __dcache_readdir(struct file *file, struct dir_context *ctx,
182*4882a593Smuzhiyun int shared_gen)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct ceph_dir_file_info *dfi = file->private_data;
185*4882a593Smuzhiyun struct dentry *parent = file->f_path.dentry;
186*4882a593Smuzhiyun struct inode *dir = d_inode(parent);
187*4882a593Smuzhiyun struct dentry *dentry, *last = NULL;
188*4882a593Smuzhiyun struct ceph_dentry_info *di;
189*4882a593Smuzhiyun struct ceph_readdir_cache_control cache_ctl = {};
190*4882a593Smuzhiyun u64 idx = 0;
191*4882a593Smuzhiyun int err = 0;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun dout("__dcache_readdir %p v%u at %llx\n", dir, (unsigned)shared_gen, ctx->pos);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /* search start position */
196*4882a593Smuzhiyun if (ctx->pos > 2) {
197*4882a593Smuzhiyun u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *));
198*4882a593Smuzhiyun while (count > 0) {
199*4882a593Smuzhiyun u64 step = count >> 1;
200*4882a593Smuzhiyun dentry = __dcache_find_get_entry(parent, idx + step,
201*4882a593Smuzhiyun &cache_ctl);
202*4882a593Smuzhiyun if (!dentry) {
203*4882a593Smuzhiyun /* use linar search */
204*4882a593Smuzhiyun idx = 0;
205*4882a593Smuzhiyun break;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun if (IS_ERR(dentry)) {
208*4882a593Smuzhiyun err = PTR_ERR(dentry);
209*4882a593Smuzhiyun goto out;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun di = ceph_dentry(dentry);
212*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
213*4882a593Smuzhiyun if (fpos_cmp(di->offset, ctx->pos) < 0) {
214*4882a593Smuzhiyun idx += step + 1;
215*4882a593Smuzhiyun count -= step + 1;
216*4882a593Smuzhiyun } else {
217*4882a593Smuzhiyun count = step;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
220*4882a593Smuzhiyun dput(dentry);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun dout("__dcache_readdir %p cache idx %llu\n", dir, idx);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun for (;;) {
228*4882a593Smuzhiyun bool emit_dentry = false;
229*4882a593Smuzhiyun dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl);
230*4882a593Smuzhiyun if (!dentry) {
231*4882a593Smuzhiyun dfi->file_info.flags |= CEPH_F_ATEND;
232*4882a593Smuzhiyun err = 0;
233*4882a593Smuzhiyun break;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun if (IS_ERR(dentry)) {
236*4882a593Smuzhiyun err = PTR_ERR(dentry);
237*4882a593Smuzhiyun goto out;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
241*4882a593Smuzhiyun di = ceph_dentry(dentry);
242*4882a593Smuzhiyun if (d_unhashed(dentry) ||
243*4882a593Smuzhiyun d_really_is_negative(dentry) ||
244*4882a593Smuzhiyun di->lease_shared_gen != shared_gen) {
245*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
246*4882a593Smuzhiyun dput(dentry);
247*4882a593Smuzhiyun err = -EAGAIN;
248*4882a593Smuzhiyun goto out;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun if (fpos_cmp(ctx->pos, di->offset) <= 0) {
251*4882a593Smuzhiyun __ceph_dentry_dir_lease_touch(di);
252*4882a593Smuzhiyun emit_dentry = true;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun if (emit_dentry) {
257*4882a593Smuzhiyun dout(" %llx dentry %p %pd %p\n", di->offset,
258*4882a593Smuzhiyun dentry, dentry, d_inode(dentry));
259*4882a593Smuzhiyun ctx->pos = di->offset;
260*4882a593Smuzhiyun if (!dir_emit(ctx, dentry->d_name.name,
261*4882a593Smuzhiyun dentry->d_name.len, ceph_present_inode(d_inode(dentry)),
262*4882a593Smuzhiyun d_inode(dentry)->i_mode >> 12)) {
263*4882a593Smuzhiyun dput(dentry);
264*4882a593Smuzhiyun err = 0;
265*4882a593Smuzhiyun break;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun ctx->pos++;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (last)
270*4882a593Smuzhiyun dput(last);
271*4882a593Smuzhiyun last = dentry;
272*4882a593Smuzhiyun } else {
273*4882a593Smuzhiyun dput(dentry);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun out:
277*4882a593Smuzhiyun ceph_readdir_cache_release(&cache_ctl);
278*4882a593Smuzhiyun if (last) {
279*4882a593Smuzhiyun int ret;
280*4882a593Smuzhiyun di = ceph_dentry(last);
281*4882a593Smuzhiyun ret = note_last_dentry(dfi, last->d_name.name, last->d_name.len,
282*4882a593Smuzhiyun fpos_off(di->offset) + 1);
283*4882a593Smuzhiyun if (ret < 0)
284*4882a593Smuzhiyun err = ret;
285*4882a593Smuzhiyun dput(last);
286*4882a593Smuzhiyun /* last_name no longer match cache index */
287*4882a593Smuzhiyun if (dfi->readdir_cache_idx >= 0) {
288*4882a593Smuzhiyun dfi->readdir_cache_idx = -1;
289*4882a593Smuzhiyun dfi->dir_release_count = 0;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun return err;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
need_send_readdir(struct ceph_dir_file_info * dfi,loff_t pos)295*4882a593Smuzhiyun static bool need_send_readdir(struct ceph_dir_file_info *dfi, loff_t pos)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun if (!dfi->last_readdir)
298*4882a593Smuzhiyun return true;
299*4882a593Smuzhiyun if (is_hash_order(pos))
300*4882a593Smuzhiyun return !ceph_frag_contains_value(dfi->frag, fpos_hash(pos));
301*4882a593Smuzhiyun else
302*4882a593Smuzhiyun return dfi->frag != fpos_frag(pos);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
ceph_readdir(struct file * file,struct dir_context * ctx)305*4882a593Smuzhiyun static int ceph_readdir(struct file *file, struct dir_context *ctx)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun struct ceph_dir_file_info *dfi = file->private_data;
308*4882a593Smuzhiyun struct inode *inode = file_inode(file);
309*4882a593Smuzhiyun struct ceph_inode_info *ci = ceph_inode(inode);
310*4882a593Smuzhiyun struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
311*4882a593Smuzhiyun struct ceph_mds_client *mdsc = fsc->mdsc;
312*4882a593Smuzhiyun int i;
313*4882a593Smuzhiyun int err;
314*4882a593Smuzhiyun unsigned frag = -1;
315*4882a593Smuzhiyun struct ceph_mds_reply_info_parsed *rinfo;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos);
318*4882a593Smuzhiyun if (dfi->file_info.flags & CEPH_F_ATEND)
319*4882a593Smuzhiyun return 0;
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /* always start with . and .. */
322*4882a593Smuzhiyun if (ctx->pos == 0) {
323*4882a593Smuzhiyun dout("readdir off 0 -> '.'\n");
324*4882a593Smuzhiyun if (!dir_emit(ctx, ".", 1, ceph_present_inode(inode),
325*4882a593Smuzhiyun inode->i_mode >> 12))
326*4882a593Smuzhiyun return 0;
327*4882a593Smuzhiyun ctx->pos = 1;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun if (ctx->pos == 1) {
330*4882a593Smuzhiyun u64 ino;
331*4882a593Smuzhiyun struct dentry *dentry = file->f_path.dentry;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
334*4882a593Smuzhiyun ino = ceph_present_inode(dentry->d_parent->d_inode);
335*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun dout("readdir off 1 -> '..'\n");
338*4882a593Smuzhiyun if (!dir_emit(ctx, "..", 2, ino, inode->i_mode >> 12))
339*4882a593Smuzhiyun return 0;
340*4882a593Smuzhiyun ctx->pos = 2;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
344*4882a593Smuzhiyun /* request Fx cap. if have Fx, we don't need to release Fs cap
345*4882a593Smuzhiyun * for later create/unlink. */
346*4882a593Smuzhiyun __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_WR);
347*4882a593Smuzhiyun /* can we use the dcache? */
348*4882a593Smuzhiyun if (ceph_test_mount_opt(fsc, DCACHE) &&
349*4882a593Smuzhiyun !ceph_test_mount_opt(fsc, NOASYNCREADDIR) &&
350*4882a593Smuzhiyun ceph_snap(inode) != CEPH_SNAPDIR &&
351*4882a593Smuzhiyun __ceph_dir_is_complete_ordered(ci) &&
352*4882a593Smuzhiyun __ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) {
353*4882a593Smuzhiyun int shared_gen = atomic_read(&ci->i_shared_gen);
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
356*4882a593Smuzhiyun err = __dcache_readdir(file, ctx, shared_gen);
357*4882a593Smuzhiyun if (err != -EAGAIN)
358*4882a593Smuzhiyun return err;
359*4882a593Smuzhiyun } else {
360*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /* proceed with a normal readdir */
364*4882a593Smuzhiyun more:
365*4882a593Smuzhiyun /* do we have the correct frag content buffered? */
366*4882a593Smuzhiyun if (need_send_readdir(dfi, ctx->pos)) {
367*4882a593Smuzhiyun struct ceph_mds_request *req;
368*4882a593Smuzhiyun int op = ceph_snap(inode) == CEPH_SNAPDIR ?
369*4882a593Smuzhiyun CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /* discard old result, if any */
372*4882a593Smuzhiyun if (dfi->last_readdir) {
373*4882a593Smuzhiyun ceph_mdsc_put_request(dfi->last_readdir);
374*4882a593Smuzhiyun dfi->last_readdir = NULL;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun if (is_hash_order(ctx->pos)) {
378*4882a593Smuzhiyun /* fragtree isn't always accurate. choose frag
379*4882a593Smuzhiyun * based on previous reply when possible. */
380*4882a593Smuzhiyun if (frag == (unsigned)-1)
381*4882a593Smuzhiyun frag = ceph_choose_frag(ci, fpos_hash(ctx->pos),
382*4882a593Smuzhiyun NULL, NULL);
383*4882a593Smuzhiyun } else {
384*4882a593Smuzhiyun frag = fpos_frag(ctx->pos);
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun dout("readdir fetching %llx.%llx frag %x offset '%s'\n",
388*4882a593Smuzhiyun ceph_vinop(inode), frag, dfi->last_name);
389*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
390*4882a593Smuzhiyun if (IS_ERR(req))
391*4882a593Smuzhiyun return PTR_ERR(req);
392*4882a593Smuzhiyun err = ceph_alloc_readdir_reply_buffer(req, inode);
393*4882a593Smuzhiyun if (err) {
394*4882a593Smuzhiyun ceph_mdsc_put_request(req);
395*4882a593Smuzhiyun return err;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun /* hints to request -> mds selection code */
398*4882a593Smuzhiyun req->r_direct_mode = USE_AUTH_MDS;
399*4882a593Smuzhiyun if (op == CEPH_MDS_OP_READDIR) {
400*4882a593Smuzhiyun req->r_direct_hash = ceph_frag_value(frag);
401*4882a593Smuzhiyun __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
402*4882a593Smuzhiyun req->r_inode_drop = CEPH_CAP_FILE_EXCL;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun if (dfi->last_name) {
405*4882a593Smuzhiyun req->r_path2 = kstrdup(dfi->last_name, GFP_KERNEL);
406*4882a593Smuzhiyun if (!req->r_path2) {
407*4882a593Smuzhiyun ceph_mdsc_put_request(req);
408*4882a593Smuzhiyun return -ENOMEM;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun } else if (is_hash_order(ctx->pos)) {
411*4882a593Smuzhiyun req->r_args.readdir.offset_hash =
412*4882a593Smuzhiyun cpu_to_le32(fpos_hash(ctx->pos));
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun req->r_dir_release_cnt = dfi->dir_release_count;
416*4882a593Smuzhiyun req->r_dir_ordered_cnt = dfi->dir_ordered_count;
417*4882a593Smuzhiyun req->r_readdir_cache_idx = dfi->readdir_cache_idx;
418*4882a593Smuzhiyun req->r_readdir_offset = dfi->next_offset;
419*4882a593Smuzhiyun req->r_args.readdir.frag = cpu_to_le32(frag);
420*4882a593Smuzhiyun req->r_args.readdir.flags =
421*4882a593Smuzhiyun cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS);
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun req->r_inode = inode;
424*4882a593Smuzhiyun ihold(inode);
425*4882a593Smuzhiyun req->r_dentry = dget(file->f_path.dentry);
426*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, NULL, req);
427*4882a593Smuzhiyun if (err < 0) {
428*4882a593Smuzhiyun ceph_mdsc_put_request(req);
429*4882a593Smuzhiyun return err;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun dout("readdir got and parsed readdir result=%d on "
432*4882a593Smuzhiyun "frag %x, end=%d, complete=%d, hash_order=%d\n",
433*4882a593Smuzhiyun err, frag,
434*4882a593Smuzhiyun (int)req->r_reply_info.dir_end,
435*4882a593Smuzhiyun (int)req->r_reply_info.dir_complete,
436*4882a593Smuzhiyun (int)req->r_reply_info.hash_order);
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun rinfo = &req->r_reply_info;
439*4882a593Smuzhiyun if (le32_to_cpu(rinfo->dir_dir->frag) != frag) {
440*4882a593Smuzhiyun frag = le32_to_cpu(rinfo->dir_dir->frag);
441*4882a593Smuzhiyun if (!rinfo->hash_order) {
442*4882a593Smuzhiyun dfi->next_offset = req->r_readdir_offset;
443*4882a593Smuzhiyun /* adjust ctx->pos to beginning of frag */
444*4882a593Smuzhiyun ctx->pos = ceph_make_fpos(frag,
445*4882a593Smuzhiyun dfi->next_offset,
446*4882a593Smuzhiyun false);
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun dfi->frag = frag;
451*4882a593Smuzhiyun dfi->last_readdir = req;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) {
454*4882a593Smuzhiyun dfi->readdir_cache_idx = req->r_readdir_cache_idx;
455*4882a593Smuzhiyun if (dfi->readdir_cache_idx < 0) {
456*4882a593Smuzhiyun /* preclude from marking dir ordered */
457*4882a593Smuzhiyun dfi->dir_ordered_count = 0;
458*4882a593Smuzhiyun } else if (ceph_frag_is_leftmost(frag) &&
459*4882a593Smuzhiyun dfi->next_offset == 2) {
460*4882a593Smuzhiyun /* note dir version at start of readdir so
461*4882a593Smuzhiyun * we can tell if any dentries get dropped */
462*4882a593Smuzhiyun dfi->dir_release_count = req->r_dir_release_cnt;
463*4882a593Smuzhiyun dfi->dir_ordered_count = req->r_dir_ordered_cnt;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun } else {
466*4882a593Smuzhiyun dout("readdir !did_prepopulate\n");
467*4882a593Smuzhiyun /* disable readdir cache */
468*4882a593Smuzhiyun dfi->readdir_cache_idx = -1;
469*4882a593Smuzhiyun /* preclude from marking dir complete */
470*4882a593Smuzhiyun dfi->dir_release_count = 0;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /* note next offset and last dentry name */
474*4882a593Smuzhiyun if (rinfo->dir_nr > 0) {
475*4882a593Smuzhiyun struct ceph_mds_reply_dir_entry *rde =
476*4882a593Smuzhiyun rinfo->dir_entries + (rinfo->dir_nr-1);
477*4882a593Smuzhiyun unsigned next_offset = req->r_reply_info.dir_end ?
478*4882a593Smuzhiyun 2 : (fpos_off(rde->offset) + 1);
479*4882a593Smuzhiyun err = note_last_dentry(dfi, rde->name, rde->name_len,
480*4882a593Smuzhiyun next_offset);
481*4882a593Smuzhiyun if (err) {
482*4882a593Smuzhiyun ceph_mdsc_put_request(dfi->last_readdir);
483*4882a593Smuzhiyun dfi->last_readdir = NULL;
484*4882a593Smuzhiyun return err;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun } else if (req->r_reply_info.dir_end) {
487*4882a593Smuzhiyun dfi->next_offset = 2;
488*4882a593Smuzhiyun /* keep last name */
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun rinfo = &dfi->last_readdir->r_reply_info;
493*4882a593Smuzhiyun dout("readdir frag %x num %d pos %llx chunk first %llx\n",
494*4882a593Smuzhiyun dfi->frag, rinfo->dir_nr, ctx->pos,
495*4882a593Smuzhiyun rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL);
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun i = 0;
498*4882a593Smuzhiyun /* search start position */
499*4882a593Smuzhiyun if (rinfo->dir_nr > 0) {
500*4882a593Smuzhiyun int step, nr = rinfo->dir_nr;
501*4882a593Smuzhiyun while (nr > 0) {
502*4882a593Smuzhiyun step = nr >> 1;
503*4882a593Smuzhiyun if (rinfo->dir_entries[i + step].offset < ctx->pos) {
504*4882a593Smuzhiyun i += step + 1;
505*4882a593Smuzhiyun nr -= step + 1;
506*4882a593Smuzhiyun } else {
507*4882a593Smuzhiyun nr = step;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun for (; i < rinfo->dir_nr; i++) {
512*4882a593Smuzhiyun struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun BUG_ON(rde->offset < ctx->pos);
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun ctx->pos = rde->offset;
517*4882a593Smuzhiyun dout("readdir (%d/%d) -> %llx '%.*s' %p\n",
518*4882a593Smuzhiyun i, rinfo->dir_nr, ctx->pos,
519*4882a593Smuzhiyun rde->name_len, rde->name, &rde->inode.in);
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun BUG_ON(!rde->inode.in);
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun if (!dir_emit(ctx, rde->name, rde->name_len,
524*4882a593Smuzhiyun ceph_present_ino(inode->i_sb, le64_to_cpu(rde->inode.in->ino)),
525*4882a593Smuzhiyun le32_to_cpu(rde->inode.in->mode) >> 12)) {
526*4882a593Smuzhiyun /*
527*4882a593Smuzhiyun * NOTE: Here no need to put the 'dfi->last_readdir',
528*4882a593Smuzhiyun * because when dir_emit stops us it's most likely
529*4882a593Smuzhiyun * doesn't have enough memory, etc. So for next readdir
530*4882a593Smuzhiyun * it will continue.
531*4882a593Smuzhiyun */
532*4882a593Smuzhiyun dout("filldir stopping us...\n");
533*4882a593Smuzhiyun return 0;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun ctx->pos++;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun ceph_mdsc_put_request(dfi->last_readdir);
539*4882a593Smuzhiyun dfi->last_readdir = NULL;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun if (dfi->next_offset > 2) {
542*4882a593Smuzhiyun frag = dfi->frag;
543*4882a593Smuzhiyun goto more;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun /* more frags? */
547*4882a593Smuzhiyun if (!ceph_frag_is_rightmost(dfi->frag)) {
548*4882a593Smuzhiyun frag = ceph_frag_next(dfi->frag);
549*4882a593Smuzhiyun if (is_hash_order(ctx->pos)) {
550*4882a593Smuzhiyun loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag),
551*4882a593Smuzhiyun dfi->next_offset, true);
552*4882a593Smuzhiyun if (new_pos > ctx->pos)
553*4882a593Smuzhiyun ctx->pos = new_pos;
554*4882a593Smuzhiyun /* keep last_name */
555*4882a593Smuzhiyun } else {
556*4882a593Smuzhiyun ctx->pos = ceph_make_fpos(frag, dfi->next_offset,
557*4882a593Smuzhiyun false);
558*4882a593Smuzhiyun kfree(dfi->last_name);
559*4882a593Smuzhiyun dfi->last_name = NULL;
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun dout("readdir next frag is %x\n", frag);
562*4882a593Smuzhiyun goto more;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun dfi->file_info.flags |= CEPH_F_ATEND;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun /*
567*4882a593Smuzhiyun * if dir_release_count still matches the dir, no dentries
568*4882a593Smuzhiyun * were released during the whole readdir, and we should have
569*4882a593Smuzhiyun * the complete dir contents in our cache.
570*4882a593Smuzhiyun */
571*4882a593Smuzhiyun if (atomic64_read(&ci->i_release_count) ==
572*4882a593Smuzhiyun dfi->dir_release_count) {
573*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
574*4882a593Smuzhiyun if (dfi->dir_ordered_count ==
575*4882a593Smuzhiyun atomic64_read(&ci->i_ordered_count)) {
576*4882a593Smuzhiyun dout(" marking %p complete and ordered\n", inode);
577*4882a593Smuzhiyun /* use i_size to track number of entries in
578*4882a593Smuzhiyun * readdir cache */
579*4882a593Smuzhiyun BUG_ON(dfi->readdir_cache_idx < 0);
580*4882a593Smuzhiyun i_size_write(inode, dfi->readdir_cache_idx *
581*4882a593Smuzhiyun sizeof(struct dentry*));
582*4882a593Smuzhiyun } else {
583*4882a593Smuzhiyun dout(" marking %p complete\n", inode);
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun __ceph_dir_set_complete(ci, dfi->dir_release_count,
586*4882a593Smuzhiyun dfi->dir_ordered_count);
587*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun dout("readdir %p file %p done.\n", inode, file);
591*4882a593Smuzhiyun return 0;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
reset_readdir(struct ceph_dir_file_info * dfi)594*4882a593Smuzhiyun static void reset_readdir(struct ceph_dir_file_info *dfi)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun if (dfi->last_readdir) {
597*4882a593Smuzhiyun ceph_mdsc_put_request(dfi->last_readdir);
598*4882a593Smuzhiyun dfi->last_readdir = NULL;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun kfree(dfi->last_name);
601*4882a593Smuzhiyun dfi->last_name = NULL;
602*4882a593Smuzhiyun dfi->dir_release_count = 0;
603*4882a593Smuzhiyun dfi->readdir_cache_idx = -1;
604*4882a593Smuzhiyun dfi->next_offset = 2; /* compensate for . and .. */
605*4882a593Smuzhiyun dfi->file_info.flags &= ~CEPH_F_ATEND;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun /*
609*4882a593Smuzhiyun * discard buffered readdir content on seekdir(0), or seek to new frag,
610*4882a593Smuzhiyun * or seek prior to current chunk
611*4882a593Smuzhiyun */
need_reset_readdir(struct ceph_dir_file_info * dfi,loff_t new_pos)612*4882a593Smuzhiyun static bool need_reset_readdir(struct ceph_dir_file_info *dfi, loff_t new_pos)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun struct ceph_mds_reply_info_parsed *rinfo;
615*4882a593Smuzhiyun loff_t chunk_offset;
616*4882a593Smuzhiyun if (new_pos == 0)
617*4882a593Smuzhiyun return true;
618*4882a593Smuzhiyun if (is_hash_order(new_pos)) {
619*4882a593Smuzhiyun /* no need to reset last_name for a forward seek when
620*4882a593Smuzhiyun * dentries are sotred in hash order */
621*4882a593Smuzhiyun } else if (dfi->frag != fpos_frag(new_pos)) {
622*4882a593Smuzhiyun return true;
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun rinfo = dfi->last_readdir ? &dfi->last_readdir->r_reply_info : NULL;
625*4882a593Smuzhiyun if (!rinfo || !rinfo->dir_nr)
626*4882a593Smuzhiyun return true;
627*4882a593Smuzhiyun chunk_offset = rinfo->dir_entries[0].offset;
628*4882a593Smuzhiyun return new_pos < chunk_offset ||
629*4882a593Smuzhiyun is_hash_order(new_pos) != is_hash_order(chunk_offset);
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
ceph_dir_llseek(struct file * file,loff_t offset,int whence)632*4882a593Smuzhiyun static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun struct ceph_dir_file_info *dfi = file->private_data;
635*4882a593Smuzhiyun struct inode *inode = file->f_mapping->host;
636*4882a593Smuzhiyun loff_t retval;
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun inode_lock(inode);
639*4882a593Smuzhiyun retval = -EINVAL;
640*4882a593Smuzhiyun switch (whence) {
641*4882a593Smuzhiyun case SEEK_CUR:
642*4882a593Smuzhiyun offset += file->f_pos;
643*4882a593Smuzhiyun case SEEK_SET:
644*4882a593Smuzhiyun break;
645*4882a593Smuzhiyun case SEEK_END:
646*4882a593Smuzhiyun retval = -EOPNOTSUPP;
647*4882a593Smuzhiyun default:
648*4882a593Smuzhiyun goto out;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
651*4882a593Smuzhiyun if (offset >= 0) {
652*4882a593Smuzhiyun if (need_reset_readdir(dfi, offset)) {
653*4882a593Smuzhiyun dout("dir_llseek dropping %p content\n", file);
654*4882a593Smuzhiyun reset_readdir(dfi);
655*4882a593Smuzhiyun } else if (is_hash_order(offset) && offset > file->f_pos) {
656*4882a593Smuzhiyun /* for hash offset, we don't know if a forward seek
657*4882a593Smuzhiyun * is within same frag */
658*4882a593Smuzhiyun dfi->dir_release_count = 0;
659*4882a593Smuzhiyun dfi->readdir_cache_idx = -1;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun if (offset != file->f_pos) {
663*4882a593Smuzhiyun file->f_pos = offset;
664*4882a593Smuzhiyun file->f_version = 0;
665*4882a593Smuzhiyun dfi->file_info.flags &= ~CEPH_F_ATEND;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun retval = offset;
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun out:
670*4882a593Smuzhiyun inode_unlock(inode);
671*4882a593Smuzhiyun return retval;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /*
675*4882a593Smuzhiyun * Handle lookups for the hidden .snap directory.
676*4882a593Smuzhiyun */
ceph_handle_snapdir(struct ceph_mds_request * req,struct dentry * dentry,int err)677*4882a593Smuzhiyun int ceph_handle_snapdir(struct ceph_mds_request *req,
678*4882a593Smuzhiyun struct dentry *dentry, int err)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
681*4882a593Smuzhiyun struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun /* .snap dir? */
684*4882a593Smuzhiyun if (err == -ENOENT &&
685*4882a593Smuzhiyun ceph_snap(parent) == CEPH_NOSNAP &&
686*4882a593Smuzhiyun strcmp(dentry->d_name.name,
687*4882a593Smuzhiyun fsc->mount_options->snapdir_name) == 0) {
688*4882a593Smuzhiyun struct inode *inode = ceph_get_snapdir(parent);
689*4882a593Smuzhiyun dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n",
690*4882a593Smuzhiyun dentry, dentry, inode);
691*4882a593Smuzhiyun BUG_ON(!d_unhashed(dentry));
692*4882a593Smuzhiyun d_add(dentry, inode);
693*4882a593Smuzhiyun err = 0;
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun return err;
696*4882a593Smuzhiyun }
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /*
699*4882a593Smuzhiyun * Figure out final result of a lookup/open request.
700*4882a593Smuzhiyun *
701*4882a593Smuzhiyun * Mainly, make sure we return the final req->r_dentry (if it already
702*4882a593Smuzhiyun * existed) in place of the original VFS-provided dentry when they
703*4882a593Smuzhiyun * differ.
704*4882a593Smuzhiyun *
705*4882a593Smuzhiyun * Gracefully handle the case where the MDS replies with -ENOENT and
706*4882a593Smuzhiyun * no trace (which it may do, at its discretion, e.g., if it doesn't
707*4882a593Smuzhiyun * care to issue a lease on the negative dentry).
708*4882a593Smuzhiyun */
ceph_finish_lookup(struct ceph_mds_request * req,struct dentry * dentry,int err)709*4882a593Smuzhiyun struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
710*4882a593Smuzhiyun struct dentry *dentry, int err)
711*4882a593Smuzhiyun {
712*4882a593Smuzhiyun if (err == -ENOENT) {
713*4882a593Smuzhiyun /* no trace? */
714*4882a593Smuzhiyun err = 0;
715*4882a593Smuzhiyun if (!req->r_reply_info.head->is_dentry) {
716*4882a593Smuzhiyun dout("ENOENT and no trace, dentry %p inode %p\n",
717*4882a593Smuzhiyun dentry, d_inode(dentry));
718*4882a593Smuzhiyun if (d_really_is_positive(dentry)) {
719*4882a593Smuzhiyun d_drop(dentry);
720*4882a593Smuzhiyun err = -ENOENT;
721*4882a593Smuzhiyun } else {
722*4882a593Smuzhiyun d_add(dentry, NULL);
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun if (err)
727*4882a593Smuzhiyun dentry = ERR_PTR(err);
728*4882a593Smuzhiyun else if (dentry != req->r_dentry)
729*4882a593Smuzhiyun dentry = dget(req->r_dentry); /* we got spliced */
730*4882a593Smuzhiyun else
731*4882a593Smuzhiyun dentry = NULL;
732*4882a593Smuzhiyun return dentry;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun
is_root_ceph_dentry(struct inode * inode,struct dentry * dentry)735*4882a593Smuzhiyun static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun return ceph_ino(inode) == CEPH_INO_ROOT &&
738*4882a593Smuzhiyun strncmp(dentry->d_name.name, ".ceph", 5) == 0;
739*4882a593Smuzhiyun }
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun /*
742*4882a593Smuzhiyun * Look up a single dir entry. If there is a lookup intent, inform
743*4882a593Smuzhiyun * the MDS so that it gets our 'caps wanted' value in a single op.
744*4882a593Smuzhiyun */
ceph_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)745*4882a593Smuzhiyun static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
746*4882a593Smuzhiyun unsigned int flags)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
749*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
750*4882a593Smuzhiyun struct ceph_mds_request *req;
751*4882a593Smuzhiyun int op;
752*4882a593Smuzhiyun int mask;
753*4882a593Smuzhiyun int err;
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun dout("lookup %p dentry %p '%pd'\n",
756*4882a593Smuzhiyun dir, dentry, dentry);
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun if (dentry->d_name.len > NAME_MAX)
759*4882a593Smuzhiyun return ERR_PTR(-ENAMETOOLONG);
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /* can we conclude ENOENT locally? */
762*4882a593Smuzhiyun if (d_really_is_negative(dentry)) {
763*4882a593Smuzhiyun struct ceph_inode_info *ci = ceph_inode(dir);
764*4882a593Smuzhiyun struct ceph_dentry_info *di = ceph_dentry(dentry);
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
767*4882a593Smuzhiyun dout(" dir %p flags are 0x%lx\n", dir, ci->i_ceph_flags);
768*4882a593Smuzhiyun if (strncmp(dentry->d_name.name,
769*4882a593Smuzhiyun fsc->mount_options->snapdir_name,
770*4882a593Smuzhiyun dentry->d_name.len) &&
771*4882a593Smuzhiyun !is_root_ceph_dentry(dir, dentry) &&
772*4882a593Smuzhiyun ceph_test_mount_opt(fsc, DCACHE) &&
773*4882a593Smuzhiyun __ceph_dir_is_complete(ci) &&
774*4882a593Smuzhiyun __ceph_caps_issued_mask_metric(ci, CEPH_CAP_FILE_SHARED, 1)) {
775*4882a593Smuzhiyun __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD);
776*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
777*4882a593Smuzhiyun dout(" dir %p complete, -ENOENT\n", dir);
778*4882a593Smuzhiyun d_add(dentry, NULL);
779*4882a593Smuzhiyun di->lease_shared_gen = atomic_read(&ci->i_shared_gen);
780*4882a593Smuzhiyun return NULL;
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun op = ceph_snap(dir) == CEPH_SNAPDIR ?
786*4882a593Smuzhiyun CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
787*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
788*4882a593Smuzhiyun if (IS_ERR(req))
789*4882a593Smuzhiyun return ERR_CAST(req);
790*4882a593Smuzhiyun req->r_dentry = dget(dentry);
791*4882a593Smuzhiyun req->r_num_caps = 2;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
794*4882a593Smuzhiyun if (ceph_security_xattr_wanted(dir))
795*4882a593Smuzhiyun mask |= CEPH_CAP_XATTR_SHARED;
796*4882a593Smuzhiyun req->r_args.getattr.mask = cpu_to_le32(mask);
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun req->r_parent = dir;
799*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
800*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, NULL, req);
801*4882a593Smuzhiyun err = ceph_handle_snapdir(req, dentry, err);
802*4882a593Smuzhiyun dentry = ceph_finish_lookup(req, dentry, err);
803*4882a593Smuzhiyun ceph_mdsc_put_request(req); /* will dput(dentry) */
804*4882a593Smuzhiyun dout("lookup result=%p\n", dentry);
805*4882a593Smuzhiyun return dentry;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun /*
809*4882a593Smuzhiyun * If we do a create but get no trace back from the MDS, follow up with
810*4882a593Smuzhiyun * a lookup (the VFS expects us to link up the provided dentry).
811*4882a593Smuzhiyun */
ceph_handle_notrace_create(struct inode * dir,struct dentry * dentry)812*4882a593Smuzhiyun int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry)
813*4882a593Smuzhiyun {
814*4882a593Smuzhiyun struct dentry *result = ceph_lookup(dir, dentry, 0);
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun if (result && !IS_ERR(result)) {
817*4882a593Smuzhiyun /*
818*4882a593Smuzhiyun * We created the item, then did a lookup, and found
819*4882a593Smuzhiyun * it was already linked to another inode we already
820*4882a593Smuzhiyun * had in our cache (and thus got spliced). To not
821*4882a593Smuzhiyun * confuse VFS (especially when inode is a directory),
822*4882a593Smuzhiyun * we don't link our dentry to that inode, return an
823*4882a593Smuzhiyun * error instead.
824*4882a593Smuzhiyun *
825*4882a593Smuzhiyun * This event should be rare and it happens only when
826*4882a593Smuzhiyun * we talk to old MDS. Recent MDS does not send traceless
827*4882a593Smuzhiyun * reply for request that creates new inode.
828*4882a593Smuzhiyun */
829*4882a593Smuzhiyun d_drop(result);
830*4882a593Smuzhiyun return -ESTALE;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun return PTR_ERR(result);
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun
ceph_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)835*4882a593Smuzhiyun static int ceph_mknod(struct inode *dir, struct dentry *dentry,
836*4882a593Smuzhiyun umode_t mode, dev_t rdev)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
839*4882a593Smuzhiyun struct ceph_mds_request *req;
840*4882a593Smuzhiyun struct ceph_acl_sec_ctx as_ctx = {};
841*4882a593Smuzhiyun int err;
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun if (ceph_snap(dir) != CEPH_NOSNAP)
844*4882a593Smuzhiyun return -EROFS;
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun if (ceph_quota_is_max_files_exceeded(dir)) {
847*4882a593Smuzhiyun err = -EDQUOT;
848*4882a593Smuzhiyun goto out;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun err = ceph_pre_init_acls(dir, &mode, &as_ctx);
852*4882a593Smuzhiyun if (err < 0)
853*4882a593Smuzhiyun goto out;
854*4882a593Smuzhiyun err = ceph_security_init_secctx(dentry, mode, &as_ctx);
855*4882a593Smuzhiyun if (err < 0)
856*4882a593Smuzhiyun goto out;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n",
859*4882a593Smuzhiyun dir, dentry, mode, rdev);
860*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS);
861*4882a593Smuzhiyun if (IS_ERR(req)) {
862*4882a593Smuzhiyun err = PTR_ERR(req);
863*4882a593Smuzhiyun goto out;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun req->r_dentry = dget(dentry);
866*4882a593Smuzhiyun req->r_num_caps = 2;
867*4882a593Smuzhiyun req->r_parent = dir;
868*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
869*4882a593Smuzhiyun req->r_args.mknod.mode = cpu_to_le32(mode);
870*4882a593Smuzhiyun req->r_args.mknod.rdev = cpu_to_le32(rdev);
871*4882a593Smuzhiyun req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
872*4882a593Smuzhiyun req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
873*4882a593Smuzhiyun if (as_ctx.pagelist) {
874*4882a593Smuzhiyun req->r_pagelist = as_ctx.pagelist;
875*4882a593Smuzhiyun as_ctx.pagelist = NULL;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, dir, req);
878*4882a593Smuzhiyun if (!err && !req->r_reply_info.head->is_dentry)
879*4882a593Smuzhiyun err = ceph_handle_notrace_create(dir, dentry);
880*4882a593Smuzhiyun ceph_mdsc_put_request(req);
881*4882a593Smuzhiyun out:
882*4882a593Smuzhiyun if (!err)
883*4882a593Smuzhiyun ceph_init_inode_acls(d_inode(dentry), &as_ctx);
884*4882a593Smuzhiyun else
885*4882a593Smuzhiyun d_drop(dentry);
886*4882a593Smuzhiyun ceph_release_acl_sec_ctx(&as_ctx);
887*4882a593Smuzhiyun return err;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
ceph_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)890*4882a593Smuzhiyun static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode,
891*4882a593Smuzhiyun bool excl)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun return ceph_mknod(dir, dentry, mode, 0);
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun
ceph_symlink(struct inode * dir,struct dentry * dentry,const char * dest)896*4882a593Smuzhiyun static int ceph_symlink(struct inode *dir, struct dentry *dentry,
897*4882a593Smuzhiyun const char *dest)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
900*4882a593Smuzhiyun struct ceph_mds_request *req;
901*4882a593Smuzhiyun struct ceph_acl_sec_ctx as_ctx = {};
902*4882a593Smuzhiyun int err;
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun if (ceph_snap(dir) != CEPH_NOSNAP)
905*4882a593Smuzhiyun return -EROFS;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun if (ceph_quota_is_max_files_exceeded(dir)) {
908*4882a593Smuzhiyun err = -EDQUOT;
909*4882a593Smuzhiyun goto out;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun err = ceph_security_init_secctx(dentry, S_IFLNK | 0777, &as_ctx);
913*4882a593Smuzhiyun if (err < 0)
914*4882a593Smuzhiyun goto out;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest);
917*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS);
918*4882a593Smuzhiyun if (IS_ERR(req)) {
919*4882a593Smuzhiyun err = PTR_ERR(req);
920*4882a593Smuzhiyun goto out;
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun req->r_path2 = kstrdup(dest, GFP_KERNEL);
923*4882a593Smuzhiyun if (!req->r_path2) {
924*4882a593Smuzhiyun err = -ENOMEM;
925*4882a593Smuzhiyun ceph_mdsc_put_request(req);
926*4882a593Smuzhiyun goto out;
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun req->r_parent = dir;
929*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
930*4882a593Smuzhiyun req->r_dentry = dget(dentry);
931*4882a593Smuzhiyun req->r_num_caps = 2;
932*4882a593Smuzhiyun req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
933*4882a593Smuzhiyun req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
934*4882a593Smuzhiyun if (as_ctx.pagelist) {
935*4882a593Smuzhiyun req->r_pagelist = as_ctx.pagelist;
936*4882a593Smuzhiyun as_ctx.pagelist = NULL;
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, dir, req);
939*4882a593Smuzhiyun if (!err && !req->r_reply_info.head->is_dentry)
940*4882a593Smuzhiyun err = ceph_handle_notrace_create(dir, dentry);
941*4882a593Smuzhiyun ceph_mdsc_put_request(req);
942*4882a593Smuzhiyun out:
943*4882a593Smuzhiyun if (err)
944*4882a593Smuzhiyun d_drop(dentry);
945*4882a593Smuzhiyun ceph_release_acl_sec_ctx(&as_ctx);
946*4882a593Smuzhiyun return err;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun
ceph_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)949*4882a593Smuzhiyun static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
950*4882a593Smuzhiyun {
951*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
952*4882a593Smuzhiyun struct ceph_mds_request *req;
953*4882a593Smuzhiyun struct ceph_acl_sec_ctx as_ctx = {};
954*4882a593Smuzhiyun int err = -EROFS;
955*4882a593Smuzhiyun int op;
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun if (ceph_snap(dir) == CEPH_SNAPDIR) {
958*4882a593Smuzhiyun /* mkdir .snap/foo is a MKSNAP */
959*4882a593Smuzhiyun op = CEPH_MDS_OP_MKSNAP;
960*4882a593Smuzhiyun dout("mksnap dir %p snap '%pd' dn %p\n", dir,
961*4882a593Smuzhiyun dentry, dentry);
962*4882a593Smuzhiyun } else if (ceph_snap(dir) == CEPH_NOSNAP) {
963*4882a593Smuzhiyun dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode);
964*4882a593Smuzhiyun op = CEPH_MDS_OP_MKDIR;
965*4882a593Smuzhiyun } else {
966*4882a593Smuzhiyun goto out;
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun if (op == CEPH_MDS_OP_MKDIR &&
970*4882a593Smuzhiyun ceph_quota_is_max_files_exceeded(dir)) {
971*4882a593Smuzhiyun err = -EDQUOT;
972*4882a593Smuzhiyun goto out;
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun mode |= S_IFDIR;
976*4882a593Smuzhiyun err = ceph_pre_init_acls(dir, &mode, &as_ctx);
977*4882a593Smuzhiyun if (err < 0)
978*4882a593Smuzhiyun goto out;
979*4882a593Smuzhiyun err = ceph_security_init_secctx(dentry, mode, &as_ctx);
980*4882a593Smuzhiyun if (err < 0)
981*4882a593Smuzhiyun goto out;
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
984*4882a593Smuzhiyun if (IS_ERR(req)) {
985*4882a593Smuzhiyun err = PTR_ERR(req);
986*4882a593Smuzhiyun goto out;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun req->r_dentry = dget(dentry);
990*4882a593Smuzhiyun req->r_num_caps = 2;
991*4882a593Smuzhiyun req->r_parent = dir;
992*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
993*4882a593Smuzhiyun req->r_args.mkdir.mode = cpu_to_le32(mode);
994*4882a593Smuzhiyun req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL;
995*4882a593Smuzhiyun req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
996*4882a593Smuzhiyun if (as_ctx.pagelist) {
997*4882a593Smuzhiyun req->r_pagelist = as_ctx.pagelist;
998*4882a593Smuzhiyun as_ctx.pagelist = NULL;
999*4882a593Smuzhiyun }
1000*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, dir, req);
1001*4882a593Smuzhiyun if (!err &&
1002*4882a593Smuzhiyun !req->r_reply_info.head->is_target &&
1003*4882a593Smuzhiyun !req->r_reply_info.head->is_dentry)
1004*4882a593Smuzhiyun err = ceph_handle_notrace_create(dir, dentry);
1005*4882a593Smuzhiyun ceph_mdsc_put_request(req);
1006*4882a593Smuzhiyun out:
1007*4882a593Smuzhiyun if (!err)
1008*4882a593Smuzhiyun ceph_init_inode_acls(d_inode(dentry), &as_ctx);
1009*4882a593Smuzhiyun else
1010*4882a593Smuzhiyun d_drop(dentry);
1011*4882a593Smuzhiyun ceph_release_acl_sec_ctx(&as_ctx);
1012*4882a593Smuzhiyun return err;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun
ceph_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)1015*4882a593Smuzhiyun static int ceph_link(struct dentry *old_dentry, struct inode *dir,
1016*4882a593Smuzhiyun struct dentry *dentry)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(dir->i_sb);
1019*4882a593Smuzhiyun struct ceph_mds_request *req;
1020*4882a593Smuzhiyun int err;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun if (ceph_snap(dir) != CEPH_NOSNAP)
1023*4882a593Smuzhiyun return -EROFS;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun dout("link in dir %p old_dentry %p dentry %p\n", dir,
1026*4882a593Smuzhiyun old_dentry, dentry);
1027*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS);
1028*4882a593Smuzhiyun if (IS_ERR(req)) {
1029*4882a593Smuzhiyun d_drop(dentry);
1030*4882a593Smuzhiyun return PTR_ERR(req);
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun req->r_dentry = dget(dentry);
1033*4882a593Smuzhiyun req->r_num_caps = 2;
1034*4882a593Smuzhiyun req->r_old_dentry = dget(old_dentry);
1035*4882a593Smuzhiyun req->r_parent = dir;
1036*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1037*4882a593Smuzhiyun req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1038*4882a593Smuzhiyun req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1039*4882a593Smuzhiyun /* release LINK_SHARED on source inode (mds will lock it) */
1040*4882a593Smuzhiyun req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1041*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, dir, req);
1042*4882a593Smuzhiyun if (err) {
1043*4882a593Smuzhiyun d_drop(dentry);
1044*4882a593Smuzhiyun } else if (!req->r_reply_info.head->is_dentry) {
1045*4882a593Smuzhiyun ihold(d_inode(old_dentry));
1046*4882a593Smuzhiyun d_instantiate(dentry, d_inode(old_dentry));
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun ceph_mdsc_put_request(req);
1049*4882a593Smuzhiyun return err;
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun
ceph_async_unlink_cb(struct ceph_mds_client * mdsc,struct ceph_mds_request * req)1052*4882a593Smuzhiyun static void ceph_async_unlink_cb(struct ceph_mds_client *mdsc,
1053*4882a593Smuzhiyun struct ceph_mds_request *req)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun int result = req->r_err ? req->r_err :
1056*4882a593Smuzhiyun le32_to_cpu(req->r_reply_info.head->result);
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun if (result == -EJUKEBOX)
1059*4882a593Smuzhiyun goto out;
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun /* If op failed, mark everyone involved for errors */
1062*4882a593Smuzhiyun if (result) {
1063*4882a593Smuzhiyun int pathlen = 0;
1064*4882a593Smuzhiyun u64 base = 0;
1065*4882a593Smuzhiyun char *path = ceph_mdsc_build_path(req->r_dentry, &pathlen,
1066*4882a593Smuzhiyun &base, 0);
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun /* mark error on parent + clear complete */
1069*4882a593Smuzhiyun mapping_set_error(req->r_parent->i_mapping, result);
1070*4882a593Smuzhiyun ceph_dir_clear_complete(req->r_parent);
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun /* drop the dentry -- we don't know its status */
1073*4882a593Smuzhiyun if (!d_unhashed(req->r_dentry))
1074*4882a593Smuzhiyun d_drop(req->r_dentry);
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun /* mark inode itself for an error (since metadata is bogus) */
1077*4882a593Smuzhiyun mapping_set_error(req->r_old_inode->i_mapping, result);
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun pr_warn("ceph: async unlink failure path=(%llx)%s result=%d!\n",
1080*4882a593Smuzhiyun base, IS_ERR(path) ? "<<bad>>" : path, result);
1081*4882a593Smuzhiyun ceph_mdsc_free_path(path, pathlen);
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun out:
1084*4882a593Smuzhiyun iput(req->r_old_inode);
1085*4882a593Smuzhiyun ceph_mdsc_release_dir_caps(req);
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun
get_caps_for_async_unlink(struct inode * dir,struct dentry * dentry)1088*4882a593Smuzhiyun static int get_caps_for_async_unlink(struct inode *dir, struct dentry *dentry)
1089*4882a593Smuzhiyun {
1090*4882a593Smuzhiyun struct ceph_inode_info *ci = ceph_inode(dir);
1091*4882a593Smuzhiyun struct ceph_dentry_info *di;
1092*4882a593Smuzhiyun int got = 0, want = CEPH_CAP_FILE_EXCL | CEPH_CAP_DIR_UNLINK;
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
1095*4882a593Smuzhiyun if ((__ceph_caps_issued(ci, NULL) & want) == want) {
1096*4882a593Smuzhiyun ceph_take_cap_refs(ci, want, false);
1097*4882a593Smuzhiyun got = want;
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun /* If we didn't get anything, return 0 */
1102*4882a593Smuzhiyun if (!got)
1103*4882a593Smuzhiyun return 0;
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
1106*4882a593Smuzhiyun di = ceph_dentry(dentry);
1107*4882a593Smuzhiyun /*
1108*4882a593Smuzhiyun * - We are holding Fx, which implies Fs caps.
1109*4882a593Smuzhiyun * - Only support async unlink for primary linkage
1110*4882a593Smuzhiyun */
1111*4882a593Smuzhiyun if (atomic_read(&ci->i_shared_gen) != di->lease_shared_gen ||
1112*4882a593Smuzhiyun !(di->flags & CEPH_DENTRY_PRIMARY_LINK))
1113*4882a593Smuzhiyun want = 0;
1114*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun /* Do we still want what we've got? */
1117*4882a593Smuzhiyun if (want == got)
1118*4882a593Smuzhiyun return got;
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun ceph_put_cap_refs(ci, got);
1121*4882a593Smuzhiyun return 0;
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun /*
1125*4882a593Smuzhiyun * rmdir and unlink are differ only by the metadata op code
1126*4882a593Smuzhiyun */
ceph_unlink(struct inode * dir,struct dentry * dentry)1127*4882a593Smuzhiyun static int ceph_unlink(struct inode *dir, struct dentry *dentry)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
1130*4882a593Smuzhiyun struct ceph_mds_client *mdsc = fsc->mdsc;
1131*4882a593Smuzhiyun struct inode *inode = d_inode(dentry);
1132*4882a593Smuzhiyun struct ceph_mds_request *req;
1133*4882a593Smuzhiyun bool try_async = ceph_test_mount_opt(fsc, ASYNC_DIROPS);
1134*4882a593Smuzhiyun int err = -EROFS;
1135*4882a593Smuzhiyun int op;
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun if (ceph_snap(dir) == CEPH_SNAPDIR) {
1138*4882a593Smuzhiyun /* rmdir .snap/foo is RMSNAP */
1139*4882a593Smuzhiyun dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry);
1140*4882a593Smuzhiyun op = CEPH_MDS_OP_RMSNAP;
1141*4882a593Smuzhiyun } else if (ceph_snap(dir) == CEPH_NOSNAP) {
1142*4882a593Smuzhiyun dout("unlink/rmdir dir %p dn %p inode %p\n",
1143*4882a593Smuzhiyun dir, dentry, inode);
1144*4882a593Smuzhiyun op = d_is_dir(dentry) ?
1145*4882a593Smuzhiyun CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK;
1146*4882a593Smuzhiyun } else
1147*4882a593Smuzhiyun goto out;
1148*4882a593Smuzhiyun retry:
1149*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1150*4882a593Smuzhiyun if (IS_ERR(req)) {
1151*4882a593Smuzhiyun err = PTR_ERR(req);
1152*4882a593Smuzhiyun goto out;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun req->r_dentry = dget(dentry);
1155*4882a593Smuzhiyun req->r_num_caps = 2;
1156*4882a593Smuzhiyun req->r_parent = dir;
1157*4882a593Smuzhiyun req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1158*4882a593Smuzhiyun req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1159*4882a593Smuzhiyun req->r_inode_drop = ceph_drop_caps_for_unlink(inode);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun if (try_async && op == CEPH_MDS_OP_UNLINK &&
1162*4882a593Smuzhiyun (req->r_dir_caps = get_caps_for_async_unlink(dir, dentry))) {
1163*4882a593Smuzhiyun dout("async unlink on %llu/%.*s caps=%s", ceph_ino(dir),
1164*4882a593Smuzhiyun dentry->d_name.len, dentry->d_name.name,
1165*4882a593Smuzhiyun ceph_cap_string(req->r_dir_caps));
1166*4882a593Smuzhiyun set_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags);
1167*4882a593Smuzhiyun req->r_callback = ceph_async_unlink_cb;
1168*4882a593Smuzhiyun req->r_old_inode = d_inode(dentry);
1169*4882a593Smuzhiyun ihold(req->r_old_inode);
1170*4882a593Smuzhiyun err = ceph_mdsc_submit_request(mdsc, dir, req);
1171*4882a593Smuzhiyun if (!err) {
1172*4882a593Smuzhiyun /*
1173*4882a593Smuzhiyun * We have enough caps, so we assume that the unlink
1174*4882a593Smuzhiyun * will succeed. Fix up the target inode and dcache.
1175*4882a593Smuzhiyun */
1176*4882a593Smuzhiyun drop_nlink(inode);
1177*4882a593Smuzhiyun d_delete(dentry);
1178*4882a593Smuzhiyun } else if (err == -EJUKEBOX) {
1179*4882a593Smuzhiyun try_async = false;
1180*4882a593Smuzhiyun ceph_mdsc_put_request(req);
1181*4882a593Smuzhiyun goto retry;
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun } else {
1184*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1185*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, dir, req);
1186*4882a593Smuzhiyun if (!err && !req->r_reply_info.head->is_dentry)
1187*4882a593Smuzhiyun d_delete(dentry);
1188*4882a593Smuzhiyun }
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun ceph_mdsc_put_request(req);
1191*4882a593Smuzhiyun out:
1192*4882a593Smuzhiyun return err;
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun
ceph_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1195*4882a593Smuzhiyun static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry,
1196*4882a593Smuzhiyun struct inode *new_dir, struct dentry *new_dentry,
1197*4882a593Smuzhiyun unsigned int flags)
1198*4882a593Smuzhiyun {
1199*4882a593Smuzhiyun struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(old_dir->i_sb);
1200*4882a593Smuzhiyun struct ceph_mds_request *req;
1201*4882a593Smuzhiyun int op = CEPH_MDS_OP_RENAME;
1202*4882a593Smuzhiyun int err;
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun if (flags)
1205*4882a593Smuzhiyun return -EINVAL;
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun if (ceph_snap(old_dir) != ceph_snap(new_dir))
1208*4882a593Smuzhiyun return -EXDEV;
1209*4882a593Smuzhiyun if (ceph_snap(old_dir) != CEPH_NOSNAP) {
1210*4882a593Smuzhiyun if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR)
1211*4882a593Smuzhiyun op = CEPH_MDS_OP_RENAMESNAP;
1212*4882a593Smuzhiyun else
1213*4882a593Smuzhiyun return -EROFS;
1214*4882a593Smuzhiyun } else if (old_dir != new_dir) {
1215*4882a593Smuzhiyun err = ceph_quota_check_rename(mdsc, d_inode(old_dentry),
1216*4882a593Smuzhiyun new_dir);
1217*4882a593Smuzhiyun if (err)
1218*4882a593Smuzhiyun return err;
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun dout("rename dir %p dentry %p to dir %p dentry %p\n",
1222*4882a593Smuzhiyun old_dir, old_dentry, new_dir, new_dentry);
1223*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS);
1224*4882a593Smuzhiyun if (IS_ERR(req))
1225*4882a593Smuzhiyun return PTR_ERR(req);
1226*4882a593Smuzhiyun ihold(old_dir);
1227*4882a593Smuzhiyun req->r_dentry = dget(new_dentry);
1228*4882a593Smuzhiyun req->r_num_caps = 2;
1229*4882a593Smuzhiyun req->r_old_dentry = dget(old_dentry);
1230*4882a593Smuzhiyun req->r_old_dentry_dir = old_dir;
1231*4882a593Smuzhiyun req->r_parent = new_dir;
1232*4882a593Smuzhiyun set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
1233*4882a593Smuzhiyun req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED;
1234*4882a593Smuzhiyun req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL;
1235*4882a593Smuzhiyun req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
1236*4882a593Smuzhiyun req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
1237*4882a593Smuzhiyun /* release LINK_RDCACHE on source inode (mds will lock it) */
1238*4882a593Smuzhiyun req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL;
1239*4882a593Smuzhiyun if (d_really_is_positive(new_dentry)) {
1240*4882a593Smuzhiyun req->r_inode_drop =
1241*4882a593Smuzhiyun ceph_drop_caps_for_unlink(d_inode(new_dentry));
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, old_dir, req);
1244*4882a593Smuzhiyun if (!err && !req->r_reply_info.head->is_dentry) {
1245*4882a593Smuzhiyun /*
1246*4882a593Smuzhiyun * Normally d_move() is done by fill_trace (called by
1247*4882a593Smuzhiyun * do_request, above). If there is no trace, we need
1248*4882a593Smuzhiyun * to do it here.
1249*4882a593Smuzhiyun */
1250*4882a593Smuzhiyun d_move(old_dentry, new_dentry);
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun ceph_mdsc_put_request(req);
1253*4882a593Smuzhiyun return err;
1254*4882a593Smuzhiyun }
1255*4882a593Smuzhiyun
1256*4882a593Smuzhiyun /*
1257*4882a593Smuzhiyun * Move dentry to tail of mdsc->dentry_leases list when lease is updated.
1258*4882a593Smuzhiyun * Leases at front of the list will expire first. (Assume all leases have
1259*4882a593Smuzhiyun * similar duration)
1260*4882a593Smuzhiyun *
1261*4882a593Smuzhiyun * Called under dentry->d_lock.
1262*4882a593Smuzhiyun */
__ceph_dentry_lease_touch(struct ceph_dentry_info * di)1263*4882a593Smuzhiyun void __ceph_dentry_lease_touch(struct ceph_dentry_info *di)
1264*4882a593Smuzhiyun {
1265*4882a593Smuzhiyun struct dentry *dn = di->dentry;
1266*4882a593Smuzhiyun struct ceph_mds_client *mdsc;
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun dout("dentry_lease_touch %p %p '%pd'\n", di, dn, dn);
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun di->flags |= CEPH_DENTRY_LEASE_LIST;
1271*4882a593Smuzhiyun if (di->flags & CEPH_DENTRY_SHRINK_LIST) {
1272*4882a593Smuzhiyun di->flags |= CEPH_DENTRY_REFERENCED;
1273*4882a593Smuzhiyun return;
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun
1276*4882a593Smuzhiyun mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1277*4882a593Smuzhiyun spin_lock(&mdsc->dentry_list_lock);
1278*4882a593Smuzhiyun list_move_tail(&di->lease_list, &mdsc->dentry_leases);
1279*4882a593Smuzhiyun spin_unlock(&mdsc->dentry_list_lock);
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun
__dentry_dir_lease_touch(struct ceph_mds_client * mdsc,struct ceph_dentry_info * di)1282*4882a593Smuzhiyun static void __dentry_dir_lease_touch(struct ceph_mds_client* mdsc,
1283*4882a593Smuzhiyun struct ceph_dentry_info *di)
1284*4882a593Smuzhiyun {
1285*4882a593Smuzhiyun di->flags &= ~(CEPH_DENTRY_LEASE_LIST | CEPH_DENTRY_REFERENCED);
1286*4882a593Smuzhiyun di->lease_gen = 0;
1287*4882a593Smuzhiyun di->time = jiffies;
1288*4882a593Smuzhiyun list_move_tail(&di->lease_list, &mdsc->dentry_dir_leases);
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun
1291*4882a593Smuzhiyun /*
1292*4882a593Smuzhiyun * When dir lease is used, add dentry to tail of mdsc->dentry_dir_leases
1293*4882a593Smuzhiyun * list if it's not in the list, otherwise set 'referenced' flag.
1294*4882a593Smuzhiyun *
1295*4882a593Smuzhiyun * Called under dentry->d_lock.
1296*4882a593Smuzhiyun */
__ceph_dentry_dir_lease_touch(struct ceph_dentry_info * di)1297*4882a593Smuzhiyun void __ceph_dentry_dir_lease_touch(struct ceph_dentry_info *di)
1298*4882a593Smuzhiyun {
1299*4882a593Smuzhiyun struct dentry *dn = di->dentry;
1300*4882a593Smuzhiyun struct ceph_mds_client *mdsc;
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun dout("dentry_dir_lease_touch %p %p '%pd' (offset 0x%llx)\n",
1303*4882a593Smuzhiyun di, dn, dn, di->offset);
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun if (!list_empty(&di->lease_list)) {
1306*4882a593Smuzhiyun if (di->flags & CEPH_DENTRY_LEASE_LIST) {
1307*4882a593Smuzhiyun /* don't remove dentry from dentry lease list
1308*4882a593Smuzhiyun * if its lease is valid */
1309*4882a593Smuzhiyun if (__dentry_lease_is_valid(di))
1310*4882a593Smuzhiyun return;
1311*4882a593Smuzhiyun } else {
1312*4882a593Smuzhiyun di->flags |= CEPH_DENTRY_REFERENCED;
1313*4882a593Smuzhiyun return;
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun }
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun if (di->flags & CEPH_DENTRY_SHRINK_LIST) {
1318*4882a593Smuzhiyun di->flags |= CEPH_DENTRY_REFERENCED;
1319*4882a593Smuzhiyun di->flags &= ~CEPH_DENTRY_LEASE_LIST;
1320*4882a593Smuzhiyun return;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun mdsc = ceph_sb_to_client(dn->d_sb)->mdsc;
1324*4882a593Smuzhiyun spin_lock(&mdsc->dentry_list_lock);
1325*4882a593Smuzhiyun __dentry_dir_lease_touch(mdsc, di),
1326*4882a593Smuzhiyun spin_unlock(&mdsc->dentry_list_lock);
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
__dentry_lease_unlist(struct ceph_dentry_info * di)1329*4882a593Smuzhiyun static void __dentry_lease_unlist(struct ceph_dentry_info *di)
1330*4882a593Smuzhiyun {
1331*4882a593Smuzhiyun struct ceph_mds_client *mdsc;
1332*4882a593Smuzhiyun if (di->flags & CEPH_DENTRY_SHRINK_LIST)
1333*4882a593Smuzhiyun return;
1334*4882a593Smuzhiyun if (list_empty(&di->lease_list))
1335*4882a593Smuzhiyun return;
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyun mdsc = ceph_sb_to_client(di->dentry->d_sb)->mdsc;
1338*4882a593Smuzhiyun spin_lock(&mdsc->dentry_list_lock);
1339*4882a593Smuzhiyun list_del_init(&di->lease_list);
1340*4882a593Smuzhiyun spin_unlock(&mdsc->dentry_list_lock);
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun enum {
1344*4882a593Smuzhiyun KEEP = 0,
1345*4882a593Smuzhiyun DELETE = 1,
1346*4882a593Smuzhiyun TOUCH = 2,
1347*4882a593Smuzhiyun STOP = 4,
1348*4882a593Smuzhiyun };
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun struct ceph_lease_walk_control {
1351*4882a593Smuzhiyun bool dir_lease;
1352*4882a593Smuzhiyun bool expire_dir_lease;
1353*4882a593Smuzhiyun unsigned long nr_to_scan;
1354*4882a593Smuzhiyun unsigned long dir_lease_ttl;
1355*4882a593Smuzhiyun };
1356*4882a593Smuzhiyun
1357*4882a593Smuzhiyun static unsigned long
__dentry_leases_walk(struct ceph_mds_client * mdsc,struct ceph_lease_walk_control * lwc,int (* check)(struct dentry *,void *))1358*4882a593Smuzhiyun __dentry_leases_walk(struct ceph_mds_client *mdsc,
1359*4882a593Smuzhiyun struct ceph_lease_walk_control *lwc,
1360*4882a593Smuzhiyun int (*check)(struct dentry*, void*))
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun struct ceph_dentry_info *di, *tmp;
1363*4882a593Smuzhiyun struct dentry *dentry, *last = NULL;
1364*4882a593Smuzhiyun struct list_head* list;
1365*4882a593Smuzhiyun LIST_HEAD(dispose);
1366*4882a593Smuzhiyun unsigned long freed = 0;
1367*4882a593Smuzhiyun int ret = 0;
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun list = lwc->dir_lease ? &mdsc->dentry_dir_leases : &mdsc->dentry_leases;
1370*4882a593Smuzhiyun spin_lock(&mdsc->dentry_list_lock);
1371*4882a593Smuzhiyun list_for_each_entry_safe(di, tmp, list, lease_list) {
1372*4882a593Smuzhiyun if (!lwc->nr_to_scan)
1373*4882a593Smuzhiyun break;
1374*4882a593Smuzhiyun --lwc->nr_to_scan;
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun dentry = di->dentry;
1377*4882a593Smuzhiyun if (last == dentry)
1378*4882a593Smuzhiyun break;
1379*4882a593Smuzhiyun
1380*4882a593Smuzhiyun if (!spin_trylock(&dentry->d_lock))
1381*4882a593Smuzhiyun continue;
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun if (__lockref_is_dead(&dentry->d_lockref)) {
1384*4882a593Smuzhiyun list_del_init(&di->lease_list);
1385*4882a593Smuzhiyun goto next;
1386*4882a593Smuzhiyun }
1387*4882a593Smuzhiyun
1388*4882a593Smuzhiyun ret = check(dentry, lwc);
1389*4882a593Smuzhiyun if (ret & TOUCH) {
1390*4882a593Smuzhiyun /* move it into tail of dir lease list */
1391*4882a593Smuzhiyun __dentry_dir_lease_touch(mdsc, di);
1392*4882a593Smuzhiyun if (!last)
1393*4882a593Smuzhiyun last = dentry;
1394*4882a593Smuzhiyun }
1395*4882a593Smuzhiyun if (ret & DELETE) {
1396*4882a593Smuzhiyun /* stale lease */
1397*4882a593Smuzhiyun di->flags &= ~CEPH_DENTRY_REFERENCED;
1398*4882a593Smuzhiyun if (dentry->d_lockref.count > 0) {
1399*4882a593Smuzhiyun /* update_dentry_lease() will re-add
1400*4882a593Smuzhiyun * it to lease list, or
1401*4882a593Smuzhiyun * ceph_d_delete() will return 1 when
1402*4882a593Smuzhiyun * last reference is dropped */
1403*4882a593Smuzhiyun list_del_init(&di->lease_list);
1404*4882a593Smuzhiyun } else {
1405*4882a593Smuzhiyun di->flags |= CEPH_DENTRY_SHRINK_LIST;
1406*4882a593Smuzhiyun list_move_tail(&di->lease_list, &dispose);
1407*4882a593Smuzhiyun dget_dlock(dentry);
1408*4882a593Smuzhiyun }
1409*4882a593Smuzhiyun }
1410*4882a593Smuzhiyun next:
1411*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1412*4882a593Smuzhiyun if (ret & STOP)
1413*4882a593Smuzhiyun break;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun spin_unlock(&mdsc->dentry_list_lock);
1416*4882a593Smuzhiyun
1417*4882a593Smuzhiyun while (!list_empty(&dispose)) {
1418*4882a593Smuzhiyun di = list_first_entry(&dispose, struct ceph_dentry_info,
1419*4882a593Smuzhiyun lease_list);
1420*4882a593Smuzhiyun dentry = di->dentry;
1421*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun list_del_init(&di->lease_list);
1424*4882a593Smuzhiyun di->flags &= ~CEPH_DENTRY_SHRINK_LIST;
1425*4882a593Smuzhiyun if (di->flags & CEPH_DENTRY_REFERENCED) {
1426*4882a593Smuzhiyun spin_lock(&mdsc->dentry_list_lock);
1427*4882a593Smuzhiyun if (di->flags & CEPH_DENTRY_LEASE_LIST) {
1428*4882a593Smuzhiyun list_add_tail(&di->lease_list,
1429*4882a593Smuzhiyun &mdsc->dentry_leases);
1430*4882a593Smuzhiyun } else {
1431*4882a593Smuzhiyun __dentry_dir_lease_touch(mdsc, di);
1432*4882a593Smuzhiyun }
1433*4882a593Smuzhiyun spin_unlock(&mdsc->dentry_list_lock);
1434*4882a593Smuzhiyun } else {
1435*4882a593Smuzhiyun freed++;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1439*4882a593Smuzhiyun /* ceph_d_delete() does the trick */
1440*4882a593Smuzhiyun dput(dentry);
1441*4882a593Smuzhiyun }
1442*4882a593Smuzhiyun return freed;
1443*4882a593Smuzhiyun }
1444*4882a593Smuzhiyun
__dentry_lease_check(struct dentry * dentry,void * arg)1445*4882a593Smuzhiyun static int __dentry_lease_check(struct dentry *dentry, void *arg)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun struct ceph_dentry_info *di = ceph_dentry(dentry);
1448*4882a593Smuzhiyun int ret;
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun if (__dentry_lease_is_valid(di))
1451*4882a593Smuzhiyun return STOP;
1452*4882a593Smuzhiyun ret = __dir_lease_try_check(dentry);
1453*4882a593Smuzhiyun if (ret == -EBUSY)
1454*4882a593Smuzhiyun return KEEP;
1455*4882a593Smuzhiyun if (ret > 0)
1456*4882a593Smuzhiyun return TOUCH;
1457*4882a593Smuzhiyun return DELETE;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun
__dir_lease_check(struct dentry * dentry,void * arg)1460*4882a593Smuzhiyun static int __dir_lease_check(struct dentry *dentry, void *arg)
1461*4882a593Smuzhiyun {
1462*4882a593Smuzhiyun struct ceph_lease_walk_control *lwc = arg;
1463*4882a593Smuzhiyun struct ceph_dentry_info *di = ceph_dentry(dentry);
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun int ret = __dir_lease_try_check(dentry);
1466*4882a593Smuzhiyun if (ret == -EBUSY)
1467*4882a593Smuzhiyun return KEEP;
1468*4882a593Smuzhiyun if (ret > 0) {
1469*4882a593Smuzhiyun if (time_before(jiffies, di->time + lwc->dir_lease_ttl))
1470*4882a593Smuzhiyun return STOP;
1471*4882a593Smuzhiyun /* Move dentry to tail of dir lease list if we don't want
1472*4882a593Smuzhiyun * to delete it. So dentries in the list are checked in a
1473*4882a593Smuzhiyun * round robin manner */
1474*4882a593Smuzhiyun if (!lwc->expire_dir_lease)
1475*4882a593Smuzhiyun return TOUCH;
1476*4882a593Smuzhiyun if (dentry->d_lockref.count > 0 ||
1477*4882a593Smuzhiyun (di->flags & CEPH_DENTRY_REFERENCED))
1478*4882a593Smuzhiyun return TOUCH;
1479*4882a593Smuzhiyun /* invalidate dir lease */
1480*4882a593Smuzhiyun di->lease_shared_gen = 0;
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun return DELETE;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun
ceph_trim_dentries(struct ceph_mds_client * mdsc)1485*4882a593Smuzhiyun int ceph_trim_dentries(struct ceph_mds_client *mdsc)
1486*4882a593Smuzhiyun {
1487*4882a593Smuzhiyun struct ceph_lease_walk_control lwc;
1488*4882a593Smuzhiyun unsigned long count;
1489*4882a593Smuzhiyun unsigned long freed;
1490*4882a593Smuzhiyun
1491*4882a593Smuzhiyun spin_lock(&mdsc->caps_list_lock);
1492*4882a593Smuzhiyun if (mdsc->caps_use_max > 0 &&
1493*4882a593Smuzhiyun mdsc->caps_use_count > mdsc->caps_use_max)
1494*4882a593Smuzhiyun count = mdsc->caps_use_count - mdsc->caps_use_max;
1495*4882a593Smuzhiyun else
1496*4882a593Smuzhiyun count = 0;
1497*4882a593Smuzhiyun spin_unlock(&mdsc->caps_list_lock);
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun lwc.dir_lease = false;
1500*4882a593Smuzhiyun lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE * 2;
1501*4882a593Smuzhiyun freed = __dentry_leases_walk(mdsc, &lwc, __dentry_lease_check);
1502*4882a593Smuzhiyun if (!lwc.nr_to_scan) /* more invalid leases */
1503*4882a593Smuzhiyun return -EAGAIN;
1504*4882a593Smuzhiyun
1505*4882a593Smuzhiyun if (lwc.nr_to_scan < CEPH_CAPS_PER_RELEASE)
1506*4882a593Smuzhiyun lwc.nr_to_scan = CEPH_CAPS_PER_RELEASE;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun lwc.dir_lease = true;
1509*4882a593Smuzhiyun lwc.expire_dir_lease = freed < count;
1510*4882a593Smuzhiyun lwc.dir_lease_ttl = mdsc->fsc->mount_options->caps_wanted_delay_max * HZ;
1511*4882a593Smuzhiyun freed +=__dentry_leases_walk(mdsc, &lwc, __dir_lease_check);
1512*4882a593Smuzhiyun if (!lwc.nr_to_scan) /* more to check */
1513*4882a593Smuzhiyun return -EAGAIN;
1514*4882a593Smuzhiyun
1515*4882a593Smuzhiyun return freed > 0 ? 1 : 0;
1516*4882a593Smuzhiyun }
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun /*
1519*4882a593Smuzhiyun * Ensure a dentry lease will no longer revalidate.
1520*4882a593Smuzhiyun */
ceph_invalidate_dentry_lease(struct dentry * dentry)1521*4882a593Smuzhiyun void ceph_invalidate_dentry_lease(struct dentry *dentry)
1522*4882a593Smuzhiyun {
1523*4882a593Smuzhiyun struct ceph_dentry_info *di = ceph_dentry(dentry);
1524*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
1525*4882a593Smuzhiyun di->time = jiffies;
1526*4882a593Smuzhiyun di->lease_shared_gen = 0;
1527*4882a593Smuzhiyun di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1528*4882a593Smuzhiyun __dentry_lease_unlist(di);
1529*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1530*4882a593Smuzhiyun }
1531*4882a593Smuzhiyun
1532*4882a593Smuzhiyun /*
1533*4882a593Smuzhiyun * Check if dentry lease is valid. If not, delete the lease. Try to
1534*4882a593Smuzhiyun * renew if the least is more than half up.
1535*4882a593Smuzhiyun */
__dentry_lease_is_valid(struct ceph_dentry_info * di)1536*4882a593Smuzhiyun static bool __dentry_lease_is_valid(struct ceph_dentry_info *di)
1537*4882a593Smuzhiyun {
1538*4882a593Smuzhiyun struct ceph_mds_session *session;
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun if (!di->lease_gen)
1541*4882a593Smuzhiyun return false;
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun session = di->lease_session;
1544*4882a593Smuzhiyun if (session) {
1545*4882a593Smuzhiyun u32 gen;
1546*4882a593Smuzhiyun unsigned long ttl;
1547*4882a593Smuzhiyun
1548*4882a593Smuzhiyun spin_lock(&session->s_gen_ttl_lock);
1549*4882a593Smuzhiyun gen = session->s_cap_gen;
1550*4882a593Smuzhiyun ttl = session->s_cap_ttl;
1551*4882a593Smuzhiyun spin_unlock(&session->s_gen_ttl_lock);
1552*4882a593Smuzhiyun
1553*4882a593Smuzhiyun if (di->lease_gen == gen &&
1554*4882a593Smuzhiyun time_before(jiffies, ttl) &&
1555*4882a593Smuzhiyun time_before(jiffies, di->time))
1556*4882a593Smuzhiyun return true;
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun di->lease_gen = 0;
1559*4882a593Smuzhiyun return false;
1560*4882a593Smuzhiyun }
1561*4882a593Smuzhiyun
dentry_lease_is_valid(struct dentry * dentry,unsigned int flags)1562*4882a593Smuzhiyun static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags)
1563*4882a593Smuzhiyun {
1564*4882a593Smuzhiyun struct ceph_dentry_info *di;
1565*4882a593Smuzhiyun struct ceph_mds_session *session = NULL;
1566*4882a593Smuzhiyun u32 seq = 0;
1567*4882a593Smuzhiyun int valid = 0;
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
1570*4882a593Smuzhiyun di = ceph_dentry(dentry);
1571*4882a593Smuzhiyun if (di && __dentry_lease_is_valid(di)) {
1572*4882a593Smuzhiyun valid = 1;
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun if (di->lease_renew_after &&
1575*4882a593Smuzhiyun time_after(jiffies, di->lease_renew_after)) {
1576*4882a593Smuzhiyun /*
1577*4882a593Smuzhiyun * We should renew. If we're in RCU walk mode
1578*4882a593Smuzhiyun * though, we can't do that so just return
1579*4882a593Smuzhiyun * -ECHILD.
1580*4882a593Smuzhiyun */
1581*4882a593Smuzhiyun if (flags & LOOKUP_RCU) {
1582*4882a593Smuzhiyun valid = -ECHILD;
1583*4882a593Smuzhiyun } else {
1584*4882a593Smuzhiyun session = ceph_get_mds_session(di->lease_session);
1585*4882a593Smuzhiyun seq = di->lease_seq;
1586*4882a593Smuzhiyun di->lease_renew_after = 0;
1587*4882a593Smuzhiyun di->lease_renew_from = jiffies;
1588*4882a593Smuzhiyun }
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun }
1591*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun if (session) {
1594*4882a593Smuzhiyun ceph_mdsc_lease_send_msg(session, dentry,
1595*4882a593Smuzhiyun CEPH_MDS_LEASE_RENEW, seq);
1596*4882a593Smuzhiyun ceph_put_mds_session(session);
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid);
1599*4882a593Smuzhiyun return valid;
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun /*
1603*4882a593Smuzhiyun * Called under dentry->d_lock.
1604*4882a593Smuzhiyun */
__dir_lease_try_check(const struct dentry * dentry)1605*4882a593Smuzhiyun static int __dir_lease_try_check(const struct dentry *dentry)
1606*4882a593Smuzhiyun {
1607*4882a593Smuzhiyun struct ceph_dentry_info *di = ceph_dentry(dentry);
1608*4882a593Smuzhiyun struct inode *dir;
1609*4882a593Smuzhiyun struct ceph_inode_info *ci;
1610*4882a593Smuzhiyun int valid = 0;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun if (!di->lease_shared_gen)
1613*4882a593Smuzhiyun return 0;
1614*4882a593Smuzhiyun if (IS_ROOT(dentry))
1615*4882a593Smuzhiyun return 0;
1616*4882a593Smuzhiyun
1617*4882a593Smuzhiyun dir = d_inode(dentry->d_parent);
1618*4882a593Smuzhiyun ci = ceph_inode(dir);
1619*4882a593Smuzhiyun
1620*4882a593Smuzhiyun if (spin_trylock(&ci->i_ceph_lock)) {
1621*4882a593Smuzhiyun if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen &&
1622*4882a593Smuzhiyun __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 0))
1623*4882a593Smuzhiyun valid = 1;
1624*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
1625*4882a593Smuzhiyun } else {
1626*4882a593Smuzhiyun valid = -EBUSY;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun if (!valid)
1630*4882a593Smuzhiyun di->lease_shared_gen = 0;
1631*4882a593Smuzhiyun return valid;
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun /*
1635*4882a593Smuzhiyun * Check if directory-wide content lease/cap is valid.
1636*4882a593Smuzhiyun */
dir_lease_is_valid(struct inode * dir,struct dentry * dentry,struct ceph_mds_client * mdsc)1637*4882a593Smuzhiyun static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry,
1638*4882a593Smuzhiyun struct ceph_mds_client *mdsc)
1639*4882a593Smuzhiyun {
1640*4882a593Smuzhiyun struct ceph_inode_info *ci = ceph_inode(dir);
1641*4882a593Smuzhiyun int valid;
1642*4882a593Smuzhiyun int shared_gen;
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun spin_lock(&ci->i_ceph_lock);
1645*4882a593Smuzhiyun valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1);
1646*4882a593Smuzhiyun if (valid) {
1647*4882a593Smuzhiyun __ceph_touch_fmode(ci, mdsc, CEPH_FILE_MODE_RD);
1648*4882a593Smuzhiyun shared_gen = atomic_read(&ci->i_shared_gen);
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun spin_unlock(&ci->i_ceph_lock);
1651*4882a593Smuzhiyun if (valid) {
1652*4882a593Smuzhiyun struct ceph_dentry_info *di;
1653*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
1654*4882a593Smuzhiyun di = ceph_dentry(dentry);
1655*4882a593Smuzhiyun if (dir == d_inode(dentry->d_parent) &&
1656*4882a593Smuzhiyun di && di->lease_shared_gen == shared_gen)
1657*4882a593Smuzhiyun __ceph_dentry_dir_lease_touch(di);
1658*4882a593Smuzhiyun else
1659*4882a593Smuzhiyun valid = 0;
1660*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1661*4882a593Smuzhiyun }
1662*4882a593Smuzhiyun dout("dir_lease_is_valid dir %p v%u dentry %p = %d\n",
1663*4882a593Smuzhiyun dir, (unsigned)atomic_read(&ci->i_shared_gen), dentry, valid);
1664*4882a593Smuzhiyun return valid;
1665*4882a593Smuzhiyun }
1666*4882a593Smuzhiyun
1667*4882a593Smuzhiyun /*
1668*4882a593Smuzhiyun * Check if cached dentry can be trusted.
1669*4882a593Smuzhiyun */
ceph_d_revalidate(struct dentry * dentry,unsigned int flags)1670*4882a593Smuzhiyun static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags)
1671*4882a593Smuzhiyun {
1672*4882a593Smuzhiyun int valid = 0;
1673*4882a593Smuzhiyun struct dentry *parent;
1674*4882a593Smuzhiyun struct inode *dir, *inode;
1675*4882a593Smuzhiyun struct ceph_mds_client *mdsc;
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun if (flags & LOOKUP_RCU) {
1678*4882a593Smuzhiyun parent = READ_ONCE(dentry->d_parent);
1679*4882a593Smuzhiyun dir = d_inode_rcu(parent);
1680*4882a593Smuzhiyun if (!dir)
1681*4882a593Smuzhiyun return -ECHILD;
1682*4882a593Smuzhiyun inode = d_inode_rcu(dentry);
1683*4882a593Smuzhiyun } else {
1684*4882a593Smuzhiyun parent = dget_parent(dentry);
1685*4882a593Smuzhiyun dir = d_inode(parent);
1686*4882a593Smuzhiyun inode = d_inode(dentry);
1687*4882a593Smuzhiyun }
1688*4882a593Smuzhiyun
1689*4882a593Smuzhiyun dout("d_revalidate %p '%pd' inode %p offset 0x%llx\n", dentry,
1690*4882a593Smuzhiyun dentry, inode, ceph_dentry(dentry)->offset);
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun mdsc = ceph_sb_to_client(dir->i_sb)->mdsc;
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun /* always trust cached snapped dentries, snapdir dentry */
1695*4882a593Smuzhiyun if (ceph_snap(dir) != CEPH_NOSNAP) {
1696*4882a593Smuzhiyun dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry,
1697*4882a593Smuzhiyun dentry, inode);
1698*4882a593Smuzhiyun valid = 1;
1699*4882a593Smuzhiyun } else if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
1700*4882a593Smuzhiyun valid = 1;
1701*4882a593Smuzhiyun } else {
1702*4882a593Smuzhiyun valid = dentry_lease_is_valid(dentry, flags);
1703*4882a593Smuzhiyun if (valid == -ECHILD)
1704*4882a593Smuzhiyun return valid;
1705*4882a593Smuzhiyun if (valid || dir_lease_is_valid(dir, dentry, mdsc)) {
1706*4882a593Smuzhiyun if (inode)
1707*4882a593Smuzhiyun valid = ceph_is_any_caps(inode);
1708*4882a593Smuzhiyun else
1709*4882a593Smuzhiyun valid = 1;
1710*4882a593Smuzhiyun }
1711*4882a593Smuzhiyun }
1712*4882a593Smuzhiyun
1713*4882a593Smuzhiyun if (!valid) {
1714*4882a593Smuzhiyun struct ceph_mds_request *req;
1715*4882a593Smuzhiyun int op, err;
1716*4882a593Smuzhiyun u32 mask;
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun if (flags & LOOKUP_RCU)
1719*4882a593Smuzhiyun return -ECHILD;
1720*4882a593Smuzhiyun
1721*4882a593Smuzhiyun percpu_counter_inc(&mdsc->metric.d_lease_mis);
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun op = ceph_snap(dir) == CEPH_SNAPDIR ?
1724*4882a593Smuzhiyun CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP;
1725*4882a593Smuzhiyun req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS);
1726*4882a593Smuzhiyun if (!IS_ERR(req)) {
1727*4882a593Smuzhiyun req->r_dentry = dget(dentry);
1728*4882a593Smuzhiyun req->r_num_caps = 2;
1729*4882a593Smuzhiyun req->r_parent = dir;
1730*4882a593Smuzhiyun
1731*4882a593Smuzhiyun mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED;
1732*4882a593Smuzhiyun if (ceph_security_xattr_wanted(dir))
1733*4882a593Smuzhiyun mask |= CEPH_CAP_XATTR_SHARED;
1734*4882a593Smuzhiyun req->r_args.getattr.mask = cpu_to_le32(mask);
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun err = ceph_mdsc_do_request(mdsc, NULL, req);
1737*4882a593Smuzhiyun switch (err) {
1738*4882a593Smuzhiyun case 0:
1739*4882a593Smuzhiyun if (d_really_is_positive(dentry) &&
1740*4882a593Smuzhiyun d_inode(dentry) == req->r_target_inode)
1741*4882a593Smuzhiyun valid = 1;
1742*4882a593Smuzhiyun break;
1743*4882a593Smuzhiyun case -ENOENT:
1744*4882a593Smuzhiyun if (d_really_is_negative(dentry))
1745*4882a593Smuzhiyun valid = 1;
1746*4882a593Smuzhiyun fallthrough;
1747*4882a593Smuzhiyun default:
1748*4882a593Smuzhiyun break;
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun ceph_mdsc_put_request(req);
1751*4882a593Smuzhiyun dout("d_revalidate %p lookup result=%d\n",
1752*4882a593Smuzhiyun dentry, err);
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun } else {
1755*4882a593Smuzhiyun percpu_counter_inc(&mdsc->metric.d_lease_hit);
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun
1758*4882a593Smuzhiyun dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid");
1759*4882a593Smuzhiyun if (!valid)
1760*4882a593Smuzhiyun ceph_dir_clear_complete(dir);
1761*4882a593Smuzhiyun
1762*4882a593Smuzhiyun if (!(flags & LOOKUP_RCU))
1763*4882a593Smuzhiyun dput(parent);
1764*4882a593Smuzhiyun return valid;
1765*4882a593Smuzhiyun }
1766*4882a593Smuzhiyun
1767*4882a593Smuzhiyun /*
1768*4882a593Smuzhiyun * Delete unused dentry that doesn't have valid lease
1769*4882a593Smuzhiyun *
1770*4882a593Smuzhiyun * Called under dentry->d_lock.
1771*4882a593Smuzhiyun */
ceph_d_delete(const struct dentry * dentry)1772*4882a593Smuzhiyun static int ceph_d_delete(const struct dentry *dentry)
1773*4882a593Smuzhiyun {
1774*4882a593Smuzhiyun struct ceph_dentry_info *di;
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun /* won't release caps */
1777*4882a593Smuzhiyun if (d_really_is_negative(dentry))
1778*4882a593Smuzhiyun return 0;
1779*4882a593Smuzhiyun if (ceph_snap(d_inode(dentry)) != CEPH_NOSNAP)
1780*4882a593Smuzhiyun return 0;
1781*4882a593Smuzhiyun /* vaild lease? */
1782*4882a593Smuzhiyun di = ceph_dentry(dentry);
1783*4882a593Smuzhiyun if (di) {
1784*4882a593Smuzhiyun if (__dentry_lease_is_valid(di))
1785*4882a593Smuzhiyun return 0;
1786*4882a593Smuzhiyun if (__dir_lease_try_check(dentry))
1787*4882a593Smuzhiyun return 0;
1788*4882a593Smuzhiyun }
1789*4882a593Smuzhiyun return 1;
1790*4882a593Smuzhiyun }
1791*4882a593Smuzhiyun
1792*4882a593Smuzhiyun /*
1793*4882a593Smuzhiyun * Release our ceph_dentry_info.
1794*4882a593Smuzhiyun */
ceph_d_release(struct dentry * dentry)1795*4882a593Smuzhiyun static void ceph_d_release(struct dentry *dentry)
1796*4882a593Smuzhiyun {
1797*4882a593Smuzhiyun struct ceph_dentry_info *di = ceph_dentry(dentry);
1798*4882a593Smuzhiyun struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb);
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun dout("d_release %p\n", dentry);
1801*4882a593Smuzhiyun
1802*4882a593Smuzhiyun atomic64_dec(&fsc->mdsc->metric.total_dentries);
1803*4882a593Smuzhiyun
1804*4882a593Smuzhiyun spin_lock(&dentry->d_lock);
1805*4882a593Smuzhiyun __dentry_lease_unlist(di);
1806*4882a593Smuzhiyun dentry->d_fsdata = NULL;
1807*4882a593Smuzhiyun spin_unlock(&dentry->d_lock);
1808*4882a593Smuzhiyun
1809*4882a593Smuzhiyun ceph_put_mds_session(di->lease_session);
1810*4882a593Smuzhiyun kmem_cache_free(ceph_dentry_cachep, di);
1811*4882a593Smuzhiyun }
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun /*
1814*4882a593Smuzhiyun * When the VFS prunes a dentry from the cache, we need to clear the
1815*4882a593Smuzhiyun * complete flag on the parent directory.
1816*4882a593Smuzhiyun *
1817*4882a593Smuzhiyun * Called under dentry->d_lock.
1818*4882a593Smuzhiyun */
ceph_d_prune(struct dentry * dentry)1819*4882a593Smuzhiyun static void ceph_d_prune(struct dentry *dentry)
1820*4882a593Smuzhiyun {
1821*4882a593Smuzhiyun struct ceph_inode_info *dir_ci;
1822*4882a593Smuzhiyun struct ceph_dentry_info *di;
1823*4882a593Smuzhiyun
1824*4882a593Smuzhiyun dout("ceph_d_prune %pd %p\n", dentry, dentry);
1825*4882a593Smuzhiyun
1826*4882a593Smuzhiyun /* do we have a valid parent? */
1827*4882a593Smuzhiyun if (IS_ROOT(dentry))
1828*4882a593Smuzhiyun return;
1829*4882a593Smuzhiyun
1830*4882a593Smuzhiyun /* we hold d_lock, so d_parent is stable */
1831*4882a593Smuzhiyun dir_ci = ceph_inode(d_inode(dentry->d_parent));
1832*4882a593Smuzhiyun if (dir_ci->i_vino.snap == CEPH_SNAPDIR)
1833*4882a593Smuzhiyun return;
1834*4882a593Smuzhiyun
1835*4882a593Smuzhiyun /* who calls d_delete() should also disable dcache readdir */
1836*4882a593Smuzhiyun if (d_really_is_negative(dentry))
1837*4882a593Smuzhiyun return;
1838*4882a593Smuzhiyun
1839*4882a593Smuzhiyun /* d_fsdata does not get cleared until d_release */
1840*4882a593Smuzhiyun if (!d_unhashed(dentry)) {
1841*4882a593Smuzhiyun __ceph_dir_clear_complete(dir_ci);
1842*4882a593Smuzhiyun return;
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun
1845*4882a593Smuzhiyun /* Disable dcache readdir just in case that someone called d_drop()
1846*4882a593Smuzhiyun * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED
1847*4882a593Smuzhiyun * properly (dcache readdir is still enabled) */
1848*4882a593Smuzhiyun di = ceph_dentry(dentry);
1849*4882a593Smuzhiyun if (di->offset > 0 &&
1850*4882a593Smuzhiyun di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen))
1851*4882a593Smuzhiyun __ceph_dir_clear_ordered(dir_ci);
1852*4882a593Smuzhiyun }
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun /*
1855*4882a593Smuzhiyun * read() on a dir. This weird interface hack only works if mounted
1856*4882a593Smuzhiyun * with '-o dirstat'.
1857*4882a593Smuzhiyun */
ceph_read_dir(struct file * file,char __user * buf,size_t size,loff_t * ppos)1858*4882a593Smuzhiyun static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size,
1859*4882a593Smuzhiyun loff_t *ppos)
1860*4882a593Smuzhiyun {
1861*4882a593Smuzhiyun struct ceph_dir_file_info *dfi = file->private_data;
1862*4882a593Smuzhiyun struct inode *inode = file_inode(file);
1863*4882a593Smuzhiyun struct ceph_inode_info *ci = ceph_inode(inode);
1864*4882a593Smuzhiyun int left;
1865*4882a593Smuzhiyun const int bufsize = 1024;
1866*4882a593Smuzhiyun
1867*4882a593Smuzhiyun if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT))
1868*4882a593Smuzhiyun return -EISDIR;
1869*4882a593Smuzhiyun
1870*4882a593Smuzhiyun if (!dfi->dir_info) {
1871*4882a593Smuzhiyun dfi->dir_info = kmalloc(bufsize, GFP_KERNEL);
1872*4882a593Smuzhiyun if (!dfi->dir_info)
1873*4882a593Smuzhiyun return -ENOMEM;
1874*4882a593Smuzhiyun dfi->dir_info_len =
1875*4882a593Smuzhiyun snprintf(dfi->dir_info, bufsize,
1876*4882a593Smuzhiyun "entries: %20lld\n"
1877*4882a593Smuzhiyun " files: %20lld\n"
1878*4882a593Smuzhiyun " subdirs: %20lld\n"
1879*4882a593Smuzhiyun "rentries: %20lld\n"
1880*4882a593Smuzhiyun " rfiles: %20lld\n"
1881*4882a593Smuzhiyun " rsubdirs: %20lld\n"
1882*4882a593Smuzhiyun "rbytes: %20lld\n"
1883*4882a593Smuzhiyun "rctime: %10lld.%09ld\n",
1884*4882a593Smuzhiyun ci->i_files + ci->i_subdirs,
1885*4882a593Smuzhiyun ci->i_files,
1886*4882a593Smuzhiyun ci->i_subdirs,
1887*4882a593Smuzhiyun ci->i_rfiles + ci->i_rsubdirs,
1888*4882a593Smuzhiyun ci->i_rfiles,
1889*4882a593Smuzhiyun ci->i_rsubdirs,
1890*4882a593Smuzhiyun ci->i_rbytes,
1891*4882a593Smuzhiyun ci->i_rctime.tv_sec,
1892*4882a593Smuzhiyun ci->i_rctime.tv_nsec);
1893*4882a593Smuzhiyun }
1894*4882a593Smuzhiyun
1895*4882a593Smuzhiyun if (*ppos >= dfi->dir_info_len)
1896*4882a593Smuzhiyun return 0;
1897*4882a593Smuzhiyun size = min_t(unsigned, size, dfi->dir_info_len-*ppos);
1898*4882a593Smuzhiyun left = copy_to_user(buf, dfi->dir_info + *ppos, size);
1899*4882a593Smuzhiyun if (left == size)
1900*4882a593Smuzhiyun return -EFAULT;
1901*4882a593Smuzhiyun *ppos += (size - left);
1902*4882a593Smuzhiyun return size - left;
1903*4882a593Smuzhiyun }
1904*4882a593Smuzhiyun
1905*4882a593Smuzhiyun
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun /*
1908*4882a593Smuzhiyun * Return name hash for a given dentry. This is dependent on
1909*4882a593Smuzhiyun * the parent directory's hash function.
1910*4882a593Smuzhiyun */
ceph_dentry_hash(struct inode * dir,struct dentry * dn)1911*4882a593Smuzhiyun unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn)
1912*4882a593Smuzhiyun {
1913*4882a593Smuzhiyun struct ceph_inode_info *dci = ceph_inode(dir);
1914*4882a593Smuzhiyun unsigned hash;
1915*4882a593Smuzhiyun
1916*4882a593Smuzhiyun switch (dci->i_dir_layout.dl_dir_hash) {
1917*4882a593Smuzhiyun case 0: /* for backward compat */
1918*4882a593Smuzhiyun case CEPH_STR_HASH_LINUX:
1919*4882a593Smuzhiyun return dn->d_name.hash;
1920*4882a593Smuzhiyun
1921*4882a593Smuzhiyun default:
1922*4882a593Smuzhiyun spin_lock(&dn->d_lock);
1923*4882a593Smuzhiyun hash = ceph_str_hash(dci->i_dir_layout.dl_dir_hash,
1924*4882a593Smuzhiyun dn->d_name.name, dn->d_name.len);
1925*4882a593Smuzhiyun spin_unlock(&dn->d_lock);
1926*4882a593Smuzhiyun return hash;
1927*4882a593Smuzhiyun }
1928*4882a593Smuzhiyun }
1929*4882a593Smuzhiyun
1930*4882a593Smuzhiyun const struct file_operations ceph_dir_fops = {
1931*4882a593Smuzhiyun .read = ceph_read_dir,
1932*4882a593Smuzhiyun .iterate = ceph_readdir,
1933*4882a593Smuzhiyun .llseek = ceph_dir_llseek,
1934*4882a593Smuzhiyun .open = ceph_open,
1935*4882a593Smuzhiyun .release = ceph_release,
1936*4882a593Smuzhiyun .unlocked_ioctl = ceph_ioctl,
1937*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
1938*4882a593Smuzhiyun .fsync = ceph_fsync,
1939*4882a593Smuzhiyun .lock = ceph_lock,
1940*4882a593Smuzhiyun .flock = ceph_flock,
1941*4882a593Smuzhiyun };
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun const struct file_operations ceph_snapdir_fops = {
1944*4882a593Smuzhiyun .iterate = ceph_readdir,
1945*4882a593Smuzhiyun .llseek = ceph_dir_llseek,
1946*4882a593Smuzhiyun .open = ceph_open,
1947*4882a593Smuzhiyun .release = ceph_release,
1948*4882a593Smuzhiyun };
1949*4882a593Smuzhiyun
1950*4882a593Smuzhiyun const struct inode_operations ceph_dir_iops = {
1951*4882a593Smuzhiyun .lookup = ceph_lookup,
1952*4882a593Smuzhiyun .permission = ceph_permission,
1953*4882a593Smuzhiyun .getattr = ceph_getattr,
1954*4882a593Smuzhiyun .setattr = ceph_setattr,
1955*4882a593Smuzhiyun .listxattr = ceph_listxattr,
1956*4882a593Smuzhiyun .get_acl = ceph_get_acl,
1957*4882a593Smuzhiyun .set_acl = ceph_set_acl,
1958*4882a593Smuzhiyun .mknod = ceph_mknod,
1959*4882a593Smuzhiyun .symlink = ceph_symlink,
1960*4882a593Smuzhiyun .mkdir = ceph_mkdir,
1961*4882a593Smuzhiyun .link = ceph_link,
1962*4882a593Smuzhiyun .unlink = ceph_unlink,
1963*4882a593Smuzhiyun .rmdir = ceph_unlink,
1964*4882a593Smuzhiyun .rename = ceph_rename,
1965*4882a593Smuzhiyun .create = ceph_create,
1966*4882a593Smuzhiyun .atomic_open = ceph_atomic_open,
1967*4882a593Smuzhiyun };
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun const struct inode_operations ceph_snapdir_iops = {
1970*4882a593Smuzhiyun .lookup = ceph_lookup,
1971*4882a593Smuzhiyun .permission = ceph_permission,
1972*4882a593Smuzhiyun .getattr = ceph_getattr,
1973*4882a593Smuzhiyun .mkdir = ceph_mkdir,
1974*4882a593Smuzhiyun .rmdir = ceph_unlink,
1975*4882a593Smuzhiyun .rename = ceph_rename,
1976*4882a593Smuzhiyun };
1977*4882a593Smuzhiyun
1978*4882a593Smuzhiyun const struct dentry_operations ceph_dentry_ops = {
1979*4882a593Smuzhiyun .d_revalidate = ceph_d_revalidate,
1980*4882a593Smuzhiyun .d_delete = ceph_d_delete,
1981*4882a593Smuzhiyun .d_release = ceph_d_release,
1982*4882a593Smuzhiyun .d_prune = ceph_d_prune,
1983*4882a593Smuzhiyun .d_init = ceph_d_init,
1984*4882a593Smuzhiyun };
1985