1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
3355da1ebSSage Weil
4355da1ebSSage Weil #include <linux/module.h>
5355da1ebSSage Weil #include <linux/fs.h>
6355da1ebSSage Weil #include <linux/slab.h>
7355da1ebSSage Weil #include <linux/string.h>
8355da1ebSSage Weil #include <linux/uaccess.h>
9355da1ebSSage Weil #include <linux/kernel.h>
10355da1ebSSage Weil #include <linux/writeback.h>
11355da1ebSSage Weil #include <linux/vmalloc.h>
122cdeb1e4SAndreas Gruenbacher #include <linux/xattr.h>
134db658eaSLinus Torvalds #include <linux/posix_acl.h>
143e7fbe9cSYan, Zheng #include <linux/random.h>
15a407846eSYan, Zheng #include <linux/sort.h>
16a35ead31SJeff Layton #include <linux/iversion.h>
172d332d5bSJeff Layton #include <linux/fscrypt.h>
18355da1ebSSage Weil
19355da1ebSSage Weil #include "super.h"
203d14c5d2SYehuda Sadeh #include "mds_client.h"
2199ccbd22SMilosz Tanski #include "cache.h"
222d332d5bSJeff Layton #include "crypto.h"
233d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
24355da1ebSSage Weil
25355da1ebSSage Weil /*
26355da1ebSSage Weil * Ceph inode operations
27355da1ebSSage Weil *
28355da1ebSSage Weil * Implement basic inode helpers (get, alloc) and inode ops (getattr,
29355da1ebSSage Weil * setattr, etc.), xattr helpers, and helpers for assimilating
30355da1ebSSage Weil * metadata returned by the MDS into our cache.
31355da1ebSSage Weil *
32355da1ebSSage Weil * Also define helpers for doing asynchronous writeback, invalidation,
33355da1ebSSage Weil * and truncation for the benefit of those who can't afford to block
34355da1ebSSage Weil * (typically because they are in the message handler path).
35355da1ebSSage Weil */
36355da1ebSSage Weil
37355da1ebSSage Weil static const struct inode_operations ceph_symlink_iops;
3879f2f6adSJeff Layton static const struct inode_operations ceph_encrypted_symlink_iops;
39355da1ebSSage Weil
401cf89a8dSYan, Zheng static void ceph_inode_work(struct work_struct *work);
41355da1ebSSage Weil
42355da1ebSSage Weil /*
43355da1ebSSage Weil * find or create an inode, given the ceph ino number
44355da1ebSSage Weil */
ceph_set_ino_cb(struct inode * inode,void * data)45ad1fee96SYehuda Sadeh static int ceph_set_ino_cb(struct inode *inode, void *data)
46ad1fee96SYehuda Sadeh {
47ebce3eb2SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
481dd8d470SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
49ebce3eb2SJeff Layton
50ebce3eb2SJeff Layton ci->i_vino = *(struct ceph_vino *)data;
51ebce3eb2SJeff Layton inode->i_ino = ceph_vino_to_ino_t(ci->i_vino);
52a35ead31SJeff Layton inode_set_iversion_raw(inode, 0);
531dd8d470SXiubo Li percpu_counter_inc(&mdsc->metric.total_inodes);
541dd8d470SXiubo Li
55ad1fee96SYehuda Sadeh return 0;
56ad1fee96SYehuda Sadeh }
57ad1fee96SYehuda Sadeh
58ec9595c0SJeff Layton /**
59ec9595c0SJeff Layton * ceph_new_inode - allocate a new inode in advance of an expected create
60ec9595c0SJeff Layton * @dir: parent directory for new inode
61ec9595c0SJeff Layton * @dentry: dentry that may eventually point to new inode
62ec9595c0SJeff Layton * @mode: mode of new inode
63ec9595c0SJeff Layton * @as_ctx: pointer to inherited security context
64ec9595c0SJeff Layton *
65ec9595c0SJeff Layton * Allocate a new inode in advance of an operation to create a new inode.
66ec9595c0SJeff Layton * This allocates the inode and sets up the acl_sec_ctx with appropriate
67ec9595c0SJeff Layton * info for the new inode.
68ec9595c0SJeff Layton *
69ec9595c0SJeff Layton * Returns a pointer to the new inode or an ERR_PTR.
70ec9595c0SJeff Layton */
ceph_new_inode(struct inode * dir,struct dentry * dentry,umode_t * mode,struct ceph_acl_sec_ctx * as_ctx)71ec9595c0SJeff Layton struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,
72ec9595c0SJeff Layton umode_t *mode, struct ceph_acl_sec_ctx *as_ctx)
73ec9595c0SJeff Layton {
74ec9595c0SJeff Layton int err;
75ec9595c0SJeff Layton struct inode *inode;
76ec9595c0SJeff Layton
77ec9595c0SJeff Layton inode = new_inode(dir->i_sb);
78ec9595c0SJeff Layton if (!inode)
79ec9595c0SJeff Layton return ERR_PTR(-ENOMEM);
80ec9595c0SJeff Layton
81ec9595c0SJeff Layton if (!S_ISLNK(*mode)) {
82ec9595c0SJeff Layton err = ceph_pre_init_acls(dir, mode, as_ctx);
83ec9595c0SJeff Layton if (err < 0)
84ec9595c0SJeff Layton goto out_err;
85ec9595c0SJeff Layton }
86ec9595c0SJeff Layton
876b5717bdSJeff Layton inode->i_state = 0;
886b5717bdSJeff Layton inode->i_mode = *mode;
896b5717bdSJeff Layton
90ec9595c0SJeff Layton err = ceph_security_init_secctx(dentry, *mode, as_ctx);
91ec9595c0SJeff Layton if (err < 0)
92ec9595c0SJeff Layton goto out_err;
93ec9595c0SJeff Layton
94dd66df00SLuís Henriques /*
95dd66df00SLuís Henriques * We'll skip setting fscrypt context for snapshots, leaving that for
96dd66df00SLuís Henriques * the handle_reply().
97dd66df00SLuís Henriques */
98dd66df00SLuís Henriques if (ceph_snap(dir) != CEPH_SNAPDIR) {
996b5717bdSJeff Layton err = ceph_fscrypt_prepare_context(dir, inode, as_ctx);
1006b5717bdSJeff Layton if (err)
1016b5717bdSJeff Layton goto out_err;
102dd66df00SLuís Henriques }
1036b5717bdSJeff Layton
104ec9595c0SJeff Layton return inode;
105ec9595c0SJeff Layton out_err:
106ec9595c0SJeff Layton iput(inode);
107ec9595c0SJeff Layton return ERR_PTR(err);
108ec9595c0SJeff Layton }
109ec9595c0SJeff Layton
ceph_as_ctx_to_req(struct ceph_mds_request * req,struct ceph_acl_sec_ctx * as_ctx)110ec9595c0SJeff Layton void ceph_as_ctx_to_req(struct ceph_mds_request *req,
111ec9595c0SJeff Layton struct ceph_acl_sec_ctx *as_ctx)
112ec9595c0SJeff Layton {
113ec9595c0SJeff Layton if (as_ctx->pagelist) {
114ec9595c0SJeff Layton req->r_pagelist = as_ctx->pagelist;
115ec9595c0SJeff Layton as_ctx->pagelist = NULL;
116ec9595c0SJeff Layton }
1176b5717bdSJeff Layton ceph_fscrypt_as_ctx_to_req(req, as_ctx);
118ec9595c0SJeff Layton }
119ec9595c0SJeff Layton
120ec9595c0SJeff Layton /**
121ec9595c0SJeff Layton * ceph_get_inode - find or create/hash a new inode
122ec9595c0SJeff Layton * @sb: superblock to search and allocate in
123ec9595c0SJeff Layton * @vino: vino to search for
124ec9595c0SJeff Layton * @newino: optional new inode to insert if one isn't found (may be NULL)
125ec9595c0SJeff Layton *
126ec9595c0SJeff Layton * Search for or insert a new inode into the hash for the given vino, and
127ec9595c0SJeff Layton * return a reference to it. If new is non-NULL, its reference is consumed.
128ec9595c0SJeff Layton */
ceph_get_inode(struct super_block * sb,struct ceph_vino vino,struct inode * newino)129ec9595c0SJeff Layton struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino,
130ec9595c0SJeff Layton struct inode *newino)
131355da1ebSSage Weil {
132355da1ebSSage Weil struct inode *inode;
133355da1ebSSage Weil
134d4f6b31dSJeff Layton if (ceph_vino_is_reserved(vino))
135d4f6b31dSJeff Layton return ERR_PTR(-EREMOTEIO);
136d4f6b31dSJeff Layton
137ec9595c0SJeff Layton if (newino) {
138ec9595c0SJeff Layton inode = inode_insert5(newino, (unsigned long)vino.ino,
139ec9595c0SJeff Layton ceph_ino_compare, ceph_set_ino_cb, &vino);
140ec9595c0SJeff Layton if (inode != newino)
141ec9595c0SJeff Layton iput(newino);
142ec9595c0SJeff Layton } else {
143ec9595c0SJeff Layton inode = iget5_locked(sb, (unsigned long)vino.ino,
144ec9595c0SJeff Layton ceph_ino_compare, ceph_set_ino_cb, &vino);
145ec9595c0SJeff Layton }
146ec9595c0SJeff Layton
147ec9595c0SJeff Layton if (!inode) {
148ec9595c0SJeff Layton dout("No inode found for %llx.%llx\n", vino.ino, vino.snap);
149355da1ebSSage Weil return ERR_PTR(-ENOMEM);
150ec9595c0SJeff Layton }
151355da1ebSSage Weil
152ebce3eb2SJeff Layton dout("get_inode on %llu=%llx.%llx got %p new %d\n", ceph_present_inode(inode),
153ebce3eb2SJeff Layton ceph_vinop(inode), inode, !!(inode->i_state & I_NEW));
154355da1ebSSage Weil return inode;
155355da1ebSSage Weil }
156355da1ebSSage Weil
157355da1ebSSage Weil /*
158355da1ebSSage Weil * get/constuct snapdir inode for a given directory
159355da1ebSSage Weil */
ceph_get_snapdir(struct inode * parent)160355da1ebSSage Weil struct inode *ceph_get_snapdir(struct inode *parent)
161355da1ebSSage Weil {
162355da1ebSSage Weil struct ceph_vino vino = {
163355da1ebSSage Weil .ino = ceph_ino(parent),
164355da1ebSSage Weil .snap = CEPH_SNAPDIR,
165355da1ebSSage Weil };
166ec9595c0SJeff Layton struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL);
167b377ff13SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
168dd66df00SLuís Henriques int ret = -ENOTDIR;
169355da1ebSSage Weil
170355da1ebSSage Weil if (IS_ERR(inode))
1717e34bc52SJulia Lawall return inode;
1723e10a15fSJeff Layton
1733e10a15fSJeff Layton if (!S_ISDIR(parent->i_mode)) {
1743e10a15fSJeff Layton pr_warn_once("bad snapdir parent type (mode=0%o)\n",
1753e10a15fSJeff Layton parent->i_mode);
176322794d3SXiubo Li goto err;
1773e10a15fSJeff Layton }
1783e10a15fSJeff Layton
1793e10a15fSJeff Layton if (!(inode->i_state & I_NEW) && !S_ISDIR(inode->i_mode)) {
1803e10a15fSJeff Layton pr_warn_once("bad snapdir inode type (mode=0%o)\n",
1813e10a15fSJeff Layton inode->i_mode);
182322794d3SXiubo Li goto err;
1833e10a15fSJeff Layton }
1843e10a15fSJeff Layton
185355da1ebSSage Weil inode->i_mode = parent->i_mode;
186355da1ebSSage Weil inode->i_uid = parent->i_uid;
187355da1ebSSage Weil inode->i_gid = parent->i_gid;
188ef915725SLuis Henriques inode->i_mtime = parent->i_mtime;
1897795aef0SJeff Layton inode_set_ctime_to_ts(inode, inode_get_ctime(parent));
190ef915725SLuis Henriques inode->i_atime = parent->i_atime;
191b377ff13SSage Weil ci->i_rbytes = 0;
192ef915725SLuis Henriques ci->i_btime = ceph_inode(parent)->i_btime;
193893e456bSJeff Layton
194dd66df00SLuís Henriques #ifdef CONFIG_FS_ENCRYPTION
195dd66df00SLuís Henriques /* if encrypted, just borrow fscrypt_auth from parent */
196dd66df00SLuís Henriques if (IS_ENCRYPTED(parent)) {
197dd66df00SLuís Henriques struct ceph_inode_info *pci = ceph_inode(parent);
198dd66df00SLuís Henriques
199dd66df00SLuís Henriques ci->fscrypt_auth = kmemdup(pci->fscrypt_auth,
200dd66df00SLuís Henriques pci->fscrypt_auth_len,
201dd66df00SLuís Henriques GFP_KERNEL);
202dd66df00SLuís Henriques if (ci->fscrypt_auth) {
203dd66df00SLuís Henriques inode->i_flags |= S_ENCRYPTED;
204dd66df00SLuís Henriques ci->fscrypt_auth_len = pci->fscrypt_auth_len;
205dd66df00SLuís Henriques } else {
206dd66df00SLuís Henriques dout("Failed to alloc snapdir fscrypt_auth\n");
207dd66df00SLuís Henriques ret = -ENOMEM;
208dd66df00SLuís Henriques goto err;
209dd66df00SLuís Henriques }
210dd66df00SLuís Henriques }
211dd66df00SLuís Henriques #endif
212d3c51ae1SJeff Layton if (inode->i_state & I_NEW) {
213d3c51ae1SJeff Layton inode->i_op = &ceph_snapdir_iops;
214d3c51ae1SJeff Layton inode->i_fop = &ceph_snapdir_fops;
215d3c51ae1SJeff Layton ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
216893e456bSJeff Layton unlock_new_inode(inode);
217d3c51ae1SJeff Layton }
218893e456bSJeff Layton
219355da1ebSSage Weil return inode;
220322794d3SXiubo Li err:
221322794d3SXiubo Li if ((inode->i_state & I_NEW))
222322794d3SXiubo Li discard_new_inode(inode);
223322794d3SXiubo Li else
224322794d3SXiubo Li iput(inode);
225dd66df00SLuís Henriques return ERR_PTR(ret);
226355da1ebSSage Weil }
227355da1ebSSage Weil
228355da1ebSSage Weil const struct inode_operations ceph_file_iops = {
229355da1ebSSage Weil .permission = ceph_permission,
230355da1ebSSage Weil .setattr = ceph_setattr,
231355da1ebSSage Weil .getattr = ceph_getattr,
232355da1ebSSage Weil .listxattr = ceph_listxattr,
233cac2f8b8SChristian Brauner .get_inode_acl = ceph_get_acl,
23472466d0bSSage Weil .set_acl = ceph_set_acl,
235355da1ebSSage Weil };
236355da1ebSSage Weil
237355da1ebSSage Weil
238355da1ebSSage Weil /*
239355da1ebSSage Weil * We use a 'frag tree' to keep track of the MDS's directory fragments
240355da1ebSSage Weil * for a given inode (usually there is just a single fragment). We
241355da1ebSSage Weil * need to know when a child frag is delegated to a new MDS, or when
242355da1ebSSage Weil * it is flagged as replicated, so we can direct our requests
243355da1ebSSage Weil * accordingly.
244355da1ebSSage Weil */
245355da1ebSSage Weil
246355da1ebSSage Weil /*
247355da1ebSSage Weil * find/create a frag in the tree
248355da1ebSSage Weil */
__get_or_create_frag(struct ceph_inode_info * ci,u32 f)249355da1ebSSage Weil static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
250355da1ebSSage Weil u32 f)
251355da1ebSSage Weil {
252355da1ebSSage Weil struct rb_node **p;
253355da1ebSSage Weil struct rb_node *parent = NULL;
254355da1ebSSage Weil struct ceph_inode_frag *frag;
255355da1ebSSage Weil int c;
256355da1ebSSage Weil
257355da1ebSSage Weil p = &ci->i_fragtree.rb_node;
258355da1ebSSage Weil while (*p) {
259355da1ebSSage Weil parent = *p;
260355da1ebSSage Weil frag = rb_entry(parent, struct ceph_inode_frag, node);
261355da1ebSSage Weil c = ceph_frag_compare(f, frag->frag);
262355da1ebSSage Weil if (c < 0)
263355da1ebSSage Weil p = &(*p)->rb_left;
264355da1ebSSage Weil else if (c > 0)
265355da1ebSSage Weil p = &(*p)->rb_right;
266355da1ebSSage Weil else
267355da1ebSSage Weil return frag;
268355da1ebSSage Weil }
269355da1ebSSage Weil
270355da1ebSSage Weil frag = kmalloc(sizeof(*frag), GFP_NOFS);
27151308806SMarkus Elfring if (!frag)
272355da1ebSSage Weil return ERR_PTR(-ENOMEM);
27351308806SMarkus Elfring
274355da1ebSSage Weil frag->frag = f;
275355da1ebSSage Weil frag->split_by = 0;
276355da1ebSSage Weil frag->mds = -1;
277355da1ebSSage Weil frag->ndist = 0;
278355da1ebSSage Weil
279355da1ebSSage Weil rb_link_node(&frag->node, parent, p);
280355da1ebSSage Weil rb_insert_color(&frag->node, &ci->i_fragtree);
281355da1ebSSage Weil
282355da1ebSSage Weil dout("get_or_create_frag added %llx.%llx frag %x\n",
283874c8ca1SDavid Howells ceph_vinop(&ci->netfs.inode), f);
284355da1ebSSage Weil return frag;
285355da1ebSSage Weil }
286355da1ebSSage Weil
287355da1ebSSage Weil /*
288355da1ebSSage Weil * find a specific frag @f
289355da1ebSSage Weil */
__ceph_find_frag(struct ceph_inode_info * ci,u32 f)290355da1ebSSage Weil struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
291355da1ebSSage Weil {
292355da1ebSSage Weil struct rb_node *n = ci->i_fragtree.rb_node;
293355da1ebSSage Weil
294355da1ebSSage Weil while (n) {
295355da1ebSSage Weil struct ceph_inode_frag *frag =
296355da1ebSSage Weil rb_entry(n, struct ceph_inode_frag, node);
297355da1ebSSage Weil int c = ceph_frag_compare(f, frag->frag);
298355da1ebSSage Weil if (c < 0)
299355da1ebSSage Weil n = n->rb_left;
300355da1ebSSage Weil else if (c > 0)
301355da1ebSSage Weil n = n->rb_right;
302355da1ebSSage Weil else
303355da1ebSSage Weil return frag;
304355da1ebSSage Weil }
305355da1ebSSage Weil return NULL;
306355da1ebSSage Weil }
307355da1ebSSage Weil
308355da1ebSSage Weil /*
309355da1ebSSage Weil * Choose frag containing the given value @v. If @pfrag is
310355da1ebSSage Weil * specified, copy the frag delegation info to the caller if
311355da1ebSSage Weil * it is present.
312355da1ebSSage Weil */
__ceph_choose_frag(struct ceph_inode_info * ci,u32 v,struct ceph_inode_frag * pfrag,int * found)3133e7fbe9cSYan, Zheng static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
3143e7fbe9cSYan, Zheng struct ceph_inode_frag *pfrag, int *found)
315355da1ebSSage Weil {
316355da1ebSSage Weil u32 t = ceph_frag_make(0, 0);
317355da1ebSSage Weil struct ceph_inode_frag *frag;
318355da1ebSSage Weil unsigned nway, i;
319355da1ebSSage Weil u32 n;
320355da1ebSSage Weil
321355da1ebSSage Weil if (found)
322355da1ebSSage Weil *found = 0;
323355da1ebSSage Weil
324355da1ebSSage Weil while (1) {
325355da1ebSSage Weil WARN_ON(!ceph_frag_contains_value(t, v));
326355da1ebSSage Weil frag = __ceph_find_frag(ci, t);
327355da1ebSSage Weil if (!frag)
328355da1ebSSage Weil break; /* t is a leaf */
329355da1ebSSage Weil if (frag->split_by == 0) {
330355da1ebSSage Weil if (pfrag)
331355da1ebSSage Weil memcpy(pfrag, frag, sizeof(*pfrag));
332355da1ebSSage Weil if (found)
333355da1ebSSage Weil *found = 1;
334355da1ebSSage Weil break;
335355da1ebSSage Weil }
336355da1ebSSage Weil
337355da1ebSSage Weil /* choose child */
338355da1ebSSage Weil nway = 1 << frag->split_by;
339355da1ebSSage Weil dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t,
340355da1ebSSage Weil frag->split_by, nway);
341355da1ebSSage Weil for (i = 0; i < nway; i++) {
342355da1ebSSage Weil n = ceph_frag_make_child(t, frag->split_by, i);
343355da1ebSSage Weil if (ceph_frag_contains_value(n, v)) {
344355da1ebSSage Weil t = n;
345355da1ebSSage Weil break;
346355da1ebSSage Weil }
347355da1ebSSage Weil }
348355da1ebSSage Weil BUG_ON(i == nway);
349355da1ebSSage Weil }
350355da1ebSSage Weil dout("choose_frag(%x) = %x\n", v, t);
351355da1ebSSage Weil
352355da1ebSSage Weil return t;
353355da1ebSSage Weil }
354355da1ebSSage Weil
ceph_choose_frag(struct ceph_inode_info * ci,u32 v,struct ceph_inode_frag * pfrag,int * found)3553e7fbe9cSYan, Zheng u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
3563e7fbe9cSYan, Zheng struct ceph_inode_frag *pfrag, int *found)
3573e7fbe9cSYan, Zheng {
3583e7fbe9cSYan, Zheng u32 ret;
3593e7fbe9cSYan, Zheng mutex_lock(&ci->i_fragtree_mutex);
3603e7fbe9cSYan, Zheng ret = __ceph_choose_frag(ci, v, pfrag, found);
3613e7fbe9cSYan, Zheng mutex_unlock(&ci->i_fragtree_mutex);
3623e7fbe9cSYan, Zheng return ret;
3633e7fbe9cSYan, Zheng }
3643e7fbe9cSYan, Zheng
365355da1ebSSage Weil /*
366355da1ebSSage Weil * Process dirfrag (delegation) info from the mds. Include leaf
367355da1ebSSage Weil * fragment in tree ONLY if ndist > 0. Otherwise, only
368355da1ebSSage Weil * branches/splits are included in i_fragtree)
369355da1ebSSage Weil */
ceph_fill_dirfrag(struct inode * inode,struct ceph_mds_reply_dirfrag * dirinfo)370355da1ebSSage Weil static int ceph_fill_dirfrag(struct inode *inode,
371355da1ebSSage Weil struct ceph_mds_reply_dirfrag *dirinfo)
372355da1ebSSage Weil {
373355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
374355da1ebSSage Weil struct ceph_inode_frag *frag;
375355da1ebSSage Weil u32 id = le32_to_cpu(dirinfo->frag);
376355da1ebSSage Weil int mds = le32_to_cpu(dirinfo->auth);
377355da1ebSSage Weil int ndist = le32_to_cpu(dirinfo->ndist);
3788d08503cSYan, Zheng int diri_auth = -1;
379355da1ebSSage Weil int i;
380355da1ebSSage Weil int err = 0;
381355da1ebSSage Weil
3828d08503cSYan, Zheng spin_lock(&ci->i_ceph_lock);
3838d08503cSYan, Zheng if (ci->i_auth_cap)
3848d08503cSYan, Zheng diri_auth = ci->i_auth_cap->mds;
3858d08503cSYan, Zheng spin_unlock(&ci->i_ceph_lock);
3868d08503cSYan, Zheng
38742172119SYan, Zheng if (mds == -1) /* CDIR_AUTH_PARENT */
38842172119SYan, Zheng mds = diri_auth;
38942172119SYan, Zheng
390355da1ebSSage Weil mutex_lock(&ci->i_fragtree_mutex);
3918d08503cSYan, Zheng if (ndist == 0 && mds == diri_auth) {
392355da1ebSSage Weil /* no delegation info needed. */
393355da1ebSSage Weil frag = __ceph_find_frag(ci, id);
394355da1ebSSage Weil if (!frag)
395355da1ebSSage Weil goto out;
396355da1ebSSage Weil if (frag->split_by == 0) {
397355da1ebSSage Weil /* tree leaf, remove */
398355da1ebSSage Weil dout("fill_dirfrag removed %llx.%llx frag %x"
399355da1ebSSage Weil " (no ref)\n", ceph_vinop(inode), id);
400355da1ebSSage Weil rb_erase(&frag->node, &ci->i_fragtree);
401355da1ebSSage Weil kfree(frag);
402355da1ebSSage Weil } else {
403355da1ebSSage Weil /* tree branch, keep and clear */
404355da1ebSSage Weil dout("fill_dirfrag cleared %llx.%llx frag %x"
405355da1ebSSage Weil " referral\n", ceph_vinop(inode), id);
406355da1ebSSage Weil frag->mds = -1;
407355da1ebSSage Weil frag->ndist = 0;
408355da1ebSSage Weil }
409355da1ebSSage Weil goto out;
410355da1ebSSage Weil }
411355da1ebSSage Weil
412355da1ebSSage Weil
413355da1ebSSage Weil /* find/add this frag to store mds delegation info */
414355da1ebSSage Weil frag = __get_or_create_frag(ci, id);
415355da1ebSSage Weil if (IS_ERR(frag)) {
416355da1ebSSage Weil /* this is not the end of the world; we can continue
417355da1ebSSage Weil with bad/inaccurate delegation info */
418355da1ebSSage Weil pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n",
419355da1ebSSage Weil ceph_vinop(inode), le32_to_cpu(dirinfo->frag));
420355da1ebSSage Weil err = -ENOMEM;
421355da1ebSSage Weil goto out;
422355da1ebSSage Weil }
423355da1ebSSage Weil
424355da1ebSSage Weil frag->mds = mds;
425355da1ebSSage Weil frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
426355da1ebSSage Weil for (i = 0; i < frag->ndist; i++)
427355da1ebSSage Weil frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
428355da1ebSSage Weil dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n",
429355da1ebSSage Weil ceph_vinop(inode), frag->frag, frag->ndist);
430355da1ebSSage Weil
431355da1ebSSage Weil out:
432355da1ebSSage Weil mutex_unlock(&ci->i_fragtree_mutex);
433355da1ebSSage Weil return err;
434355da1ebSSage Weil }
435355da1ebSSage Weil
frag_tree_split_cmp(const void * l,const void * r)436a407846eSYan, Zheng static int frag_tree_split_cmp(const void *l, const void *r)
437a407846eSYan, Zheng {
438a407846eSYan, Zheng struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
439a407846eSYan, Zheng struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
440fe2ed425SJeff Layton return ceph_frag_compare(le32_to_cpu(ls->frag),
441fe2ed425SJeff Layton le32_to_cpu(rs->frag));
442a407846eSYan, Zheng }
443a407846eSYan, Zheng
is_frag_child(u32 f,struct ceph_inode_frag * frag)444a4b7431fSYan, Zheng static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
445a4b7431fSYan, Zheng {
446a4b7431fSYan, Zheng if (!frag)
447a4b7431fSYan, Zheng return f == ceph_frag_make(0, 0);
448a4b7431fSYan, Zheng if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
449a4b7431fSYan, Zheng return false;
450a4b7431fSYan, Zheng return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
451a4b7431fSYan, Zheng }
452a4b7431fSYan, Zheng
ceph_fill_fragtree(struct inode * inode,struct ceph_frag_tree_head * fragtree,struct ceph_mds_reply_dirfrag * dirinfo)4533e7fbe9cSYan, Zheng static int ceph_fill_fragtree(struct inode *inode,
4543e7fbe9cSYan, Zheng struct ceph_frag_tree_head *fragtree,
4553e7fbe9cSYan, Zheng struct ceph_mds_reply_dirfrag *dirinfo)
4563e7fbe9cSYan, Zheng {
4573e7fbe9cSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
458a4b7431fSYan, Zheng struct ceph_inode_frag *frag, *prev_frag = NULL;
4593e7fbe9cSYan, Zheng struct rb_node *rb_node;
4601b1bc16dSYan, Zheng unsigned i, split_by, nsplits;
4611b1bc16dSYan, Zheng u32 id;
4623e7fbe9cSYan, Zheng bool update = false;
4633e7fbe9cSYan, Zheng
4643e7fbe9cSYan, Zheng mutex_lock(&ci->i_fragtree_mutex);
4653e7fbe9cSYan, Zheng nsplits = le32_to_cpu(fragtree->nsplits);
4661b1bc16dSYan, Zheng if (nsplits != ci->i_fragtree_nsplits) {
4671b1bc16dSYan, Zheng update = true;
4681b1bc16dSYan, Zheng } else if (nsplits) {
4698032bf12SJason A. Donenfeld i = get_random_u32_below(nsplits);
4703e7fbe9cSYan, Zheng id = le32_to_cpu(fragtree->splits[i].frag);
4713e7fbe9cSYan, Zheng if (!__ceph_find_frag(ci, id))
4723e7fbe9cSYan, Zheng update = true;
4733e7fbe9cSYan, Zheng } else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
4743e7fbe9cSYan, Zheng rb_node = rb_first(&ci->i_fragtree);
4753e7fbe9cSYan, Zheng frag = rb_entry(rb_node, struct ceph_inode_frag, node);
4763e7fbe9cSYan, Zheng if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
4773e7fbe9cSYan, Zheng update = true;
4783e7fbe9cSYan, Zheng }
4793e7fbe9cSYan, Zheng if (!update && dirinfo) {
4803e7fbe9cSYan, Zheng id = le32_to_cpu(dirinfo->frag);
4813e7fbe9cSYan, Zheng if (id != __ceph_choose_frag(ci, id, NULL, NULL))
4823e7fbe9cSYan, Zheng update = true;
4833e7fbe9cSYan, Zheng }
4843e7fbe9cSYan, Zheng if (!update)
4853e7fbe9cSYan, Zheng goto out_unlock;
4863e7fbe9cSYan, Zheng
487a407846eSYan, Zheng if (nsplits > 1) {
488a407846eSYan, Zheng sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
489a407846eSYan, Zheng frag_tree_split_cmp, NULL);
490a407846eSYan, Zheng }
491a407846eSYan, Zheng
4923e7fbe9cSYan, Zheng dout("fill_fragtree %llx.%llx\n", ceph_vinop(inode));
4933e7fbe9cSYan, Zheng rb_node = rb_first(&ci->i_fragtree);
4943e7fbe9cSYan, Zheng for (i = 0; i < nsplits; i++) {
4953e7fbe9cSYan, Zheng id = le32_to_cpu(fragtree->splits[i].frag);
4961b1bc16dSYan, Zheng split_by = le32_to_cpu(fragtree->splits[i].by);
4971b1bc16dSYan, Zheng if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
4981b1bc16dSYan, Zheng pr_err("fill_fragtree %llx.%llx invalid split %d/%u, "
4991b1bc16dSYan, Zheng "frag %x split by %d\n", ceph_vinop(inode),
5001b1bc16dSYan, Zheng i, nsplits, id, split_by);
5011b1bc16dSYan, Zheng continue;
5021b1bc16dSYan, Zheng }
5033e7fbe9cSYan, Zheng frag = NULL;
5043e7fbe9cSYan, Zheng while (rb_node) {
5053e7fbe9cSYan, Zheng frag = rb_entry(rb_node, struct ceph_inode_frag, node);
5063e7fbe9cSYan, Zheng if (ceph_frag_compare(frag->frag, id) >= 0) {
5073e7fbe9cSYan, Zheng if (frag->frag != id)
5083e7fbe9cSYan, Zheng frag = NULL;
5093e7fbe9cSYan, Zheng else
5103e7fbe9cSYan, Zheng rb_node = rb_next(rb_node);
5113e7fbe9cSYan, Zheng break;
5123e7fbe9cSYan, Zheng }
5133e7fbe9cSYan, Zheng rb_node = rb_next(rb_node);
514a4b7431fSYan, Zheng /* delete stale split/leaf node */
515a4b7431fSYan, Zheng if (frag->split_by > 0 ||
516a4b7431fSYan, Zheng !is_frag_child(frag->frag, prev_frag)) {
5173e7fbe9cSYan, Zheng rb_erase(&frag->node, &ci->i_fragtree);
5181b1bc16dSYan, Zheng if (frag->split_by > 0)
5191b1bc16dSYan, Zheng ci->i_fragtree_nsplits--;
5203e7fbe9cSYan, Zheng kfree(frag);
521a4b7431fSYan, Zheng }
5223e7fbe9cSYan, Zheng frag = NULL;
5233e7fbe9cSYan, Zheng }
5243e7fbe9cSYan, Zheng if (!frag) {
5253e7fbe9cSYan, Zheng frag = __get_or_create_frag(ci, id);
5263e7fbe9cSYan, Zheng if (IS_ERR(frag))
5273e7fbe9cSYan, Zheng continue;
5283e7fbe9cSYan, Zheng }
5291b1bc16dSYan, Zheng if (frag->split_by == 0)
5301b1bc16dSYan, Zheng ci->i_fragtree_nsplits++;
5311b1bc16dSYan, Zheng frag->split_by = split_by;
5323e7fbe9cSYan, Zheng dout(" frag %x split by %d\n", frag->frag, frag->split_by);
533a4b7431fSYan, Zheng prev_frag = frag;
5343e7fbe9cSYan, Zheng }
5353e7fbe9cSYan, Zheng while (rb_node) {
5363e7fbe9cSYan, Zheng frag = rb_entry(rb_node, struct ceph_inode_frag, node);
5373e7fbe9cSYan, Zheng rb_node = rb_next(rb_node);
538a4b7431fSYan, Zheng /* delete stale split/leaf node */
539a4b7431fSYan, Zheng if (frag->split_by > 0 ||
540a4b7431fSYan, Zheng !is_frag_child(frag->frag, prev_frag)) {
5413e7fbe9cSYan, Zheng rb_erase(&frag->node, &ci->i_fragtree);
5421b1bc16dSYan, Zheng if (frag->split_by > 0)
5431b1bc16dSYan, Zheng ci->i_fragtree_nsplits--;
5443e7fbe9cSYan, Zheng kfree(frag);
5453e7fbe9cSYan, Zheng }
546a4b7431fSYan, Zheng }
5473e7fbe9cSYan, Zheng out_unlock:
5483e7fbe9cSYan, Zheng mutex_unlock(&ci->i_fragtree_mutex);
5493e7fbe9cSYan, Zheng return 0;
5503e7fbe9cSYan, Zheng }
551355da1ebSSage Weil
552355da1ebSSage Weil /*
553355da1ebSSage Weil * initialize a newly allocated inode.
554355da1ebSSage Weil */
ceph_alloc_inode(struct super_block * sb)555355da1ebSSage Weil struct inode *ceph_alloc_inode(struct super_block *sb)
556355da1ebSSage Weil {
557355da1ebSSage Weil struct ceph_inode_info *ci;
558355da1ebSSage Weil int i;
559355da1ebSSage Weil
560fd60b288SMuchun Song ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
561355da1ebSSage Weil if (!ci)
562355da1ebSSage Weil return NULL;
563355da1ebSSage Weil
564874c8ca1SDavid Howells dout("alloc_inode %p\n", &ci->netfs.inode);
565355da1ebSSage Weil
566bc899ee1SDavid Howells /* Set parameters for the netfs library */
567e81fb419SLinus Torvalds netfs_inode_init(&ci->netfs, &ceph_netfs_ops);
568bc899ee1SDavid Howells
569be655596SSage Weil spin_lock_init(&ci->i_ceph_lock);
570be655596SSage Weil
571355da1ebSSage Weil ci->i_version = 0;
57231c542a1SYan, Zheng ci->i_inline_version = 0;
573355da1ebSSage Weil ci->i_time_warp_seq = 0;
574355da1ebSSage Weil ci->i_ceph_flags = 0;
575fdd4e158SYan, Zheng atomic64_set(&ci->i_ordered_count, 1);
576fdd4e158SYan, Zheng atomic64_set(&ci->i_release_count, 1);
577fdd4e158SYan, Zheng atomic64_set(&ci->i_complete_seq[0], 0);
578fdd4e158SYan, Zheng atomic64_set(&ci->i_complete_seq[1], 0);
579355da1ebSSage Weil ci->i_symlink = NULL;
580355da1ebSSage Weil
581fb18a575SLuis Henriques ci->i_max_bytes = 0;
582fb18a575SLuis Henriques ci->i_max_files = 0;
583fb18a575SLuis Henriques
5846c0f3af7SSage Weil memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
585785892feSJeff Layton memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
58630c156d9SYan, Zheng RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
5876c0f3af7SSage Weil
588355da1ebSSage Weil ci->i_fragtree = RB_ROOT;
589355da1ebSSage Weil mutex_init(&ci->i_fragtree_mutex);
590355da1ebSSage Weil
591355da1ebSSage Weil ci->i_xattrs.blob = NULL;
592355da1ebSSage Weil ci->i_xattrs.prealloc_blob = NULL;
593355da1ebSSage Weil ci->i_xattrs.dirty = false;
594355da1ebSSage Weil ci->i_xattrs.index = RB_ROOT;
595355da1ebSSage Weil ci->i_xattrs.count = 0;
596355da1ebSSage Weil ci->i_xattrs.names_size = 0;
597355da1ebSSage Weil ci->i_xattrs.vals_size = 0;
598355da1ebSSage Weil ci->i_xattrs.version = 0;
599355da1ebSSage Weil ci->i_xattrs.index_version = 0;
600355da1ebSSage Weil
601355da1ebSSage Weil ci->i_caps = RB_ROOT;
602355da1ebSSage Weil ci->i_auth_cap = NULL;
603355da1ebSSage Weil ci->i_dirty_caps = 0;
604355da1ebSSage Weil ci->i_flushing_caps = 0;
605355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_dirty_item);
606355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_flushing_item);
607f66fd9f0SYan, Zheng ci->i_prealloc_cap_flush = NULL;
608e4500b5eSYan, Zheng INIT_LIST_HEAD(&ci->i_cap_flush_list);
609355da1ebSSage Weil init_waitqueue_head(&ci->i_cap_wq);
610355da1ebSSage Weil ci->i_hold_caps_max = 0;
611355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_cap_delay_list);
612355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_cap_snaps);
613355da1ebSSage Weil ci->i_head_snapc = NULL;
614355da1ebSSage Weil ci->i_snap_caps = 0;
615355da1ebSSage Weil
616719a2514SYan, Zheng ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
617774a6a11SYan, Zheng for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
618355da1ebSSage Weil ci->i_nr_by_mode[i] = 0;
619355da1ebSSage Weil
620b0d7c223SYan, Zheng mutex_init(&ci->i_truncate_mutex);
621355da1ebSSage Weil ci->i_truncate_seq = 0;
622355da1ebSSage Weil ci->i_truncate_size = 0;
623355da1ebSSage Weil ci->i_truncate_pending = 0;
6245c64737dSXiubo Li ci->i_truncate_pagecache_size = 0;
625355da1ebSSage Weil
626355da1ebSSage Weil ci->i_max_size = 0;
627355da1ebSSage Weil ci->i_reported_size = 0;
628355da1ebSSage Weil ci->i_wanted_max_size = 0;
629355da1ebSSage Weil ci->i_requested_max_size = 0;
630355da1ebSSage Weil
631355da1ebSSage Weil ci->i_pin_ref = 0;
632355da1ebSSage Weil ci->i_rd_ref = 0;
633355da1ebSSage Weil ci->i_rdcache_ref = 0;
634355da1ebSSage Weil ci->i_wr_ref = 0;
635d3d0720dSHenry C Chang ci->i_wb_ref = 0;
636f85122afSJeff Layton ci->i_fx_ref = 0;
637355da1ebSSage Weil ci->i_wrbuffer_ref = 0;
638355da1ebSSage Weil ci->i_wrbuffer_ref_head = 0;
63989aa5930SYan, Zheng atomic_set(&ci->i_filelock_ref, 0);
6401e9c2eb6SYan, Zheng atomic_set(&ci->i_shared_gen, 1);
641355da1ebSSage Weil ci->i_rdcache_gen = 0;
642355da1ebSSage Weil ci->i_rdcache_revoking = 0;
643355da1ebSSage Weil
644355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_unsafe_dirops);
64568cd5b4bSYan, Zheng INIT_LIST_HEAD(&ci->i_unsafe_iops);
646355da1ebSSage Weil spin_lock_init(&ci->i_unsafe_lock);
647355da1ebSSage Weil
648355da1ebSSage Weil ci->i_snap_realm = NULL;
649355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_snap_realm_item);
650355da1ebSSage Weil INIT_LIST_HEAD(&ci->i_snap_flush_item);
651355da1ebSSage Weil
6521cf89a8dSYan, Zheng INIT_WORK(&ci->i_work, ceph_inode_work);
6531cf89a8dSYan, Zheng ci->i_work_mask = 0;
654245ce991SJeff Layton memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
6552d332d5bSJeff Layton #ifdef CONFIG_FS_ENCRYPTION
6562d332d5bSJeff Layton ci->fscrypt_auth = NULL;
6572d332d5bSJeff Layton ci->fscrypt_auth_len = 0;
6582d332d5bSJeff Layton #endif
659874c8ca1SDavid Howells return &ci->netfs.inode;
660355da1ebSSage Weil }
661355da1ebSSage Weil
ceph_free_inode(struct inode * inode)662cfa6d412SAl Viro void ceph_free_inode(struct inode *inode)
663fa0d7e3dSNick Piggin {
664fa0d7e3dSNick Piggin struct ceph_inode_info *ci = ceph_inode(inode);
665fa0d7e3dSNick Piggin
666daf5cc27SAl Viro kfree(ci->i_symlink);
6672d332d5bSJeff Layton #ifdef CONFIG_FS_ENCRYPTION
6682d332d5bSJeff Layton kfree(ci->fscrypt_auth);
6692d332d5bSJeff Layton #endif
67079f2f6adSJeff Layton fscrypt_free_inode(inode);
671fa0d7e3dSNick Piggin kmem_cache_free(ceph_inode_cachep, ci);
672fa0d7e3dSNick Piggin }
673fa0d7e3dSNick Piggin
ceph_evict_inode(struct inode * inode)67487bc5b89SYan, Zheng void ceph_evict_inode(struct inode *inode)
675355da1ebSSage Weil {
676355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
6771dd8d470SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
678355da1ebSSage Weil struct ceph_inode_frag *frag;
679355da1ebSSage Weil struct rb_node *n;
680355da1ebSSage Weil
68187bc5b89SYan, Zheng dout("evict_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode));
68287bc5b89SYan, Zheng
6831dd8d470SXiubo Li percpu_counter_dec(&mdsc->metric.total_inodes);
6841dd8d470SXiubo Li
68587bc5b89SYan, Zheng truncate_inode_pages_final(&inode->i_data);
686400e1286SJeff Layton if (inode->i_state & I_PINNING_FSCACHE_WB)
687400e1286SJeff Layton ceph_fscache_unuse_cookie(inode, true);
68887bc5b89SYan, Zheng clear_inode(inode);
689355da1ebSSage Weil
69099ccbd22SMilosz Tanski ceph_fscache_unregister_inode_cookie(ci);
6912d332d5bSJeff Layton fscrypt_put_encryption_info(inode);
69299ccbd22SMilosz Tanski
693d6e47819SYan, Zheng __ceph_remove_caps(ci);
694355da1ebSSage Weil
69555ab5520SLuís Henriques if (__ceph_has_quota(ci, QUOTA_GET_ANY))
696d557c48dSLuis Henriques ceph_adjust_quota_realms_count(inode, false);
697d557c48dSLuis Henriques
6988b218b8aSSage Weil /*
6998b218b8aSSage Weil * we may still have a snap_realm reference if there are stray
700d9df2783SYan, Zheng * caps in i_snap_caps.
7018b218b8aSSage Weil */
7028b218b8aSSage Weil if (ci->i_snap_realm) {
70375c9627eSYan, Zheng if (ceph_snap(inode) == CEPH_NOSNAP) {
70475c9627eSYan, Zheng dout(" dropping residual ref to snap realm %p\n",
7050ba92e1cSJeff Layton ci->i_snap_realm);
7060ba92e1cSJeff Layton ceph_change_snap_realm(inode, NULL);
70775c9627eSYan, Zheng } else {
70875c9627eSYan, Zheng ceph_put_snapid_map(mdsc, ci->i_snapid_map);
70975c9627eSYan, Zheng ci->i_snap_realm = NULL;
71075c9627eSYan, Zheng }
7118b218b8aSSage Weil }
7128b218b8aSSage Weil
713355da1ebSSage Weil while ((n = rb_first(&ci->i_fragtree)) != NULL) {
714355da1ebSSage Weil frag = rb_entry(n, struct ceph_inode_frag, node);
715355da1ebSSage Weil rb_erase(n, &ci->i_fragtree);
716355da1ebSSage Weil kfree(frag);
717355da1ebSSage Weil }
7181b1bc16dSYan, Zheng ci->i_fragtree_nsplits = 0;
719355da1ebSSage Weil
720355da1ebSSage Weil __ceph_destroy_xattrs(ci);
721b6c1d5b8SSage Weil if (ci->i_xattrs.blob)
722355da1ebSSage Weil ceph_buffer_put(ci->i_xattrs.blob);
723b6c1d5b8SSage Weil if (ci->i_xattrs.prealloc_blob)
724355da1ebSSage Weil ceph_buffer_put(ci->i_xattrs.prealloc_blob);
725355da1ebSSage Weil
726779fe0fbSYan, Zheng ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
727785892feSJeff Layton ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
728355da1ebSSage Weil }
729355da1ebSSage Weil
calc_inode_blocks(u64 size)730224a7542SYan, Zheng static inline blkcnt_t calc_inode_blocks(u64 size)
731224a7542SYan, Zheng {
732224a7542SYan, Zheng return (size + (1<<9) - 1) >> 9;
733224a7542SYan, Zheng }
734224a7542SYan, Zheng
735355da1ebSSage Weil /*
736355da1ebSSage Weil * Helpers to fill in size, ctime, mtime, and atime. We have to be
737355da1ebSSage Weil * careful because either the client or MDS may have more up to date
738355da1ebSSage Weil * info, depending on which capabilities are held, and whether
739355da1ebSSage Weil * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
740355da1ebSSage Weil * and size are monotonically increasing, except when utimes() or
741355da1ebSSage Weil * truncate() increments the corresponding _seq values.)
742355da1ebSSage Weil */
ceph_fill_file_size(struct inode * inode,int issued,u32 truncate_seq,u64 truncate_size,u64 size)743355da1ebSSage Weil int ceph_fill_file_size(struct inode *inode, int issued,
744355da1ebSSage Weil u32 truncate_seq, u64 truncate_size, u64 size)
745355da1ebSSage Weil {
746355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
747355da1ebSSage Weil int queue_trunc = 0;
7482d6795fbSJeff Layton loff_t isize = i_size_read(inode);
749355da1ebSSage Weil
750355da1ebSSage Weil if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
7512d6795fbSJeff Layton (truncate_seq == ci->i_truncate_seq && size > isize)) {
7522d6795fbSJeff Layton dout("size %lld -> %llu\n", isize, size);
753a3d714c3SYan, Zheng if (size > 0 && S_ISDIR(inode->i_mode)) {
754a3d714c3SYan, Zheng pr_err("fill_file_size non-zero size for directory\n");
755a3d714c3SYan, Zheng size = 0;
756a3d714c3SYan, Zheng }
75799c88e69SYan, Zheng i_size_write(inode, size);
758224a7542SYan, Zheng inode->i_blocks = calc_inode_blocks(size);
759400e1286SJeff Layton /*
760400e1286SJeff Layton * If we're expanding, then we should be able to just update
761400e1286SJeff Layton * the existing cookie.
762400e1286SJeff Layton */
763400e1286SJeff Layton if (size > isize)
764400e1286SJeff Layton ceph_fscache_update(inode);
765355da1ebSSage Weil ci->i_reported_size = size;
766355da1ebSSage Weil if (truncate_seq != ci->i_truncate_seq) {
767295fc4aaSXiubo Li dout("%s truncate_seq %u -> %u\n", __func__,
768355da1ebSSage Weil ci->i_truncate_seq, truncate_seq);
769355da1ebSSage Weil ci->i_truncate_seq = truncate_seq;
770b0d7c223SYan, Zheng
771b0d7c223SYan, Zheng /* the MDS should have revoked these caps */
77215c0a870SXiubo Li WARN_ON_ONCE(issued & (CEPH_CAP_FILE_RD |
773b0d7c223SYan, Zheng CEPH_CAP_FILE_LAZYIO));
7743d497d85SYehuda Sadeh /*
7753d497d85SYehuda Sadeh * If we hold relevant caps, or in the case where we're
7763d497d85SYehuda Sadeh * not the only client referencing this file and we
7773d497d85SYehuda Sadeh * don't hold those caps, then we need to check whether
7783d497d85SYehuda Sadeh * the file is either opened or mmaped
7793d497d85SYehuda Sadeh */
780b0d7c223SYan, Zheng if ((issued & (CEPH_CAP_FILE_CACHE|
781b0d7c223SYan, Zheng CEPH_CAP_FILE_BUFFER)) ||
7823d497d85SYehuda Sadeh mapping_mapped(inode->i_mapping) ||
783719a2514SYan, Zheng __ceph_is_file_opened(ci)) {
784355da1ebSSage Weil ci->i_truncate_pending++;
785355da1ebSSage Weil queue_trunc = 1;
786355da1ebSSage Weil }
787355da1ebSSage Weil }
788355da1ebSSage Weil }
789295fc4aaSXiubo Li
790295fc4aaSXiubo Li /*
791295fc4aaSXiubo Li * It's possible that the new sizes of the two consecutive
792295fc4aaSXiubo Li * size truncations will be in the same fscrypt last block,
793295fc4aaSXiubo Li * and we need to truncate the corresponding page caches
794295fc4aaSXiubo Li * anyway.
795295fc4aaSXiubo Li */
796295fc4aaSXiubo Li if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0) {
797295fc4aaSXiubo Li dout("%s truncate_size %lld -> %llu, encrypted %d\n", __func__,
798295fc4aaSXiubo Li ci->i_truncate_size, truncate_size, !!IS_ENCRYPTED(inode));
799295fc4aaSXiubo Li
800355da1ebSSage Weil ci->i_truncate_size = truncate_size;
801295fc4aaSXiubo Li
802295fc4aaSXiubo Li if (IS_ENCRYPTED(inode)) {
803295fc4aaSXiubo Li dout("%s truncate_pagecache_size %lld -> %llu\n",
804295fc4aaSXiubo Li __func__, ci->i_truncate_pagecache_size, size);
8055c64737dSXiubo Li ci->i_truncate_pagecache_size = size;
806295fc4aaSXiubo Li } else {
8075c64737dSXiubo Li ci->i_truncate_pagecache_size = truncate_size;
808355da1ebSSage Weil }
809355da1ebSSage Weil }
810355da1ebSSage Weil return queue_trunc;
811355da1ebSSage Weil }
812355da1ebSSage Weil
ceph_fill_file_time(struct inode * inode,int issued,u64 time_warp_seq,struct timespec64 * ctime,struct timespec64 * mtime,struct timespec64 * atime)813355da1ebSSage Weil void ceph_fill_file_time(struct inode *inode, int issued,
8149bbeab41SArnd Bergmann u64 time_warp_seq, struct timespec64 *ctime,
8159bbeab41SArnd Bergmann struct timespec64 *mtime, struct timespec64 *atime)
816355da1ebSSage Weil {
817355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
8187795aef0SJeff Layton struct timespec64 ictime = inode_get_ctime(inode);
819355da1ebSSage Weil int warn = 0;
820355da1ebSSage Weil
821355da1ebSSage Weil if (issued & (CEPH_CAP_FILE_EXCL|
822355da1ebSSage Weil CEPH_CAP_FILE_WR|
823d8672d64SSage Weil CEPH_CAP_FILE_BUFFER|
824d8672d64SSage Weil CEPH_CAP_AUTH_EXCL|
825d8672d64SSage Weil CEPH_CAP_XATTR_EXCL)) {
826ffdeec7aSYan, Zheng if (ci->i_version == 0 ||
8277795aef0SJeff Layton timespec64_compare(ctime, &ictime) > 0) {
82813442b03SDeepa Dinamani dout("ctime %lld.%09ld -> %lld.%09ld inc w/ cap\n",
8297795aef0SJeff Layton ictime.tv_sec, ictime.tv_nsec,
8309bbeab41SArnd Bergmann ctime->tv_sec, ctime->tv_nsec);
8317795aef0SJeff Layton inode_set_ctime_to_ts(inode, *ctime);
832355da1ebSSage Weil }
833ffdeec7aSYan, Zheng if (ci->i_version == 0 ||
834ffdeec7aSYan, Zheng ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) {
835355da1ebSSage Weil /* the MDS did a utimes() */
83613442b03SDeepa Dinamani dout("mtime %lld.%09ld -> %lld.%09ld "
837355da1ebSSage Weil "tw %d -> %d\n",
8389bbeab41SArnd Bergmann inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
8399bbeab41SArnd Bergmann mtime->tv_sec, mtime->tv_nsec,
840355da1ebSSage Weil ci->i_time_warp_seq, (int)time_warp_seq);
841355da1ebSSage Weil
8429bbeab41SArnd Bergmann inode->i_mtime = *mtime;
8439bbeab41SArnd Bergmann inode->i_atime = *atime;
844355da1ebSSage Weil ci->i_time_warp_seq = time_warp_seq;
845355da1ebSSage Weil } else if (time_warp_seq == ci->i_time_warp_seq) {
846355da1ebSSage Weil /* nobody did utimes(); take the max */
8479bbeab41SArnd Bergmann if (timespec64_compare(mtime, &inode->i_mtime) > 0) {
84813442b03SDeepa Dinamani dout("mtime %lld.%09ld -> %lld.%09ld inc\n",
8499bbeab41SArnd Bergmann inode->i_mtime.tv_sec,
850355da1ebSSage Weil inode->i_mtime.tv_nsec,
8519bbeab41SArnd Bergmann mtime->tv_sec, mtime->tv_nsec);
8529bbeab41SArnd Bergmann inode->i_mtime = *mtime;
853355da1ebSSage Weil }
8549bbeab41SArnd Bergmann if (timespec64_compare(atime, &inode->i_atime) > 0) {
85513442b03SDeepa Dinamani dout("atime %lld.%09ld -> %lld.%09ld inc\n",
8569bbeab41SArnd Bergmann inode->i_atime.tv_sec,
857355da1ebSSage Weil inode->i_atime.tv_nsec,
8589bbeab41SArnd Bergmann atime->tv_sec, atime->tv_nsec);
8599bbeab41SArnd Bergmann inode->i_atime = *atime;
860355da1ebSSage Weil }
861355da1ebSSage Weil } else if (issued & CEPH_CAP_FILE_EXCL) {
862355da1ebSSage Weil /* we did a utimes(); ignore mds values */
863355da1ebSSage Weil } else {
864355da1ebSSage Weil warn = 1;
865355da1ebSSage Weil }
866355da1ebSSage Weil } else {
867d8672d64SSage Weil /* we have no write|excl caps; whatever the MDS says is true */
868355da1ebSSage Weil if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) {
8697795aef0SJeff Layton inode_set_ctime_to_ts(inode, *ctime);
8709bbeab41SArnd Bergmann inode->i_mtime = *mtime;
8719bbeab41SArnd Bergmann inode->i_atime = *atime;
872355da1ebSSage Weil ci->i_time_warp_seq = time_warp_seq;
873355da1ebSSage Weil } else {
874355da1ebSSage Weil warn = 1;
875355da1ebSSage Weil }
876355da1ebSSage Weil }
877355da1ebSSage Weil if (warn) /* time_warp_seq shouldn't go backwards */
878355da1ebSSage Weil dout("%p mds time_warp_seq %llu < %u\n",
879355da1ebSSage Weil inode, time_warp_seq, ci->i_time_warp_seq);
880355da1ebSSage Weil }
881355da1ebSSage Weil
88279f2f6adSJeff Layton #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
decode_encrypted_symlink(const char * encsym,int enclen,u8 ** decsym)88379f2f6adSJeff Layton static int decode_encrypted_symlink(const char *encsym, int enclen, u8 **decsym)
88479f2f6adSJeff Layton {
88579f2f6adSJeff Layton int declen;
88679f2f6adSJeff Layton u8 *sym;
88779f2f6adSJeff Layton
88879f2f6adSJeff Layton sym = kmalloc(enclen + 1, GFP_NOFS);
88979f2f6adSJeff Layton if (!sym)
89079f2f6adSJeff Layton return -ENOMEM;
89179f2f6adSJeff Layton
89279f2f6adSJeff Layton declen = ceph_base64_decode(encsym, enclen, sym);
89379f2f6adSJeff Layton if (declen < 0) {
89479f2f6adSJeff Layton pr_err("%s: can't decode symlink (%d). Content: %.*s\n",
89579f2f6adSJeff Layton __func__, declen, enclen, encsym);
89679f2f6adSJeff Layton kfree(sym);
89779f2f6adSJeff Layton return -EIO;
89879f2f6adSJeff Layton }
89979f2f6adSJeff Layton sym[declen + 1] = '\0';
90079f2f6adSJeff Layton *decsym = sym;
90179f2f6adSJeff Layton return declen;
90279f2f6adSJeff Layton }
90379f2f6adSJeff Layton #else
decode_encrypted_symlink(const char * encsym,int symlen,u8 ** decsym)90479f2f6adSJeff Layton static int decode_encrypted_symlink(const char *encsym, int symlen, u8 **decsym)
90579f2f6adSJeff Layton {
90679f2f6adSJeff Layton return -EOPNOTSUPP;
90779f2f6adSJeff Layton }
90879f2f6adSJeff Layton #endif
90979f2f6adSJeff Layton
910355da1ebSSage Weil /*
911355da1ebSSage Weil * Populate an inode based on info from mds. May be called on new or
912355da1ebSSage Weil * existing inodes.
913355da1ebSSage Weil */
ceph_fill_inode(struct inode * inode,struct page * locked_page,struct ceph_mds_reply_info_in * iinfo,struct ceph_mds_reply_dirfrag * dirinfo,struct ceph_mds_session * session,int cap_fmode,struct ceph_cap_reservation * caps_reservation)914966c7160SJeff Layton int ceph_fill_inode(struct inode *inode, struct page *locked_page,
915355da1ebSSage Weil struct ceph_mds_reply_info_in *iinfo,
916355da1ebSSage Weil struct ceph_mds_reply_dirfrag *dirinfo,
91757c21994SJeff Layton struct ceph_mds_session *session, int cap_fmode,
918355da1ebSSage Weil struct ceph_cap_reservation *caps_reservation)
919355da1ebSSage Weil {
9202678da88SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
921355da1ebSSage Weil struct ceph_mds_reply_inode *info = iinfo->in;
922355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
9232af54a72SYan, Zheng int issued, new_issued, info_caps;
9249bbeab41SArnd Bergmann struct timespec64 mtime, atime, ctime;
925355da1ebSSage Weil struct ceph_buffer *xattr_blob = NULL;
926af8a85a4SLuis Henriques struct ceph_buffer *old_blob = NULL;
927779fe0fbSYan, Zheng struct ceph_string *pool_ns = NULL;
928d9df2783SYan, Zheng struct ceph_cap *new_cap = NULL;
929355da1ebSSage Weil int err = 0;
930d9df2783SYan, Zheng bool wake = false;
931f98a128aSYan, Zheng bool queue_trunc = false;
932f98a128aSYan, Zheng bool new_version = false;
93331c542a1SYan, Zheng bool fill_inline = false;
934ed94f87cSJeff Layton umode_t mode = le32_to_cpu(info->mode);
935ed94f87cSJeff Layton dev_t rdev = le32_to_cpu(info->rdev);
936355da1ebSSage Weil
93727171ae6SJeff Layton lockdep_assert_held(&mdsc->snap_rwsem);
93827171ae6SJeff Layton
939966c7160SJeff Layton dout("%s %p ino %llx.%llx v %llu had %llu\n", __func__,
940355da1ebSSage Weil inode, ceph_vinop(inode), le64_to_cpu(info->version),
941355da1ebSSage Weil ci->i_version);
942355da1ebSSage Weil
943ed94f87cSJeff Layton /* Once I_NEW is cleared, we can't change type or dev numbers */
944ed94f87cSJeff Layton if (inode->i_state & I_NEW) {
945ed94f87cSJeff Layton inode->i_mode = mode;
946ed94f87cSJeff Layton } else {
947ed94f87cSJeff Layton if (inode_wrong_type(inode, mode)) {
948ed94f87cSJeff Layton pr_warn_once("inode type changed! (ino %llx.%llx is 0%o, mds says 0%o)\n",
949ed94f87cSJeff Layton ceph_vinop(inode), inode->i_mode, mode);
950ed94f87cSJeff Layton return -ESTALE;
951ed94f87cSJeff Layton }
952ed94f87cSJeff Layton
953ed94f87cSJeff Layton if ((S_ISCHR(mode) || S_ISBLK(mode)) && inode->i_rdev != rdev) {
954ed94f87cSJeff Layton pr_warn_once("dev inode rdev changed! (ino %llx.%llx is %u:%u, mds says %u:%u)\n",
955ed94f87cSJeff Layton ceph_vinop(inode), MAJOR(inode->i_rdev),
956ed94f87cSJeff Layton MINOR(inode->i_rdev), MAJOR(rdev),
957ed94f87cSJeff Layton MINOR(rdev));
958ed94f87cSJeff Layton return -ESTALE;
959ed94f87cSJeff Layton }
960ed94f87cSJeff Layton }
961ed94f87cSJeff Layton
9622af54a72SYan, Zheng info_caps = le32_to_cpu(info->cap.caps);
9632af54a72SYan, Zheng
964d9df2783SYan, Zheng /* prealloc new cap struct */
9659a6bed4fSJeff Layton if (info_caps && ceph_snap(inode) == CEPH_NOSNAP) {
966d9df2783SYan, Zheng new_cap = ceph_get_cap(mdsc, caps_reservation);
9679a6bed4fSJeff Layton if (!new_cap)
9689a6bed4fSJeff Layton return -ENOMEM;
9699a6bed4fSJeff Layton }
970d9df2783SYan, Zheng
971355da1ebSSage Weil /*
972355da1ebSSage Weil * prealloc xattr data, if it looks like we'll need it. only
973355da1ebSSage Weil * if len > 4 (meaning there are actually xattrs; the first 4
974355da1ebSSage Weil * bytes are the xattr count).
975355da1ebSSage Weil */
976355da1ebSSage Weil if (iinfo->xattr_len > 4) {
977b6c1d5b8SSage Weil xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS);
978355da1ebSSage Weil if (!xattr_blob)
979966c7160SJeff Layton pr_err("%s ENOMEM xattr blob %d bytes\n", __func__,
980355da1ebSSage Weil iinfo->xattr_len);
981355da1ebSSage Weil }
982355da1ebSSage Weil
983779fe0fbSYan, Zheng if (iinfo->pool_ns_len > 0)
984779fe0fbSYan, Zheng pool_ns = ceph_find_or_create_string(iinfo->pool_ns_data,
985779fe0fbSYan, Zheng iinfo->pool_ns_len);
986779fe0fbSYan, Zheng
98775c9627eSYan, Zheng if (ceph_snap(inode) != CEPH_NOSNAP && !ci->i_snapid_map)
98875c9627eSYan, Zheng ci->i_snapid_map = ceph_get_snapid_map(mdsc, ceph_snap(inode));
98975c9627eSYan, Zheng
990be655596SSage Weil spin_lock(&ci->i_ceph_lock);
991355da1ebSSage Weil
992355da1ebSSage Weil /*
993355da1ebSSage Weil * provided version will be odd if inode value is projected,
9948bd59e01SSage Weil * even if stable. skip the update if we have newer stable
9958bd59e01SSage Weil * info (ours>=theirs, e.g. due to racing mds replies), unless
9968bd59e01SSage Weil * we are getting projected (unstable) info (in which case the
9978bd59e01SSage Weil * version is odd, and we want ours>theirs).
9988bd59e01SSage Weil * us them
9998bd59e01SSage Weil * 2 2 skip
10008bd59e01SSage Weil * 3 2 skip
10018bd59e01SSage Weil * 3 3 update
1002355da1ebSSage Weil */
1003f98a128aSYan, Zheng if (ci->i_version == 0 ||
1004f98a128aSYan, Zheng ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1005f98a128aSYan, Zheng le64_to_cpu(info->version) > (ci->i_version & ~1)))
1006f98a128aSYan, Zheng new_version = true;
1007355da1ebSSage Weil
1008a35ead31SJeff Layton /* Update change_attribute */
1009a35ead31SJeff Layton inode_set_max_iversion_raw(inode, iinfo->change_attr);
1010a35ead31SJeff Layton
10112af54a72SYan, Zheng __ceph_caps_issued(ci, &issued);
10122af54a72SYan, Zheng issued |= __ceph_caps_dirty(ci);
10132af54a72SYan, Zheng new_issued = ~issued & info_caps;
1014355da1ebSSage Weil
1015d557c48dSLuis Henriques __ceph_update_quota(ci, iinfo->max_bytes, iinfo->max_files);
1016fb18a575SLuis Henriques
10172d332d5bSJeff Layton #ifdef CONFIG_FS_ENCRYPTION
1018e127e030SLuís Henriques if (iinfo->fscrypt_auth_len &&
1019e127e030SLuís Henriques ((inode->i_state & I_NEW) || (ci->fscrypt_auth_len == 0))) {
10202d332d5bSJeff Layton kfree(ci->fscrypt_auth);
10212d332d5bSJeff Layton ci->fscrypt_auth_len = iinfo->fscrypt_auth_len;
10222d332d5bSJeff Layton ci->fscrypt_auth = iinfo->fscrypt_auth;
10232d332d5bSJeff Layton iinfo->fscrypt_auth = NULL;
10242d332d5bSJeff Layton iinfo->fscrypt_auth_len = 0;
10252d332d5bSJeff Layton inode_set_flags(inode, S_ENCRYPTED, S_ENCRYPTED);
10262d332d5bSJeff Layton }
10272d332d5bSJeff Layton #endif
10282d332d5bSJeff Layton
1029f98a128aSYan, Zheng if ((new_version || (new_issued & CEPH_CAP_AUTH_SHARED)) &&
1030f98a128aSYan, Zheng (issued & CEPH_CAP_AUTH_EXCL) == 0) {
1031ed94f87cSJeff Layton inode->i_mode = mode;
1032ab871b90SEric W. Biederman inode->i_uid = make_kuid(&init_user_ns, le32_to_cpu(info->uid));
1033ab871b90SEric W. Biederman inode->i_gid = make_kgid(&init_user_ns, le32_to_cpu(info->gid));
1034355da1ebSSage Weil dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode,
1035bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, inode->i_uid),
1036bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, inode->i_gid));
1037245ce991SJeff Layton ceph_decode_timespec64(&ci->i_btime, &iinfo->btime);
1038193e7b37SDavid Disseldorp ceph_decode_timespec64(&ci->i_snap_btime, &iinfo->snap_btime);
1039355da1ebSSage Weil }
1040355da1ebSSage Weil
104194af0470SJeff Layton /* directories have fl_stripe_unit set to zero */
104294af0470SJeff Layton if (IS_ENCRYPTED(inode))
104394af0470SJeff Layton inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
104494af0470SJeff Layton else if (le32_to_cpu(info->layout.fl_stripe_unit))
104594af0470SJeff Layton inode->i_blkbits =
104694af0470SJeff Layton fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1;
104794af0470SJeff Layton else
104894af0470SJeff Layton inode->i_blkbits = CEPH_BLOCK_SHIFT;
104994af0470SJeff Layton
1050f98a128aSYan, Zheng if ((new_version || (new_issued & CEPH_CAP_LINK_SHARED)) &&
1051f98a128aSYan, Zheng (issued & CEPH_CAP_LINK_EXCL) == 0)
1052bfe86848SMiklos Szeredi set_nlink(inode, le32_to_cpu(info->nlink));
1053355da1ebSSage Weil
1054f98a128aSYan, Zheng if (new_version || (new_issued & CEPH_CAP_ANY_RD)) {
1055355da1ebSSage Weil /* be careful with mtime, atime, size */
10569bbeab41SArnd Bergmann ceph_decode_timespec64(&atime, &info->atime);
10579bbeab41SArnd Bergmann ceph_decode_timespec64(&mtime, &info->mtime);
10589bbeab41SArnd Bergmann ceph_decode_timespec64(&ctime, &info->ctime);
1059f98a128aSYan, Zheng ceph_fill_file_time(inode, issued,
1060f98a128aSYan, Zheng le32_to_cpu(info->time_warp_seq),
1061f98a128aSYan, Zheng &ctime, &mtime, &atime);
1062f98a128aSYan, Zheng }
1063f98a128aSYan, Zheng
10642af54a72SYan, Zheng if (new_version || (info_caps & CEPH_CAP_FILE_SHARED)) {
10652af54a72SYan, Zheng ci->i_files = le64_to_cpu(info->files);
10662af54a72SYan, Zheng ci->i_subdirs = le64_to_cpu(info->subdirs);
10672af54a72SYan, Zheng }
10682af54a72SYan, Zheng
1069f98a128aSYan, Zheng if (new_version ||
1070f98a128aSYan, Zheng (new_issued & (CEPH_CAP_ANY_FILE_RD | CEPH_CAP_ANY_FILE_WR))) {
107116be62fcSJeff Layton u64 size = le64_to_cpu(info->size);
10727627151eSYan, Zheng s64 old_pool = ci->i_layout.pool_id;
1073779fe0fbSYan, Zheng struct ceph_string *old_ns;
1074779fe0fbSYan, Zheng
10757627151eSYan, Zheng ceph_file_layout_from_legacy(&ci->i_layout, &info->layout);
1076779fe0fbSYan, Zheng old_ns = rcu_dereference_protected(ci->i_layout.pool_ns,
1077779fe0fbSYan, Zheng lockdep_is_held(&ci->i_ceph_lock));
1078779fe0fbSYan, Zheng rcu_assign_pointer(ci->i_layout.pool_ns, pool_ns);
1079779fe0fbSYan, Zheng
1080779fe0fbSYan, Zheng if (ci->i_layout.pool_id != old_pool || pool_ns != old_ns)
108110183a69SYan, Zheng ci->i_ceph_flags &= ~CEPH_I_POOL_PERM;
108210183a69SYan, Zheng
1083779fe0fbSYan, Zheng pool_ns = old_ns;
1084f98a128aSYan, Zheng
108516be62fcSJeff Layton if (IS_ENCRYPTED(inode) && size &&
108616be62fcSJeff Layton iinfo->fscrypt_file_len == sizeof(__le64)) {
108716be62fcSJeff Layton u64 fsize = __le64_to_cpu(*(__le64 *)iinfo->fscrypt_file);
108816be62fcSJeff Layton
108916be62fcSJeff Layton if (size == round_up(fsize, CEPH_FSCRYPT_BLOCK_SIZE)) {
109016be62fcSJeff Layton size = fsize;
109116be62fcSJeff Layton } else {
109216be62fcSJeff Layton pr_warn("fscrypt size mismatch: size=%llu fscrypt_file=%llu, discarding fscrypt_file size.\n",
109316be62fcSJeff Layton info->size, size);
109416be62fcSJeff Layton }
109516be62fcSJeff Layton }
109616be62fcSJeff Layton
1097355da1ebSSage Weil queue_trunc = ceph_fill_file_size(inode, issued,
1098355da1ebSSage Weil le32_to_cpu(info->truncate_seq),
1099355da1ebSSage Weil le64_to_cpu(info->truncate_size),
110016be62fcSJeff Layton size);
1101f98a128aSYan, Zheng /* only update max_size on auth cap */
1102f98a128aSYan, Zheng if ((info->cap.flags & CEPH_CAP_FLAG_AUTH) &&
1103f98a128aSYan, Zheng ci->i_max_size != le64_to_cpu(info->max_size)) {
1104f98a128aSYan, Zheng dout("max_size %lld -> %llu\n", ci->i_max_size,
1105f98a128aSYan, Zheng le64_to_cpu(info->max_size));
1106f98a128aSYan, Zheng ci->i_max_size = le64_to_cpu(info->max_size);
1107f98a128aSYan, Zheng }
1108f98a128aSYan, Zheng }
1109355da1ebSSage Weil
111049a9f4f6SYan, Zheng /* layout and rstat are not tracked by capability, update them if
111149a9f4f6SYan, Zheng * the inode info is from auth mds */
111249a9f4f6SYan, Zheng if (new_version || (info->cap.flags & CEPH_CAP_FLAG_AUTH)) {
111349a9f4f6SYan, Zheng if (S_ISDIR(inode->i_mode)) {
111449a9f4f6SYan, Zheng ci->i_dir_layout = iinfo->dir_layout;
111549a9f4f6SYan, Zheng ci->i_rbytes = le64_to_cpu(info->rbytes);
111649a9f4f6SYan, Zheng ci->i_rfiles = le64_to_cpu(info->rfiles);
111749a9f4f6SYan, Zheng ci->i_rsubdirs = le64_to_cpu(info->rsubdirs);
111808796873SYan, Zheng ci->i_dir_pin = iinfo->dir_pin;
1119e7f72952SYanhu Cao ci->i_rsnaps = iinfo->rsnaps;
11209bbeab41SArnd Bergmann ceph_decode_timespec64(&ci->i_rctime, &info->rctime);
112149a9f4f6SYan, Zheng }
112249a9f4f6SYan, Zheng }
112349a9f4f6SYan, Zheng
1124355da1ebSSage Weil /* xattrs */
1125355da1ebSSage Weil /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */
1126508b32d8SYan, Zheng if ((ci->i_xattrs.version == 0 || !(issued & CEPH_CAP_XATTR_EXCL)) &&
1127355da1ebSSage Weil le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) {
1128355da1ebSSage Weil if (ci->i_xattrs.blob)
1129af8a85a4SLuis Henriques old_blob = ci->i_xattrs.blob;
1130355da1ebSSage Weil ci->i_xattrs.blob = xattr_blob;
1131355da1ebSSage Weil if (xattr_blob)
1132355da1ebSSage Weil memcpy(ci->i_xattrs.blob->vec.iov_base,
1133355da1ebSSage Weil iinfo->xattr_data, iinfo->xattr_len);
1134355da1ebSSage Weil ci->i_xattrs.version = le64_to_cpu(info->xattr_version);
11357221fe4cSGuangliang Zhao ceph_forget_all_cached_acls(inode);
1136ac6713ccSYan, Zheng ceph_security_invalidate_secctx(inode);
1137a6424e48SSage Weil xattr_blob = NULL;
1138355da1ebSSage Weil }
1139355da1ebSSage Weil
1140ffdeec7aSYan, Zheng /* finally update i_version */
1141aae1a442SYan, Zheng if (le64_to_cpu(info->version) > ci->i_version)
1142ffdeec7aSYan, Zheng ci->i_version = le64_to_cpu(info->version);
1143ffdeec7aSYan, Zheng
1144355da1ebSSage Weil inode->i_mapping->a_ops = &ceph_aops;
1145355da1ebSSage Weil
1146355da1ebSSage Weil switch (inode->i_mode & S_IFMT) {
1147355da1ebSSage Weil case S_IFIFO:
1148355da1ebSSage Weil case S_IFBLK:
1149355da1ebSSage Weil case S_IFCHR:
1150355da1ebSSage Weil case S_IFSOCK:
11515ba72e60SYan, Zheng inode->i_blkbits = PAGE_SHIFT;
1152ed94f87cSJeff Layton init_special_inode(inode, inode->i_mode, rdev);
1153355da1ebSSage Weil inode->i_op = &ceph_file_iops;
1154355da1ebSSage Weil break;
1155355da1ebSSage Weil case S_IFREG:
1156355da1ebSSage Weil inode->i_op = &ceph_file_iops;
1157355da1ebSSage Weil inode->i_fop = &ceph_file_fops;
1158355da1ebSSage Weil break;
1159355da1ebSSage Weil case S_IFLNK:
1160355da1ebSSage Weil if (!ci->i_symlink) {
1161810339ecSXi Wang u32 symlen = iinfo->symlink_len;
1162355da1ebSSage Weil char *sym;
1163355da1ebSSage Weil
1164be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
1165355da1ebSSage Weil
116679f2f6adSJeff Layton if (IS_ENCRYPTED(inode)) {
116779f2f6adSJeff Layton if (symlen != i_size_read(inode))
116879f2f6adSJeff Layton pr_err("%s %llx.%llx BAD symlink size %lld\n",
116979f2f6adSJeff Layton __func__, ceph_vinop(inode),
117079f2f6adSJeff Layton i_size_read(inode));
117179f2f6adSJeff Layton
117279f2f6adSJeff Layton err = decode_encrypted_symlink(iinfo->symlink,
117379f2f6adSJeff Layton symlen, (u8 **)&sym);
117479f2f6adSJeff Layton if (err < 0) {
117579f2f6adSJeff Layton pr_err("%s decoding encrypted symlink failed: %d\n",
117679f2f6adSJeff Layton __func__, err);
117779f2f6adSJeff Layton goto out;
117879f2f6adSJeff Layton }
117979f2f6adSJeff Layton symlen = err;
118079f2f6adSJeff Layton i_size_write(inode, symlen);
118179f2f6adSJeff Layton inode->i_blocks = calc_inode_blocks(symlen);
118279f2f6adSJeff Layton } else {
1183224a7542SYan, Zheng if (symlen != i_size_read(inode)) {
118479f2f6adSJeff Layton pr_err("%s %llx.%llx BAD symlink size %lld\n",
118579f2f6adSJeff Layton __func__, ceph_vinop(inode),
1186224a7542SYan, Zheng i_size_read(inode));
1187224a7542SYan, Zheng i_size_write(inode, symlen);
1188224a7542SYan, Zheng inode->i_blocks = calc_inode_blocks(symlen);
1189224a7542SYan, Zheng }
1190810339ecSXi Wang
1191355da1ebSSage Weil err = -ENOMEM;
1192810339ecSXi Wang sym = kstrndup(iinfo->symlink, symlen, GFP_NOFS);
1193355da1ebSSage Weil if (!sym)
1194355da1ebSSage Weil goto out;
119579f2f6adSJeff Layton }
1196355da1ebSSage Weil
1197be655596SSage Weil spin_lock(&ci->i_ceph_lock);
1198355da1ebSSage Weil if (!ci->i_symlink)
1199355da1ebSSage Weil ci->i_symlink = sym;
1200355da1ebSSage Weil else
1201355da1ebSSage Weil kfree(sym); /* lost a race */
1202355da1ebSSage Weil }
120379f2f6adSJeff Layton
120479f2f6adSJeff Layton if (IS_ENCRYPTED(inode)) {
120579f2f6adSJeff Layton /*
120679f2f6adSJeff Layton * Encrypted symlinks need to be decrypted before we can
120779f2f6adSJeff Layton * cache their targets in i_link. Don't touch it here.
120879f2f6adSJeff Layton */
120979f2f6adSJeff Layton inode->i_op = &ceph_encrypted_symlink_iops;
121079f2f6adSJeff Layton } else {
1211ac194dccSAl Viro inode->i_link = ci->i_symlink;
121279f2f6adSJeff Layton inode->i_op = &ceph_symlink_iops;
121379f2f6adSJeff Layton }
1214355da1ebSSage Weil break;
1215355da1ebSSage Weil case S_IFDIR:
1216355da1ebSSage Weil inode->i_op = &ceph_dir_iops;
1217355da1ebSSage Weil inode->i_fop = &ceph_dir_fops;
1218355da1ebSSage Weil break;
1219355da1ebSSage Weil default:
1220966c7160SJeff Layton pr_err("%s %llx.%llx BAD mode 0%o\n", __func__,
1221355da1ebSSage Weil ceph_vinop(inode), inode->i_mode);
1222355da1ebSSage Weil }
1223355da1ebSSage Weil
1224355da1ebSSage Weil /* were we issued a capability? */
12252af54a72SYan, Zheng if (info_caps) {
1226355da1ebSSage Weil if (ceph_snap(inode) == CEPH_NOSNAP) {
1227355da1ebSSage Weil ceph_add_cap(inode, session,
1228355da1ebSSage Weil le64_to_cpu(info->cap.cap_id),
1229135e671eSYan, Zheng info_caps,
1230355da1ebSSage Weil le32_to_cpu(info->cap.wanted),
1231355da1ebSSage Weil le32_to_cpu(info->cap.seq),
1232355da1ebSSage Weil le32_to_cpu(info->cap.mseq),
1233355da1ebSSage Weil le64_to_cpu(info->cap.realm),
1234d9df2783SYan, Zheng info->cap.flags, &new_cap);
12352f92b3d0SYan, Zheng
12362f92b3d0SYan, Zheng /* set dir completion flag? */
12372f92b3d0SYan, Zheng if (S_ISDIR(inode->i_mode) &&
12382f92b3d0SYan, Zheng ci->i_files == 0 && ci->i_subdirs == 0 &&
12392af54a72SYan, Zheng (info_caps & CEPH_CAP_FILE_SHARED) &&
12402f92b3d0SYan, Zheng (issued & CEPH_CAP_FILE_EXCL) == 0 &&
12412f92b3d0SYan, Zheng !__ceph_dir_is_complete(ci)) {
12422f92b3d0SYan, Zheng dout(" marking %p complete (empty)\n", inode);
1243fdd4e158SYan, Zheng i_size_write(inode, 0);
12442f92b3d0SYan, Zheng __ceph_dir_set_complete(ci,
1245fdd4e158SYan, Zheng atomic64_read(&ci->i_release_count),
1246fdd4e158SYan, Zheng atomic64_read(&ci->i_ordered_count));
12472f92b3d0SYan, Zheng }
12482f92b3d0SYan, Zheng
1249d9df2783SYan, Zheng wake = true;
1250355da1ebSSage Weil } else {
1251355da1ebSSage Weil dout(" %p got snap_caps %s\n", inode,
12522af54a72SYan, Zheng ceph_cap_string(info_caps));
12532af54a72SYan, Zheng ci->i_snap_caps |= info_caps;
1254355da1ebSSage Weil }
1255355da1ebSSage Weil }
125631c542a1SYan, Zheng
125731c542a1SYan, Zheng if (iinfo->inline_version > 0 &&
125831c542a1SYan, Zheng iinfo->inline_version >= ci->i_inline_version) {
125931c542a1SYan, Zheng int cache_caps = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
126031c542a1SYan, Zheng ci->i_inline_version = iinfo->inline_version;
126148490776SXiubo Li if (ceph_has_inline_data(ci) &&
12622af54a72SYan, Zheng (locked_page || (info_caps & cache_caps)))
126331c542a1SYan, Zheng fill_inline = true;
126431c542a1SYan, Zheng }
126531c542a1SYan, Zheng
1266719a2514SYan, Zheng if (cap_fmode >= 0) {
1267719a2514SYan, Zheng if (!info_caps)
1268719a2514SYan, Zheng pr_warn("mds issued no caps on %llx.%llx\n",
1269719a2514SYan, Zheng ceph_vinop(inode));
1270719a2514SYan, Zheng __ceph_touch_fmode(ci, mdsc, cap_fmode);
1271719a2514SYan, Zheng }
1272719a2514SYan, Zheng
1273355da1ebSSage Weil spin_unlock(&ci->i_ceph_lock);
1274355da1ebSSage Weil
1275400e1286SJeff Layton ceph_fscache_register_inode_cookie(inode);
1276400e1286SJeff Layton
127731c542a1SYan, Zheng if (fill_inline)
127801deead0SYan, Zheng ceph_fill_inline_data(inode, locked_page,
127931c542a1SYan, Zheng iinfo->inline_data, iinfo->inline_len);
128031c542a1SYan, Zheng
1281d9df2783SYan, Zheng if (wake)
1282d9df2783SYan, Zheng wake_up_all(&ci->i_cap_wq);
1283d9df2783SYan, Zheng
1284355da1ebSSage Weil /* queue truncate if we saw i_size decrease */
1285355da1ebSSage Weil if (queue_trunc)
1286355da1ebSSage Weil ceph_queue_vmtruncate(inode);
1287355da1ebSSage Weil
1288355da1ebSSage Weil /* populate frag tree */
12893e7fbe9cSYan, Zheng if (S_ISDIR(inode->i_mode))
12903e7fbe9cSYan, Zheng ceph_fill_fragtree(inode, &info->fragtree, dirinfo);
1291355da1ebSSage Weil
1292355da1ebSSage Weil /* update delegation info? */
1293355da1ebSSage Weil if (dirinfo)
1294355da1ebSSage Weil ceph_fill_dirfrag(inode, dirinfo);
1295355da1ebSSage Weil
1296355da1ebSSage Weil err = 0;
1297355da1ebSSage Weil out:
1298d9df2783SYan, Zheng if (new_cap)
1299d9df2783SYan, Zheng ceph_put_cap(mdsc, new_cap);
1300af8a85a4SLuis Henriques ceph_buffer_put(old_blob);
1301355da1ebSSage Weil ceph_buffer_put(xattr_blob);
1302779fe0fbSYan, Zheng ceph_put_string(pool_ns);
1303355da1ebSSage Weil return err;
1304355da1ebSSage Weil }
1305355da1ebSSage Weil
1306355da1ebSSage Weil /*
1307543212b3SYan, Zheng * caller should hold session s_mutex and dentry->d_lock.
1308355da1ebSSage Weil */
__update_dentry_lease(struct inode * dir,struct dentry * dentry,struct ceph_mds_reply_lease * lease,struct ceph_mds_session * session,unsigned long from_time,struct ceph_mds_session ** old_lease_session)1309543212b3SYan, Zheng static void __update_dentry_lease(struct inode *dir, struct dentry *dentry,
1310355da1ebSSage Weil struct ceph_mds_reply_lease *lease,
1311355da1ebSSage Weil struct ceph_mds_session *session,
1312f5d55f03SJeff Layton unsigned long from_time,
1313543212b3SYan, Zheng struct ceph_mds_session **old_lease_session)
1314355da1ebSSage Weil {
1315355da1ebSSage Weil struct ceph_dentry_info *di = ceph_dentry(dentry);
1316f5e17aedSJeff Layton unsigned mask = le16_to_cpu(lease->mask);
1317355da1ebSSage Weil long unsigned duration = le32_to_cpu(lease->duration_ms);
1318355da1ebSSage Weil long unsigned ttl = from_time + (duration * HZ) / 1000;
1319355da1ebSSage Weil long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000;
1320355da1ebSSage Weil
13212f90b852SSage Weil dout("update_dentry_lease %p duration %lu ms ttl %lu\n",
13222f90b852SSage Weil dentry, duration, ttl);
1323355da1ebSSage Weil
132418fc8abdSAl Viro /* only track leases on regular dentries */
132518fc8abdSAl Viro if (ceph_snap(dir) != CEPH_NOSNAP)
1326543212b3SYan, Zheng return;
132718fc8abdSAl Viro
1328f5e17aedSJeff Layton if (mask & CEPH_LEASE_PRIMARY_LINK)
1329f5e17aedSJeff Layton di->flags |= CEPH_DENTRY_PRIMARY_LINK;
1330f5e17aedSJeff Layton else
1331f5e17aedSJeff Layton di->flags &= ~CEPH_DENTRY_PRIMARY_LINK;
1332f5e17aedSJeff Layton
133397aeb6bfSYan, Zheng di->lease_shared_gen = atomic_read(&ceph_inode(dir)->i_shared_gen);
1334f5e17aedSJeff Layton if (!(mask & CEPH_LEASE_VALID)) {
133537c4efc1SYan, Zheng __ceph_dentry_dir_lease_touch(di);
1336543212b3SYan, Zheng return;
133737c4efc1SYan, Zheng }
1338355da1ebSSage Weil
133952d60f8eSJeff Layton if (di->lease_gen == atomic_read(&session->s_cap_gen) &&
13409b16f03cSMiklos Szeredi time_before(ttl, di->time))
1341543212b3SYan, Zheng return; /* we already have a newer lease. */
1342355da1ebSSage Weil
1343481f001fSYan, Zheng if (di->lease_session && di->lease_session != session) {
1344543212b3SYan, Zheng *old_lease_session = di->lease_session;
1345481f001fSYan, Zheng di->lease_session = NULL;
1346481f001fSYan, Zheng }
1347355da1ebSSage Weil
1348355da1ebSSage Weil if (!di->lease_session)
1349355da1ebSSage Weil di->lease_session = ceph_get_mds_session(session);
135052d60f8eSJeff Layton di->lease_gen = atomic_read(&session->s_cap_gen);
1351355da1ebSSage Weil di->lease_seq = le32_to_cpu(lease->seq);
1352355da1ebSSage Weil di->lease_renew_after = half_ttl;
1353355da1ebSSage Weil di->lease_renew_from = 0;
13549b16f03cSMiklos Szeredi di->time = ttl;
135537c4efc1SYan, Zheng
135637c4efc1SYan, Zheng __ceph_dentry_lease_touch(di);
1357543212b3SYan, Zheng }
1358543212b3SYan, Zheng
update_dentry_lease(struct inode * dir,struct dentry * dentry,struct ceph_mds_reply_lease * lease,struct ceph_mds_session * session,unsigned long from_time)1359543212b3SYan, Zheng static inline void update_dentry_lease(struct inode *dir, struct dentry *dentry,
1360543212b3SYan, Zheng struct ceph_mds_reply_lease *lease,
1361543212b3SYan, Zheng struct ceph_mds_session *session,
1362543212b3SYan, Zheng unsigned long from_time)
1363543212b3SYan, Zheng {
1364543212b3SYan, Zheng struct ceph_mds_session *old_lease_session = NULL;
1365543212b3SYan, Zheng spin_lock(&dentry->d_lock);
1366543212b3SYan, Zheng __update_dentry_lease(dir, dentry, lease, session, from_time,
1367543212b3SYan, Zheng &old_lease_session);
1368543212b3SYan, Zheng spin_unlock(&dentry->d_lock);
1369543212b3SYan, Zheng ceph_put_mds_session(old_lease_session);
1370543212b3SYan, Zheng }
1371543212b3SYan, Zheng
1372543212b3SYan, Zheng /*
1373543212b3SYan, Zheng * update dentry lease without having parent inode locked
1374543212b3SYan, Zheng */
update_dentry_lease_careful(struct dentry * dentry,struct ceph_mds_reply_lease * lease,struct ceph_mds_session * session,unsigned long from_time,char * dname,u32 dname_len,struct ceph_vino * pdvino,struct ceph_vino * ptvino)1375543212b3SYan, Zheng static void update_dentry_lease_careful(struct dentry *dentry,
1376543212b3SYan, Zheng struct ceph_mds_reply_lease *lease,
1377543212b3SYan, Zheng struct ceph_mds_session *session,
1378543212b3SYan, Zheng unsigned long from_time,
1379543212b3SYan, Zheng char *dname, u32 dname_len,
1380543212b3SYan, Zheng struct ceph_vino *pdvino,
1381543212b3SYan, Zheng struct ceph_vino *ptvino)
1382543212b3SYan, Zheng
1383543212b3SYan, Zheng {
1384543212b3SYan, Zheng struct inode *dir;
1385543212b3SYan, Zheng struct ceph_mds_session *old_lease_session = NULL;
1386543212b3SYan, Zheng
1387543212b3SYan, Zheng spin_lock(&dentry->d_lock);
1388543212b3SYan, Zheng /* make sure dentry's name matches target */
1389543212b3SYan, Zheng if (dentry->d_name.len != dname_len ||
1390543212b3SYan, Zheng memcmp(dentry->d_name.name, dname, dname_len))
1391543212b3SYan, Zheng goto out_unlock;
1392543212b3SYan, Zheng
1393543212b3SYan, Zheng dir = d_inode(dentry->d_parent);
1394543212b3SYan, Zheng /* make sure parent matches dvino */
1395543212b3SYan, Zheng if (!ceph_ino_compare(dir, pdvino))
1396543212b3SYan, Zheng goto out_unlock;
1397543212b3SYan, Zheng
1398543212b3SYan, Zheng /* make sure dentry's inode matches target. NULL ptvino means that
1399543212b3SYan, Zheng * we expect a negative dentry */
1400543212b3SYan, Zheng if (ptvino) {
1401543212b3SYan, Zheng if (d_really_is_negative(dentry))
1402543212b3SYan, Zheng goto out_unlock;
1403543212b3SYan, Zheng if (!ceph_ino_compare(d_inode(dentry), ptvino))
1404543212b3SYan, Zheng goto out_unlock;
1405543212b3SYan, Zheng } else {
1406543212b3SYan, Zheng if (d_really_is_positive(dentry))
1407543212b3SYan, Zheng goto out_unlock;
1408543212b3SYan, Zheng }
1409543212b3SYan, Zheng
1410543212b3SYan, Zheng __update_dentry_lease(dir, dentry, lease, session,
1411543212b3SYan, Zheng from_time, &old_lease_session);
1412355da1ebSSage Weil out_unlock:
1413355da1ebSSage Weil spin_unlock(&dentry->d_lock);
1414481f001fSYan, Zheng ceph_put_mds_session(old_lease_session);
1415355da1ebSSage Weil }
1416355da1ebSSage Weil
1417355da1ebSSage Weil /*
14181cd3935bSSage Weil * splice a dentry to an inode.
1419810313c5Shongnanli * caller must hold directory i_rwsem for this to be safe.
14201cd3935bSSage Weil */
splice_dentry(struct dentry ** pdn,struct inode * in)14212bf996acSYan, Zheng static int splice_dentry(struct dentry **pdn, struct inode *in)
14221cd3935bSSage Weil {
14232bf996acSYan, Zheng struct dentry *dn = *pdn;
14241cd3935bSSage Weil struct dentry *realdn;
14251cd3935bSSage Weil
14262b0143b5SDavid Howells BUG_ON(d_inode(dn));
14271cd3935bSSage Weil
14285495c2d0SYan, Zheng if (S_ISDIR(in->i_mode)) {
14295495c2d0SYan, Zheng /* If inode is directory, d_splice_alias() below will remove
14305495c2d0SYan, Zheng * 'realdn' from its origin parent. We need to ensure that
14315495c2d0SYan, Zheng * origin parent's readdir cache will not reference 'realdn'
14325495c2d0SYan, Zheng */
14335495c2d0SYan, Zheng realdn = d_find_any_alias(in);
14345495c2d0SYan, Zheng if (realdn) {
14355495c2d0SYan, Zheng struct ceph_dentry_info *di = ceph_dentry(realdn);
14365495c2d0SYan, Zheng spin_lock(&realdn->d_lock);
14375495c2d0SYan, Zheng
14385495c2d0SYan, Zheng realdn->d_op->d_prune(realdn);
14395495c2d0SYan, Zheng
14405495c2d0SYan, Zheng di->time = jiffies;
14415495c2d0SYan, Zheng di->lease_shared_gen = 0;
14425495c2d0SYan, Zheng di->offset = 0;
14435495c2d0SYan, Zheng
14445495c2d0SYan, Zheng spin_unlock(&realdn->d_lock);
14455495c2d0SYan, Zheng dput(realdn);
14465495c2d0SYan, Zheng }
14475495c2d0SYan, Zheng }
14485495c2d0SYan, Zheng
14491cd3935bSSage Weil /* dn must be unhashed */
14501cd3935bSSage Weil if (!d_unhashed(dn))
14511cd3935bSSage Weil d_drop(dn);
145241d28bcaSAl Viro realdn = d_splice_alias(in, dn);
14531cd3935bSSage Weil if (IS_ERR(realdn)) {
1454d69ed05aSSage Weil pr_err("splice_dentry error %ld %p inode %p ino %llx.%llx\n",
1455d69ed05aSSage Weil PTR_ERR(realdn), dn, in, ceph_vinop(in));
14562bf996acSYan, Zheng return PTR_ERR(realdn);
14572bf996acSYan, Zheng }
14582bf996acSYan, Zheng
14592bf996acSYan, Zheng if (realdn) {
14601cd3935bSSage Weil dout("dn %p (%d) spliced with %p (%d) "
14611cd3935bSSage Weil "inode %p ino %llx.%llx\n",
146284d08fa8SAl Viro dn, d_count(dn),
146384d08fa8SAl Viro realdn, d_count(realdn),
14642b0143b5SDavid Howells d_inode(realdn), ceph_vinop(d_inode(realdn)));
14651cd3935bSSage Weil dput(dn);
14662bf996acSYan, Zheng *pdn = realdn;
14671cd3935bSSage Weil } else {
14681cd3935bSSage Weil BUG_ON(!ceph_dentry(dn));
14691cd3935bSSage Weil dout("dn %p attached to %p ino %llx.%llx\n",
14702b0143b5SDavid Howells dn, d_inode(dn), ceph_vinop(d_inode(dn)));
14711cd3935bSSage Weil }
14722bf996acSYan, Zheng return 0;
14731cd3935bSSage Weil }
14741cd3935bSSage Weil
14751cd3935bSSage Weil /*
1476355da1ebSSage Weil * Incorporate results into the local cache. This is either just
1477355da1ebSSage Weil * one inode, or a directory, dentry, and possibly linked-to inode (e.g.,
1478355da1ebSSage Weil * after a lookup).
1479355da1ebSSage Weil *
1480355da1ebSSage Weil * A reply may contain
1481355da1ebSSage Weil * a directory inode along with a dentry.
1482355da1ebSSage Weil * and/or a target inode
1483355da1ebSSage Weil *
1484355da1ebSSage Weil * Called with snap_rwsem (read).
1485355da1ebSSage Weil */
ceph_fill_trace(struct super_block * sb,struct ceph_mds_request * req)1486f5a03b08SJeff Layton int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req)
1487355da1ebSSage Weil {
1488f5a03b08SJeff Layton struct ceph_mds_session *session = req->r_session;
1489355da1ebSSage Weil struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1490355da1ebSSage Weil struct inode *in = NULL;
1491f5d55f03SJeff Layton struct ceph_vino tvino, dvino;
1492*985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
1493355da1ebSSage Weil int err = 0;
1494355da1ebSSage Weil
1495355da1ebSSage Weil dout("fill_trace %p is_dentry %d is_target %d\n", req,
1496355da1ebSSage Weil rinfo->head->is_dentry, rinfo->head->is_target);
1497355da1ebSSage Weil
1498355da1ebSSage Weil if (!rinfo->head->is_target && !rinfo->head->is_dentry) {
1499355da1ebSSage Weil dout("fill_trace reply is empty!\n");
15003dd69aabSJeff Layton if (rinfo->head->result == 0 && req->r_parent)
1501167c9e35SSage Weil ceph_invalidate_dir_request(req);
1502355da1ebSSage Weil return 0;
1503355da1ebSSage Weil }
1504355da1ebSSage Weil
1505355da1ebSSage Weil if (rinfo->head->is_dentry) {
15063dd69aabSJeff Layton struct inode *dir = req->r_parent;
15075b1daecdSSage Weil
15086c5e50faSSage Weil if (dir) {
1509966c7160SJeff Layton err = ceph_fill_inode(dir, NULL, &rinfo->diri,
1510966c7160SJeff Layton rinfo->dirfrag, session, -1,
15115b1daecdSSage Weil &req->r_caps_reservation);
15125b1daecdSSage Weil if (err < 0)
151319913b4eSYan, Zheng goto done;
15146c5e50faSSage Weil } else {
15156c5e50faSSage Weil WARN_ON_ONCE(1);
15166c5e50faSSage Weil }
151719913b4eSYan, Zheng
151874c9e6bfSYan, Zheng if (dir && req->r_op == CEPH_MDS_OP_LOOKUPNAME &&
151974c9e6bfSYan, Zheng test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
152074c9e6bfSYan, Zheng !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
152185529096SJeff Layton bool is_nokey = false;
152219913b4eSYan, Zheng struct qstr dname;
152319913b4eSYan, Zheng struct dentry *dn, *parent;
152485529096SJeff Layton struct fscrypt_str oname = FSTR_INIT(NULL, 0);
152585529096SJeff Layton struct ceph_fname fname = { .dir = dir,
152685529096SJeff Layton .name = rinfo->dname,
152785529096SJeff Layton .ctext = rinfo->altname,
152885529096SJeff Layton .name_len = rinfo->dname_len,
152985529096SJeff Layton .ctext_len = rinfo->altname_len };
153019913b4eSYan, Zheng
153119913b4eSYan, Zheng BUG_ON(!rinfo->head->is_target);
153219913b4eSYan, Zheng BUG_ON(req->r_dentry);
153319913b4eSYan, Zheng
153419913b4eSYan, Zheng parent = d_find_any_alias(dir);
153519913b4eSYan, Zheng BUG_ON(!parent);
153619913b4eSYan, Zheng
153785529096SJeff Layton err = ceph_fname_alloc_buffer(dir, &oname);
153885529096SJeff Layton if (err < 0) {
153985529096SJeff Layton dput(parent);
154085529096SJeff Layton goto done;
154185529096SJeff Layton }
154285529096SJeff Layton
154385529096SJeff Layton err = ceph_fname_to_usr(&fname, NULL, &oname, &is_nokey);
154485529096SJeff Layton if (err < 0) {
154585529096SJeff Layton dput(parent);
154685529096SJeff Layton ceph_fname_free_buffer(dir, &oname);
154785529096SJeff Layton goto done;
154885529096SJeff Layton }
154985529096SJeff Layton dname.name = oname.name;
155085529096SJeff Layton dname.len = oname.len;
15518387ff25SLinus Torvalds dname.hash = full_name_hash(parent, dname.name, dname.len);
1552f5d55f03SJeff Layton tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1553f5d55f03SJeff Layton tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
155419913b4eSYan, Zheng retry_lookup:
155519913b4eSYan, Zheng dn = d_lookup(parent, &dname);
155619913b4eSYan, Zheng dout("d_lookup on parent=%p name=%.*s got %p\n",
155719913b4eSYan, Zheng parent, dname.len, dname.name, dn);
155819913b4eSYan, Zheng
155919913b4eSYan, Zheng if (!dn) {
156019913b4eSYan, Zheng dn = d_alloc(parent, &dname);
156119913b4eSYan, Zheng dout("d_alloc %p '%.*s' = %p\n", parent,
156219913b4eSYan, Zheng dname.len, dname.name, dn);
1563d37b1d99SMarkus Elfring if (!dn) {
156419913b4eSYan, Zheng dput(parent);
156585529096SJeff Layton ceph_fname_free_buffer(dir, &oname);
156619913b4eSYan, Zheng err = -ENOMEM;
156719913b4eSYan, Zheng goto done;
156819913b4eSYan, Zheng }
156985529096SJeff Layton if (is_nokey) {
157085529096SJeff Layton spin_lock(&dn->d_lock);
157185529096SJeff Layton dn->d_flags |= DCACHE_NOKEY_NAME;
157285529096SJeff Layton spin_unlock(&dn->d_lock);
157385529096SJeff Layton }
1574ad5cb123SAl Viro err = 0;
15752b0143b5SDavid Howells } else if (d_really_is_positive(dn) &&
1576f5d55f03SJeff Layton (ceph_ino(d_inode(dn)) != tvino.ino ||
1577f5d55f03SJeff Layton ceph_snap(d_inode(dn)) != tvino.snap)) {
157819913b4eSYan, Zheng dout(" dn %p points to wrong inode %p\n",
15792b0143b5SDavid Howells dn, d_inode(dn));
1580933ad2c9SYan, Zheng ceph_dir_clear_ordered(dir);
158119913b4eSYan, Zheng d_delete(dn);
158219913b4eSYan, Zheng dput(dn);
158319913b4eSYan, Zheng goto retry_lookup;
158419913b4eSYan, Zheng }
158585529096SJeff Layton ceph_fname_free_buffer(dir, &oname);
158619913b4eSYan, Zheng
158719913b4eSYan, Zheng req->r_dentry = dn;
158819913b4eSYan, Zheng dput(parent);
158919913b4eSYan, Zheng }
15905b1daecdSSage Weil }
15915b1daecdSSage Weil
159286b58d13SYan, Zheng if (rinfo->head->is_target) {
1593bca9fc14SJeff Layton /* Should be filled in by handle_reply */
1594bca9fc14SJeff Layton BUG_ON(!req->r_target_inode);
159586b58d13SYan, Zheng
1596bca9fc14SJeff Layton in = req->r_target_inode;
1597966c7160SJeff Layton err = ceph_fill_inode(in, req->r_locked_page, &rinfo->targeti,
1598966c7160SJeff Layton NULL, session,
1599bc2de10dSJeff Layton (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
16003bb48b41SJeff Layton !test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) &&
1601bc2de10dSJeff Layton rinfo->head->result == 0) ? req->r_fmode : -1,
160286b58d13SYan, Zheng &req->r_caps_reservation);
160386b58d13SYan, Zheng if (err < 0) {
1604966c7160SJeff Layton pr_err("ceph_fill_inode badness %p %llx.%llx\n",
160586b58d13SYan, Zheng in, ceph_vinop(in));
1606bca9fc14SJeff Layton req->r_target_inode = NULL;
1607893e456bSJeff Layton if (in->i_state & I_NEW)
1608893e456bSJeff Layton discard_new_inode(in);
160968cbb805SJeff Layton else
161068cbb805SJeff Layton iput(in);
161186b58d13SYan, Zheng goto done;
161286b58d13SYan, Zheng }
1613893e456bSJeff Layton if (in->i_state & I_NEW)
1614893e456bSJeff Layton unlock_new_inode(in);
161586b58d13SYan, Zheng }
161686b58d13SYan, Zheng
16179358c6d4SSage Weil /*
16189358c6d4SSage Weil * ignore null lease/binding on snapdir ENOENT, or else we
16199358c6d4SSage Weil * will have trouble splicing in the virtual snapdir later
16209358c6d4SSage Weil */
16213dd69aabSJeff Layton if (rinfo->head->is_dentry &&
1622bc2de10dSJeff Layton !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags) &&
16233dd69aabSJeff Layton test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
16249358c6d4SSage Weil (rinfo->head->is_target || strncmp(req->r_dentry->d_name.name,
16253d14c5d2SYehuda Sadeh fsc->mount_options->snapdir_name,
16269358c6d4SSage Weil req->r_dentry->d_name.len))) {
1627355da1ebSSage Weil /*
1628355da1ebSSage Weil * lookup link rename : null -> possibly existing inode
1629355da1ebSSage Weil * mknod symlink mkdir : null -> new inode
1630355da1ebSSage Weil * unlink : linked -> null
1631355da1ebSSage Weil */
16323dd69aabSJeff Layton struct inode *dir = req->r_parent;
1633355da1ebSSage Weil struct dentry *dn = req->r_dentry;
1634355da1ebSSage Weil bool have_dir_cap, have_lease;
1635355da1ebSSage Weil
1636355da1ebSSage Weil BUG_ON(!dn);
1637355da1ebSSage Weil BUG_ON(!dir);
16382b0143b5SDavid Howells BUG_ON(d_inode(dn->d_parent) != dir);
1639f5d55f03SJeff Layton
1640f5d55f03SJeff Layton dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1641f5d55f03SJeff Layton dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1642f5d55f03SJeff Layton
1643f5d55f03SJeff Layton BUG_ON(ceph_ino(dir) != dvino.ino);
1644f5d55f03SJeff Layton BUG_ON(ceph_snap(dir) != dvino.snap);
1645355da1ebSSage Weil
1646355da1ebSSage Weil /* do we have a lease on the whole dir? */
1647355da1ebSSage Weil have_dir_cap =
1648355da1ebSSage Weil (le32_to_cpu(rinfo->diri.in->cap.caps) &
1649355da1ebSSage Weil CEPH_CAP_FILE_SHARED);
1650355da1ebSSage Weil
1651355da1ebSSage Weil /* do we have a dn lease? */
1652355da1ebSSage Weil have_lease = have_dir_cap ||
16532f90b852SSage Weil le32_to_cpu(rinfo->dlease->duration_ms);
1654355da1ebSSage Weil if (!have_lease)
1655355da1ebSSage Weil dout("fill_trace no dentry lease or dir cap\n");
1656355da1ebSSage Weil
1657355da1ebSSage Weil /* rename? */
1658355da1ebSSage Weil if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) {
16590a8a70f9SYan, Zheng struct inode *olddir = req->r_old_dentry_dir;
16600a8a70f9SYan, Zheng BUG_ON(!olddir);
16610a8a70f9SYan, Zheng
1662a455589fSAl Viro dout(" src %p '%pd' dst %p '%pd'\n",
1663355da1ebSSage Weil req->r_old_dentry,
1664a455589fSAl Viro req->r_old_dentry,
1665a455589fSAl Viro dn, dn);
1666355da1ebSSage Weil dout("fill_trace doing d_move %p -> %p\n",
1667355da1ebSSage Weil req->r_old_dentry, dn);
1668c10f5e12SSage Weil
1669fdd4e158SYan, Zheng /* d_move screws up sibling dentries' offsets */
1670fdd4e158SYan, Zheng ceph_dir_clear_ordered(dir);
1671fdd4e158SYan, Zheng ceph_dir_clear_ordered(olddir);
1672fdd4e158SYan, Zheng
1673355da1ebSSage Weil d_move(req->r_old_dentry, dn);
1674a455589fSAl Viro dout(" src %p '%pd' dst %p '%pd'\n",
1675355da1ebSSage Weil req->r_old_dentry,
1676a455589fSAl Viro req->r_old_dentry,
1677a455589fSAl Viro dn, dn);
167881a6cf2dSSage Weil
1679c4a29f26SSage Weil /* ensure target dentry is invalidated, despite
1680c4a29f26SSage Weil rehashing bug in vfs_rename_dir */
168181a6cf2dSSage Weil ceph_invalidate_dentry_lease(dn);
168281a6cf2dSSage Weil
168309adc80cSSage Weil dout("dn %p gets new offset %lld\n", req->r_old_dentry,
16841cd3935bSSage Weil ceph_dentry(req->r_old_dentry)->offset);
168581a6cf2dSSage Weil
16862bf996acSYan, Zheng /* swap r_dentry and r_old_dentry in case that
16872bf996acSYan, Zheng * splice_dentry() gets called later. This is safe
16882bf996acSYan, Zheng * because no other place will use them */
16892bf996acSYan, Zheng req->r_dentry = req->r_old_dentry;
16902bf996acSYan, Zheng req->r_old_dentry = dn;
16912bf996acSYan, Zheng dn = req->r_dentry;
1692355da1ebSSage Weil }
1693355da1ebSSage Weil
1694355da1ebSSage Weil /* null dentry? */
1695355da1ebSSage Weil if (!rinfo->head->is_target) {
1696355da1ebSSage Weil dout("fill_trace null dentry\n");
16972b0143b5SDavid Howells if (d_really_is_positive(dn)) {
1698355da1ebSSage Weil dout("d_delete %p\n", dn);
16995495c2d0SYan, Zheng ceph_dir_clear_ordered(dir);
1700355da1ebSSage Weil d_delete(dn);
170180d025ffSJeff Layton } else if (have_lease) {
170280d025ffSJeff Layton if (d_unhashed(dn))
1703f8b31710SAl Viro d_add(dn, NULL);
17047ffe4fceSXiubo Li }
17057ffe4fceSXiubo Li
17067ffe4fceSXiubo Li if (!d_unhashed(dn) && have_lease)
1707543212b3SYan, Zheng update_dentry_lease(dir, dn,
1708543212b3SYan, Zheng rinfo->dlease, session,
1709543212b3SYan, Zheng req->r_request_started);
1710355da1ebSSage Weil goto done;
1711355da1ebSSage Weil }
1712355da1ebSSage Weil
1713355da1ebSSage Weil /* attach proper inode */
17142b0143b5SDavid Howells if (d_really_is_negative(dn)) {
171570db4f36SYan, Zheng ceph_dir_clear_ordered(dir);
171686b58d13SYan, Zheng ihold(in);
17172bf996acSYan, Zheng err = splice_dentry(&req->r_dentry, in);
17182bf996acSYan, Zheng if (err < 0)
1719355da1ebSSage Weil goto done;
17202bf996acSYan, Zheng dn = req->r_dentry; /* may have spliced */
17212b0143b5SDavid Howells } else if (d_really_is_positive(dn) && d_inode(dn) != in) {
1722355da1ebSSage Weil dout(" %p links to %p %llx.%llx, not %llx.%llx\n",
17232b0143b5SDavid Howells dn, d_inode(dn), ceph_vinop(d_inode(dn)),
172486b58d13SYan, Zheng ceph_vinop(in));
1725200fd27cSYan, Zheng d_invalidate(dn);
1726355da1ebSSage Weil have_lease = false;
1727355da1ebSSage Weil }
1728355da1ebSSage Weil
1729f5d55f03SJeff Layton if (have_lease) {
1730543212b3SYan, Zheng update_dentry_lease(dir, dn,
1731543212b3SYan, Zheng rinfo->dlease, session,
1732543212b3SYan, Zheng req->r_request_started);
1733f5d55f03SJeff Layton }
1734355da1ebSSage Weil dout(" final dn %p\n", dn);
1735bc2de10dSJeff Layton } else if ((req->r_op == CEPH_MDS_OP_LOOKUPSNAP ||
1736bc2de10dSJeff Layton req->r_op == CEPH_MDS_OP_MKSNAP) &&
17371f08529cSAl Viro test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags) &&
1738bc2de10dSJeff Layton !test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
17393dd69aabSJeff Layton struct inode *dir = req->r_parent;
1740355da1ebSSage Weil
1741355da1ebSSage Weil /* fill out a snapdir LOOKUPSNAP dentry */
17420a8a70f9SYan, Zheng BUG_ON(!dir);
17430a8a70f9SYan, Zheng BUG_ON(ceph_snap(dir) != CEPH_SNAPDIR);
17442bf996acSYan, Zheng BUG_ON(!req->r_dentry);
17452bf996acSYan, Zheng dout(" linking snapped dir %p to dn %p\n", in, req->r_dentry);
174670db4f36SYan, Zheng ceph_dir_clear_ordered(dir);
174786b58d13SYan, Zheng ihold(in);
17482bf996acSYan, Zheng err = splice_dentry(&req->r_dentry, in);
17492bf996acSYan, Zheng if (err < 0)
1750355da1ebSSage Weil goto done;
1751543212b3SYan, Zheng } else if (rinfo->head->is_dentry && req->r_dentry) {
1752543212b3SYan, Zheng /* parent inode is not locked, be carefull */
1753cdde7c43SJeff Layton struct ceph_vino *ptvino = NULL;
1754cdde7c43SJeff Layton dvino.ino = le64_to_cpu(rinfo->diri.in->ino);
1755cdde7c43SJeff Layton dvino.snap = le64_to_cpu(rinfo->diri.in->snapid);
1756cdde7c43SJeff Layton if (rinfo->head->is_target) {
1757cdde7c43SJeff Layton tvino.ino = le64_to_cpu(rinfo->targeti.in->ino);
1758cdde7c43SJeff Layton tvino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
1759cdde7c43SJeff Layton ptvino = &tvino;
1760cdde7c43SJeff Layton }
1761543212b3SYan, Zheng update_dentry_lease_careful(req->r_dentry, rinfo->dlease,
1762543212b3SYan, Zheng session, req->r_request_started,
1763543212b3SYan, Zheng rinfo->dname, rinfo->dname_len,
1764543212b3SYan, Zheng &dvino, ptvino);
1765355da1ebSSage Weil }
1766355da1ebSSage Weil done:
1767355da1ebSSage Weil dout("fill_trace done err=%d\n", err);
1768355da1ebSSage Weil return err;
1769355da1ebSSage Weil }
1770355da1ebSSage Weil
1771355da1ebSSage Weil /*
1772355da1ebSSage Weil * Prepopulate our cache with readdir results, leases, etc.
1773355da1ebSSage Weil */
readdir_prepopulate_inodes_only(struct ceph_mds_request * req,struct ceph_mds_session * session)177479f9f99aSSage Weil static int readdir_prepopulate_inodes_only(struct ceph_mds_request *req,
177579f9f99aSSage Weil struct ceph_mds_session *session)
177679f9f99aSSage Weil {
177779f9f99aSSage Weil struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
177879f9f99aSSage Weil int i, err = 0;
177979f9f99aSSage Weil
178079f9f99aSSage Weil for (i = 0; i < rinfo->dir_nr; i++) {
17812a5beea3SYan, Zheng struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
178279f9f99aSSage Weil struct ceph_vino vino;
178379f9f99aSSage Weil struct inode *in;
178479f9f99aSSage Weil int rc;
178579f9f99aSSage Weil
17862a5beea3SYan, Zheng vino.ino = le64_to_cpu(rde->inode.in->ino);
17872a5beea3SYan, Zheng vino.snap = le64_to_cpu(rde->inode.in->snapid);
178879f9f99aSSage Weil
1789ec9595c0SJeff Layton in = ceph_get_inode(req->r_dentry->d_sb, vino, NULL);
179079f9f99aSSage Weil if (IS_ERR(in)) {
179179f9f99aSSage Weil err = PTR_ERR(in);
179279f9f99aSSage Weil dout("new_inode badness got %d\n", err);
179379f9f99aSSage Weil continue;
179479f9f99aSSage Weil }
1795966c7160SJeff Layton rc = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
179657c21994SJeff Layton -1, &req->r_caps_reservation);
179779f9f99aSSage Weil if (rc < 0) {
1798966c7160SJeff Layton pr_err("ceph_fill_inode badness on %p got %d\n",
1799966c7160SJeff Layton in, rc);
180079f9f99aSSage Weil err = rc;
1801893e456bSJeff Layton if (in->i_state & I_NEW) {
1802893e456bSJeff Layton ihold(in);
1803893e456bSJeff Layton discard_new_inode(in);
180479f9f99aSSage Weil }
1805893e456bSJeff Layton } else if (in->i_state & I_NEW) {
1806893e456bSJeff Layton unlock_new_inode(in);
1807893e456bSJeff Layton }
1808893e456bSJeff Layton
180923c2c76eSJeff Layton iput(in);
181079f9f99aSSage Weil }
181179f9f99aSSage Weil
181279f9f99aSSage Weil return err;
181379f9f99aSSage Weil }
181479f9f99aSSage Weil
ceph_readdir_cache_release(struct ceph_readdir_cache_control * ctl)1815fdd4e158SYan, Zheng void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl)
1816fdd4e158SYan, Zheng {
1817fdd4e158SYan, Zheng if (ctl->page) {
1818fdd4e158SYan, Zheng kunmap(ctl->page);
181909cbfeafSKirill A. Shutemov put_page(ctl->page);
1820fdd4e158SYan, Zheng ctl->page = NULL;
1821fdd4e158SYan, Zheng }
1822fdd4e158SYan, Zheng }
1823fdd4e158SYan, Zheng
fill_readdir_cache(struct inode * dir,struct dentry * dn,struct ceph_readdir_cache_control * ctl,struct ceph_mds_request * req)1824fdd4e158SYan, Zheng static int fill_readdir_cache(struct inode *dir, struct dentry *dn,
1825fdd4e158SYan, Zheng struct ceph_readdir_cache_control *ctl,
1826fdd4e158SYan, Zheng struct ceph_mds_request *req)
1827fdd4e158SYan, Zheng {
1828fdd4e158SYan, Zheng struct ceph_inode_info *ci = ceph_inode(dir);
182909cbfeafSKirill A. Shutemov unsigned nsize = PAGE_SIZE / sizeof(struct dentry*);
1830fdd4e158SYan, Zheng unsigned idx = ctl->index % nsize;
1831fdd4e158SYan, Zheng pgoff_t pgoff = ctl->index / nsize;
1832fdd4e158SYan, Zheng
1833fdd4e158SYan, Zheng if (!ctl->page || pgoff != page_index(ctl->page)) {
1834fdd4e158SYan, Zheng ceph_readdir_cache_release(ctl);
1835af5e5eb5SYan, Zheng if (idx == 0)
1836fdd4e158SYan, Zheng ctl->page = grab_cache_page(&dir->i_data, pgoff);
1837af5e5eb5SYan, Zheng else
1838af5e5eb5SYan, Zheng ctl->page = find_lock_page(&dir->i_data, pgoff);
1839fdd4e158SYan, Zheng if (!ctl->page) {
1840fdd4e158SYan, Zheng ctl->index = -1;
1841af5e5eb5SYan, Zheng return idx == 0 ? -ENOMEM : 0;
1842fdd4e158SYan, Zheng }
1843fdd4e158SYan, Zheng /* reading/filling the cache are serialized by
1844810313c5Shongnanli * i_rwsem, no need to use page lock */
1845fdd4e158SYan, Zheng unlock_page(ctl->page);
1846fdd4e158SYan, Zheng ctl->dentries = kmap(ctl->page);
1847af5e5eb5SYan, Zheng if (idx == 0)
184809cbfeafSKirill A. Shutemov memset(ctl->dentries, 0, PAGE_SIZE);
1849fdd4e158SYan, Zheng }
1850fdd4e158SYan, Zheng
1851fdd4e158SYan, Zheng if (req->r_dir_release_cnt == atomic64_read(&ci->i_release_count) &&
1852fdd4e158SYan, Zheng req->r_dir_ordered_cnt == atomic64_read(&ci->i_ordered_count)) {
1853fdd4e158SYan, Zheng dout("readdir cache dn %p idx %d\n", dn, ctl->index);
1854fdd4e158SYan, Zheng ctl->dentries[idx] = dn;
1855fdd4e158SYan, Zheng ctl->index++;
1856fdd4e158SYan, Zheng } else {
1857fdd4e158SYan, Zheng dout("disable readdir cache\n");
1858fdd4e158SYan, Zheng ctl->index = -1;
1859fdd4e158SYan, Zheng }
1860fdd4e158SYan, Zheng return 0;
1861fdd4e158SYan, Zheng }
1862fdd4e158SYan, Zheng
ceph_readdir_prepopulate(struct ceph_mds_request * req,struct ceph_mds_session * session)1863355da1ebSSage Weil int ceph_readdir_prepopulate(struct ceph_mds_request *req,
1864355da1ebSSage Weil struct ceph_mds_session *session)
1865355da1ebSSage Weil {
1866355da1ebSSage Weil struct dentry *parent = req->r_dentry;
1867af9ffa6dSXiubo Li struct inode *inode = d_inode(parent);
1868af9ffa6dSXiubo Li struct ceph_inode_info *ci = ceph_inode(inode);
1869355da1ebSSage Weil struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
1870355da1ebSSage Weil struct qstr dname;
1871355da1ebSSage Weil struct dentry *dn;
1872355da1ebSSage Weil struct inode *in;
1873315f2408SYan, Zheng int err = 0, skipped = 0, ret, i;
18740f51a983SJeff Layton u32 frag = le32_to_cpu(req->r_args.readdir.frag);
1875f3c4ebe6SYan, Zheng u32 last_hash = 0;
1876f3c4ebe6SYan, Zheng u32 fpos_offset;
1877fdd4e158SYan, Zheng struct ceph_readdir_cache_control cache_ctl = {};
1878fdd4e158SYan, Zheng
1879bc2de10dSJeff Layton if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
1880fdd4e158SYan, Zheng return readdir_prepopulate_inodes_only(req, session);
188181c6aea5SYan, Zheng
188279162547SYan, Zheng if (rinfo->hash_order) {
188379162547SYan, Zheng if (req->r_path2) {
1884f3c4ebe6SYan, Zheng last_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
188579162547SYan, Zheng req->r_path2,
188679162547SYan, Zheng strlen(req->r_path2));
1887f3c4ebe6SYan, Zheng last_hash = ceph_frag_value(last_hash);
188879162547SYan, Zheng } else if (rinfo->offset_hash) {
188979162547SYan, Zheng /* mds understands offset_hash */
189079162547SYan, Zheng WARN_ON_ONCE(req->r_readdir_offset != 2);
18910f51a983SJeff Layton last_hash = le32_to_cpu(req->r_args.readdir.offset_hash);
189279162547SYan, Zheng }
1893f3c4ebe6SYan, Zheng }
1894f3c4ebe6SYan, Zheng
189581c6aea5SYan, Zheng if (rinfo->dir_dir &&
189681c6aea5SYan, Zheng le32_to_cpu(rinfo->dir_dir->frag) != frag) {
189781c6aea5SYan, Zheng dout("readdir_prepopulate got new frag %x -> %x\n",
189881c6aea5SYan, Zheng frag, le32_to_cpu(rinfo->dir_dir->frag));
189981c6aea5SYan, Zheng frag = le32_to_cpu(rinfo->dir_dir->frag);
1900f3c4ebe6SYan, Zheng if (!rinfo->hash_order)
1901fdd4e158SYan, Zheng req->r_readdir_offset = 2;
190281c6aea5SYan, Zheng }
1903355da1ebSSage Weil
1904355da1ebSSage Weil if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) {
1905355da1ebSSage Weil dout("readdir_prepopulate %d items under SNAPDIR dn %p\n",
1906355da1ebSSage Weil rinfo->dir_nr, parent);
1907355da1ebSSage Weil } else {
1908355da1ebSSage Weil dout("readdir_prepopulate %d items under dn %p\n",
1909355da1ebSSage Weil rinfo->dir_nr, parent);
1910355da1ebSSage Weil if (rinfo->dir_dir)
19112b0143b5SDavid Howells ceph_fill_dirfrag(d_inode(parent), rinfo->dir_dir);
1912355da1ebSSage Weil
19138d45b911SYan, Zheng if (ceph_frag_is_leftmost(frag) &&
19148d45b911SYan, Zheng req->r_readdir_offset == 2 &&
191579162547SYan, Zheng !(rinfo->hash_order && last_hash)) {
19168d45b911SYan, Zheng /* note dir version at start of readdir so we can
19178d45b911SYan, Zheng * tell if any dentries get dropped */
19188d45b911SYan, Zheng req->r_dir_release_cnt =
19198d45b911SYan, Zheng atomic64_read(&ci->i_release_count);
19208d45b911SYan, Zheng req->r_dir_ordered_cnt =
19218d45b911SYan, Zheng atomic64_read(&ci->i_ordered_count);
1922fdd4e158SYan, Zheng req->r_readdir_cache_idx = 0;
1923fdd4e158SYan, Zheng }
19248d45b911SYan, Zheng }
1925fdd4e158SYan, Zheng
1926fdd4e158SYan, Zheng cache_ctl.index = req->r_readdir_cache_idx;
1927f3c4ebe6SYan, Zheng fpos_offset = req->r_readdir_offset;
1928fdd4e158SYan, Zheng
192986b58d13SYan, Zheng /* FIXME: release caps/leases if error occurs */
1930355da1ebSSage Weil for (i = 0; i < rinfo->dir_nr; i++) {
19312a5beea3SYan, Zheng struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i;
1932543212b3SYan, Zheng struct ceph_vino tvino;
1933355da1ebSSage Weil
19342a5beea3SYan, Zheng dname.name = rde->name;
19352a5beea3SYan, Zheng dname.len = rde->name_len;
19368387ff25SLinus Torvalds dname.hash = full_name_hash(parent, dname.name, dname.len);
1937355da1ebSSage Weil
1938f5d55f03SJeff Layton tvino.ino = le64_to_cpu(rde->inode.in->ino);
1939f5d55f03SJeff Layton tvino.snap = le64_to_cpu(rde->inode.in->snapid);
1940355da1ebSSage Weil
1941f3c4ebe6SYan, Zheng if (rinfo->hash_order) {
1942af9ffa6dSXiubo Li u32 hash = ceph_frag_value(rde->raw_hash);
1943f3c4ebe6SYan, Zheng if (hash != last_hash)
1944f3c4ebe6SYan, Zheng fpos_offset = 2;
1945f3c4ebe6SYan, Zheng last_hash = hash;
1946f3c4ebe6SYan, Zheng rde->offset = ceph_make_fpos(hash, fpos_offset++, true);
1947f3c4ebe6SYan, Zheng } else {
1948f3c4ebe6SYan, Zheng rde->offset = ceph_make_fpos(frag, fpos_offset++, false);
1949f3c4ebe6SYan, Zheng }
1950355da1ebSSage Weil
1951355da1ebSSage Weil retry_lookup:
1952355da1ebSSage Weil dn = d_lookup(parent, &dname);
1953355da1ebSSage Weil dout("d_lookup on parent=%p name=%.*s got %p\n",
1954355da1ebSSage Weil parent, dname.len, dname.name, dn);
1955355da1ebSSage Weil
1956355da1ebSSage Weil if (!dn) {
1957355da1ebSSage Weil dn = d_alloc(parent, &dname);
1958355da1ebSSage Weil dout("d_alloc %p '%.*s' = %p\n", parent,
1959355da1ebSSage Weil dname.len, dname.name, dn);
1960d37b1d99SMarkus Elfring if (!dn) {
1961355da1ebSSage Weil dout("d_alloc badness\n");
1962355da1ebSSage Weil err = -ENOMEM;
1963355da1ebSSage Weil goto out;
1964355da1ebSSage Weil }
1965af9ffa6dSXiubo Li if (rde->is_nokey) {
1966af9ffa6dSXiubo Li spin_lock(&dn->d_lock);
1967af9ffa6dSXiubo Li dn->d_flags |= DCACHE_NOKEY_NAME;
1968af9ffa6dSXiubo Li spin_unlock(&dn->d_lock);
1969af9ffa6dSXiubo Li }
19702b0143b5SDavid Howells } else if (d_really_is_positive(dn) &&
1971f5d55f03SJeff Layton (ceph_ino(d_inode(dn)) != tvino.ino ||
1972f5d55f03SJeff Layton ceph_snap(d_inode(dn)) != tvino.snap)) {
19735495c2d0SYan, Zheng struct ceph_dentry_info *di = ceph_dentry(dn);
1974355da1ebSSage Weil dout(" dn %p points to wrong inode %p\n",
19752b0143b5SDavid Howells dn, d_inode(dn));
19765495c2d0SYan, Zheng
19775495c2d0SYan, Zheng spin_lock(&dn->d_lock);
19785495c2d0SYan, Zheng if (di->offset > 0 &&
19795495c2d0SYan, Zheng di->lease_shared_gen ==
19805495c2d0SYan, Zheng atomic_read(&ci->i_shared_gen)) {
1981933ad2c9SYan, Zheng __ceph_dir_clear_ordered(ci);
19825495c2d0SYan, Zheng di->offset = 0;
19835495c2d0SYan, Zheng }
19845495c2d0SYan, Zheng spin_unlock(&dn->d_lock);
19855495c2d0SYan, Zheng
1986355da1ebSSage Weil d_delete(dn);
1987355da1ebSSage Weil dput(dn);
1988355da1ebSSage Weil goto retry_lookup;
1989355da1ebSSage Weil }
1990355da1ebSSage Weil
1991355da1ebSSage Weil /* inode */
19922b0143b5SDavid Howells if (d_really_is_positive(dn)) {
19932b0143b5SDavid Howells in = d_inode(dn);
1994355da1ebSSage Weil } else {
1995ec9595c0SJeff Layton in = ceph_get_inode(parent->d_sb, tvino, NULL);
1996ac1f12efSDan Carpenter if (IS_ERR(in)) {
1997355da1ebSSage Weil dout("new_inode badness\n");
19982744c171SAl Viro d_drop(dn);
1999355da1ebSSage Weil dput(dn);
2000ac1f12efSDan Carpenter err = PTR_ERR(in);
2001355da1ebSSage Weil goto out;
2002355da1ebSSage Weil }
2003355da1ebSSage Weil }
2004355da1ebSSage Weil
2005966c7160SJeff Layton ret = ceph_fill_inode(in, NULL, &rde->inode, NULL, session,
200657c21994SJeff Layton -1, &req->r_caps_reservation);
2007fdd4e158SYan, Zheng if (ret < 0) {
2008966c7160SJeff Layton pr_err("ceph_fill_inode badness on %p\n", in);
20093e1d0452SYan, Zheng if (d_really_is_negative(dn)) {
2010893e456bSJeff Layton if (in->i_state & I_NEW) {
2011893e456bSJeff Layton ihold(in);
2012893e456bSJeff Layton discard_new_inode(in);
2013893e456bSJeff Layton }
201423c2c76eSJeff Layton iput(in);
20153e1d0452SYan, Zheng }
201686b58d13SYan, Zheng d_drop(dn);
2017fdd4e158SYan, Zheng err = ret;
2018d69ed05aSSage Weil goto next_item;
2019355da1ebSSage Weil }
2020893e456bSJeff Layton if (in->i_state & I_NEW)
2021893e456bSJeff Layton unlock_new_inode(in);
202286b58d13SYan, Zheng
20232b0143b5SDavid Howells if (d_really_is_negative(dn)) {
2024315f2408SYan, Zheng if (ceph_security_xattr_deadlock(in)) {
2025315f2408SYan, Zheng dout(" skip splicing dn %p to inode %p"
2026315f2408SYan, Zheng " (security xattr deadlock)\n", dn, in);
202723c2c76eSJeff Layton iput(in);
2028315f2408SYan, Zheng skipped++;
2029315f2408SYan, Zheng goto next_item;
2030315f2408SYan, Zheng }
2031315f2408SYan, Zheng
20322bf996acSYan, Zheng err = splice_dentry(&dn, in);
20332bf996acSYan, Zheng if (err < 0)
203486b58d13SYan, Zheng goto next_item;
203586b58d13SYan, Zheng }
203686b58d13SYan, Zheng
2037f3c4ebe6SYan, Zheng ceph_dentry(dn)->offset = rde->offset;
203886b58d13SYan, Zheng
2039543212b3SYan, Zheng update_dentry_lease(d_inode(parent), dn,
2040543212b3SYan, Zheng rde->lease, req->r_session,
2041543212b3SYan, Zheng req->r_request_started);
2042fdd4e158SYan, Zheng
2043315f2408SYan, Zheng if (err == 0 && skipped == 0 && cache_ctl.index >= 0) {
2044fdd4e158SYan, Zheng ret = fill_readdir_cache(d_inode(parent), dn,
2045fdd4e158SYan, Zheng &cache_ctl, req);
2046fdd4e158SYan, Zheng if (ret < 0)
2047fdd4e158SYan, Zheng err = ret;
2048fdd4e158SYan, Zheng }
2049d69ed05aSSage Weil next_item:
2050355da1ebSSage Weil dput(dn);
2051355da1ebSSage Weil }
2052355da1ebSSage Weil out:
2053315f2408SYan, Zheng if (err == 0 && skipped == 0) {
2054bc2de10dSJeff Layton set_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags);
2055fdd4e158SYan, Zheng req->r_readdir_cache_idx = cache_ctl.index;
2056fdd4e158SYan, Zheng }
2057fdd4e158SYan, Zheng ceph_readdir_cache_release(&cache_ctl);
2058355da1ebSSage Weil dout("readdir_prepopulate done\n");
2059355da1ebSSage Weil return err;
2060355da1ebSSage Weil }
2061355da1ebSSage Weil
ceph_inode_set_size(struct inode * inode,loff_t size)2062efb0ca76SYan, Zheng bool ceph_inode_set_size(struct inode *inode, loff_t size)
2063355da1ebSSage Weil {
2064355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
2065efb0ca76SYan, Zheng bool ret;
2066355da1ebSSage Weil
2067be655596SSage Weil spin_lock(&ci->i_ceph_lock);
20682d6795fbSJeff Layton dout("set_size %p %llu -> %llu\n", inode, i_size_read(inode), size);
206999c88e69SYan, Zheng i_size_write(inode, size);
2070400e1286SJeff Layton ceph_fscache_update(inode);
2071224a7542SYan, Zheng inode->i_blocks = calc_inode_blocks(size);
2072355da1ebSSage Weil
2073efb0ca76SYan, Zheng ret = __ceph_should_report_size(ci);
2074355da1ebSSage Weil
2075be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2076400e1286SJeff Layton
2077355da1ebSSage Weil return ret;
2078355da1ebSSage Weil }
2079355da1ebSSage Weil
ceph_queue_inode_work(struct inode * inode,int work_bit)208064f28c62SJeff Layton void ceph_queue_inode_work(struct inode *inode, int work_bit)
20813c6f6b79SSage Weil {
2082*985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
20831cf89a8dSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
208464f28c62SJeff Layton set_bit(work_bit, &ci->i_work_mask);
20851cf89a8dSYan, Zheng
208615a2015fSSage Weil ihold(inode);
208764f28c62SJeff Layton if (queue_work(fsc->inode_wq, &ci->i_work)) {
208864f28c62SJeff Layton dout("queue_inode_work %p, mask=%lx\n", inode, ci->i_work_mask);
20893c6f6b79SSage Weil } else {
209064f28c62SJeff Layton dout("queue_inode_work %p already queued, mask=%lx\n",
20911cf89a8dSYan, Zheng inode, ci->i_work_mask);
20921cf89a8dSYan, Zheng iput(inode);
20931cf89a8dSYan, Zheng }
20941cf89a8dSYan, Zheng }
20951cf89a8dSYan, Zheng
ceph_do_invalidate_pages(struct inode * inode)20961cf89a8dSYan, Zheng static void ceph_do_invalidate_pages(struct inode *inode)
20971cf89a8dSYan, Zheng {
20981cf89a8dSYan, Zheng struct ceph_inode_info *ci = ceph_inode(inode);
2099355da1ebSSage Weil u32 orig_gen;
2100355da1ebSSage Weil int check = 0;
2101355da1ebSSage Weil
2102400e1286SJeff Layton ceph_fscache_invalidate(inode, false);
2103400e1286SJeff Layton
2104b0d7c223SYan, Zheng mutex_lock(&ci->i_truncate_mutex);
21056c93df5dSYan, Zheng
21065d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode)) {
21076407fbb9SJeff Layton pr_warn_ratelimited("%s: inode %llx.%llx is shut down\n",
21086407fbb9SJeff Layton __func__, ceph_vinop(inode));
21096c93df5dSYan, Zheng mapping_set_error(inode->i_mapping, -EIO);
21106c93df5dSYan, Zheng truncate_pagecache(inode, 0);
21116c93df5dSYan, Zheng mutex_unlock(&ci->i_truncate_mutex);
21126c93df5dSYan, Zheng goto out;
21136c93df5dSYan, Zheng }
21146c93df5dSYan, Zheng
2115be655596SSage Weil spin_lock(&ci->i_ceph_lock);
2116355da1ebSSage Weil dout("invalidate_pages %p gen %d revoking %d\n", inode,
2117355da1ebSSage Weil ci->i_rdcache_gen, ci->i_rdcache_revoking);
2118cd045cb4SSage Weil if (ci->i_rdcache_revoking != ci->i_rdcache_gen) {
21199563f88cSYan, Zheng if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
21209563f88cSYan, Zheng check = 1;
2121be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2122b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex);
2123355da1ebSSage Weil goto out;
2124355da1ebSSage Weil }
2125355da1ebSSage Weil orig_gen = ci->i_rdcache_gen;
2126be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2127355da1ebSSage Weil
21289abd4db7SYan, Zheng if (invalidate_inode_pages2(inode->i_mapping) < 0) {
21296407fbb9SJeff Layton pr_err("invalidate_inode_pages2 %llx.%llx failed\n",
21306407fbb9SJeff Layton ceph_vinop(inode));
21319abd4db7SYan, Zheng }
2132355da1ebSSage Weil
2133be655596SSage Weil spin_lock(&ci->i_ceph_lock);
2134cd045cb4SSage Weil if (orig_gen == ci->i_rdcache_gen &&
2135cd045cb4SSage Weil orig_gen == ci->i_rdcache_revoking) {
2136355da1ebSSage Weil dout("invalidate_pages %p gen %d successful\n", inode,
2137355da1ebSSage Weil ci->i_rdcache_gen);
2138cd045cb4SSage Weil ci->i_rdcache_revoking--;
2139355da1ebSSage Weil check = 1;
2140355da1ebSSage Weil } else {
2141cd045cb4SSage Weil dout("invalidate_pages %p gen %d raced, now %d revoking %d\n",
2142cd045cb4SSage Weil inode, orig_gen, ci->i_rdcache_gen,
2143cd045cb4SSage Weil ci->i_rdcache_revoking);
21449563f88cSYan, Zheng if (__ceph_caps_revoking_other(ci, NULL, CEPH_CAP_FILE_CACHE))
21459563f88cSYan, Zheng check = 1;
2146355da1ebSSage Weil }
2147be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2148b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex);
21499563f88cSYan, Zheng out:
2150355da1ebSSage Weil if (check)
2151e4b731ccSXiubo Li ceph_check_caps(ci, 0);
21523c6f6b79SSage Weil }
21533c6f6b79SSage Weil
21543c6f6b79SSage Weil /*
2155355da1ebSSage Weil * Make sure any pending truncation is applied before doing anything
2156355da1ebSSage Weil * that may depend on it.
2157355da1ebSSage Weil */
__ceph_do_pending_vmtruncate(struct inode * inode)2158b415bf4fSYan, Zheng void __ceph_do_pending_vmtruncate(struct inode *inode)
2159355da1ebSSage Weil {
2160355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
2161355da1ebSSage Weil u64 to;
2162a85f50b6SYan, Zheng int wrbuffer_refs, finish = 0;
2163355da1ebSSage Weil
2164b0d7c223SYan, Zheng mutex_lock(&ci->i_truncate_mutex);
2165355da1ebSSage Weil retry:
2166be655596SSage Weil spin_lock(&ci->i_ceph_lock);
2167355da1ebSSage Weil if (ci->i_truncate_pending == 0) {
2168295fc4aaSXiubo Li dout("%s %p none pending\n", __func__, inode);
2169be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2170b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex);
2171355da1ebSSage Weil return;
2172355da1ebSSage Weil }
2173355da1ebSSage Weil
2174355da1ebSSage Weil /*
2175355da1ebSSage Weil * make sure any dirty snapped pages are flushed before we
2176355da1ebSSage Weil * possibly truncate them.. so write AND block!
2177355da1ebSSage Weil */
2178355da1ebSSage Weil if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) {
2179c8fd0d37SYan, Zheng spin_unlock(&ci->i_ceph_lock);
2180295fc4aaSXiubo Li dout("%s %p flushing snaps first\n", __func__, inode);
2181355da1ebSSage Weil filemap_write_and_wait_range(&inode->i_data, 0,
2182355da1ebSSage Weil inode->i_sb->s_maxbytes);
2183355da1ebSSage Weil goto retry;
2184355da1ebSSage Weil }
2185355da1ebSSage Weil
2186b0d7c223SYan, Zheng /* there should be no reader or writer */
2187b0d7c223SYan, Zheng WARN_ON_ONCE(ci->i_rd_ref || ci->i_wr_ref);
2188b0d7c223SYan, Zheng
21895c64737dSXiubo Li to = ci->i_truncate_pagecache_size;
2190355da1ebSSage Weil wrbuffer_refs = ci->i_wrbuffer_ref;
2191295fc4aaSXiubo Li dout("%s %p (%d) to %lld\n", __func__, inode,
2192355da1ebSSage Weil ci->i_truncate_pending, to);
2193be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2194355da1ebSSage Weil
2195400e1286SJeff Layton ceph_fscache_resize(inode, to);
21964e217b5dSYan, Zheng truncate_pagecache(inode, to);
2197355da1ebSSage Weil
2198be655596SSage Weil spin_lock(&ci->i_ceph_lock);
21995c64737dSXiubo Li if (to == ci->i_truncate_pagecache_size) {
2200a85f50b6SYan, Zheng ci->i_truncate_pending = 0;
2201a85f50b6SYan, Zheng finish = 1;
2202a85f50b6SYan, Zheng }
2203be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
2204a85f50b6SYan, Zheng if (!finish)
2205a85f50b6SYan, Zheng goto retry;
2206355da1ebSSage Weil
2207b0d7c223SYan, Zheng mutex_unlock(&ci->i_truncate_mutex);
2208b0d7c223SYan, Zheng
2209355da1ebSSage Weil if (wrbuffer_refs == 0)
2210e4b731ccSXiubo Li ceph_check_caps(ci, 0);
2211a85f50b6SYan, Zheng
221203066f23SYehuda Sadeh wake_up_all(&ci->i_cap_wq);
2213355da1ebSSage Weil }
2214355da1ebSSage Weil
ceph_inode_work(struct work_struct * work)22151cf89a8dSYan, Zheng static void ceph_inode_work(struct work_struct *work)
22161cf89a8dSYan, Zheng {
22171cf89a8dSYan, Zheng struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info,
22181cf89a8dSYan, Zheng i_work);
2219874c8ca1SDavid Howells struct inode *inode = &ci->netfs.inode;
22201cf89a8dSYan, Zheng
22211cf89a8dSYan, Zheng if (test_and_clear_bit(CEPH_I_WORK_WRITEBACK, &ci->i_work_mask)) {
22221cf89a8dSYan, Zheng dout("writeback %p\n", inode);
22231cf89a8dSYan, Zheng filemap_fdatawrite(&inode->i_data);
22241cf89a8dSYan, Zheng }
22251cf89a8dSYan, Zheng if (test_and_clear_bit(CEPH_I_WORK_INVALIDATE_PAGES, &ci->i_work_mask))
22261cf89a8dSYan, Zheng ceph_do_invalidate_pages(inode);
22271cf89a8dSYan, Zheng
22281cf89a8dSYan, Zheng if (test_and_clear_bit(CEPH_I_WORK_VMTRUNCATE, &ci->i_work_mask))
22291cf89a8dSYan, Zheng __ceph_do_pending_vmtruncate(inode);
22301cf89a8dSYan, Zheng
2231a8810cdcSJeff Layton if (test_and_clear_bit(CEPH_I_WORK_CHECK_CAPS, &ci->i_work_mask))
2232e4b731ccSXiubo Li ceph_check_caps(ci, 0);
2233a8810cdcSJeff Layton
2234a8810cdcSJeff Layton if (test_and_clear_bit(CEPH_I_WORK_FLUSH_SNAPS, &ci->i_work_mask))
2235a8810cdcSJeff Layton ceph_flush_snaps(ci, NULL);
2236a8810cdcSJeff Layton
22371cf89a8dSYan, Zheng iput(inode);
22381cf89a8dSYan, Zheng }
22391cf89a8dSYan, Zheng
ceph_encrypted_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)224079f2f6adSJeff Layton static const char *ceph_encrypted_get_link(struct dentry *dentry,
224179f2f6adSJeff Layton struct inode *inode,
224279f2f6adSJeff Layton struct delayed_call *done)
224379f2f6adSJeff Layton {
224479f2f6adSJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
224579f2f6adSJeff Layton
224679f2f6adSJeff Layton if (!dentry)
224779f2f6adSJeff Layton return ERR_PTR(-ECHILD);
224879f2f6adSJeff Layton
224979f2f6adSJeff Layton return fscrypt_get_symlink(inode, ci->i_symlink, i_size_read(inode),
225079f2f6adSJeff Layton done);
225179f2f6adSJeff Layton }
225279f2f6adSJeff Layton
ceph_encrypted_symlink_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)225379f2f6adSJeff Layton static int ceph_encrypted_symlink_getattr(struct mnt_idmap *idmap,
225479f2f6adSJeff Layton const struct path *path,
225579f2f6adSJeff Layton struct kstat *stat, u32 request_mask,
225679f2f6adSJeff Layton unsigned int query_flags)
225779f2f6adSJeff Layton {
225879f2f6adSJeff Layton int ret;
225979f2f6adSJeff Layton
226079f2f6adSJeff Layton ret = ceph_getattr(idmap, path, stat, request_mask, query_flags);
226179f2f6adSJeff Layton if (ret)
226279f2f6adSJeff Layton return ret;
226379f2f6adSJeff Layton return fscrypt_symlink_getattr(path, stat);
226479f2f6adSJeff Layton }
226579f2f6adSJeff Layton
2266355da1ebSSage Weil /*
2267355da1ebSSage Weil * symlinks
2268355da1ebSSage Weil */
2269355da1ebSSage Weil static const struct inode_operations ceph_symlink_iops = {
22706b255391SAl Viro .get_link = simple_get_link,
22710b932672SYan, Zheng .setattr = ceph_setattr,
22720b932672SYan, Zheng .getattr = ceph_getattr,
22730b932672SYan, Zheng .listxattr = ceph_listxattr,
2274355da1ebSSage Weil };
2275355da1ebSSage Weil
227679f2f6adSJeff Layton static const struct inode_operations ceph_encrypted_symlink_iops = {
227779f2f6adSJeff Layton .get_link = ceph_encrypted_get_link,
227879f2f6adSJeff Layton .setattr = ceph_setattr,
227979f2f6adSJeff Layton .getattr = ceph_encrypted_symlink_getattr,
228079f2f6adSJeff Layton .listxattr = ceph_listxattr,
228179f2f6adSJeff Layton };
228279f2f6adSJeff Layton
22835c64737dSXiubo Li /*
22845c64737dSXiubo Li * Transfer the encrypted last block to the MDS and the MDS
22855c64737dSXiubo Li * will help update it when truncating a smaller size.
22865c64737dSXiubo Li *
22875c64737dSXiubo Li * We don't support a PAGE_SIZE that is smaller than the
22885c64737dSXiubo Li * CEPH_FSCRYPT_BLOCK_SIZE.
22895c64737dSXiubo Li */
fill_fscrypt_truncate(struct inode * inode,struct ceph_mds_request * req,struct iattr * attr)22905c64737dSXiubo Li static int fill_fscrypt_truncate(struct inode *inode,
22915c64737dSXiubo Li struct ceph_mds_request *req,
22925c64737dSXiubo Li struct iattr *attr)
22935c64737dSXiubo Li {
22945c64737dSXiubo Li struct ceph_inode_info *ci = ceph_inode(inode);
22955c64737dSXiubo Li int boff = attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE;
22965c64737dSXiubo Li loff_t pos, orig_pos = round_down(attr->ia_size,
22975c64737dSXiubo Li CEPH_FSCRYPT_BLOCK_SIZE);
22985c64737dSXiubo Li u64 block = orig_pos >> CEPH_FSCRYPT_BLOCK_SHIFT;
22995c64737dSXiubo Li struct ceph_pagelist *pagelist = NULL;
23005c64737dSXiubo Li struct kvec iov = {0};
23015c64737dSXiubo Li struct iov_iter iter;
23025c64737dSXiubo Li struct page *page = NULL;
23035c64737dSXiubo Li struct ceph_fscrypt_truncate_size_header header;
23045c64737dSXiubo Li int retry_op = 0;
23055c64737dSXiubo Li int len = CEPH_FSCRYPT_BLOCK_SIZE;
23065c64737dSXiubo Li loff_t i_size = i_size_read(inode);
23075c64737dSXiubo Li int got, ret, issued;
23085c64737dSXiubo Li u64 objver;
23095c64737dSXiubo Li
23105c64737dSXiubo Li ret = __ceph_get_caps(inode, NULL, CEPH_CAP_FILE_RD, 0, -1, &got);
23115c64737dSXiubo Li if (ret < 0)
23125c64737dSXiubo Li return ret;
23135c64737dSXiubo Li
23145c64737dSXiubo Li issued = __ceph_caps_issued(ci, NULL);
23155c64737dSXiubo Li
23165c64737dSXiubo Li dout("%s size %lld -> %lld got cap refs on %s, issued %s\n", __func__,
23175c64737dSXiubo Li i_size, attr->ia_size, ceph_cap_string(got),
23185c64737dSXiubo Li ceph_cap_string(issued));
23195c64737dSXiubo Li
23205c64737dSXiubo Li /* Try to writeback the dirty pagecaches */
23215c64737dSXiubo Li if (issued & (CEPH_CAP_FILE_BUFFER)) {
23225c64737dSXiubo Li loff_t lend = orig_pos + CEPH_FSCRYPT_BLOCK_SHIFT - 1;
23235c64737dSXiubo Li
23245c64737dSXiubo Li ret = filemap_write_and_wait_range(inode->i_mapping,
23255c64737dSXiubo Li orig_pos, lend);
23265c64737dSXiubo Li if (ret < 0)
23275c64737dSXiubo Li goto out;
23285c64737dSXiubo Li }
23295c64737dSXiubo Li
23305c64737dSXiubo Li page = __page_cache_alloc(GFP_KERNEL);
23315c64737dSXiubo Li if (page == NULL) {
23325c64737dSXiubo Li ret = -ENOMEM;
23335c64737dSXiubo Li goto out;
23345c64737dSXiubo Li }
23355c64737dSXiubo Li
23365c64737dSXiubo Li pagelist = ceph_pagelist_alloc(GFP_KERNEL);
23375c64737dSXiubo Li if (!pagelist) {
23385c64737dSXiubo Li ret = -ENOMEM;
23395c64737dSXiubo Li goto out;
23405c64737dSXiubo Li }
23415c64737dSXiubo Li
23425c64737dSXiubo Li iov.iov_base = kmap_local_page(page);
23435c64737dSXiubo Li iov.iov_len = len;
23445c64737dSXiubo Li iov_iter_kvec(&iter, READ, &iov, 1, len);
23455c64737dSXiubo Li
23465c64737dSXiubo Li pos = orig_pos;
23475c64737dSXiubo Li ret = __ceph_sync_read(inode, &pos, &iter, &retry_op, &objver);
23485c64737dSXiubo Li if (ret < 0)
23495c64737dSXiubo Li goto out;
23505c64737dSXiubo Li
23515c64737dSXiubo Li /* Insert the header first */
23525c64737dSXiubo Li header.ver = 1;
23535c64737dSXiubo Li header.compat = 1;
23545c64737dSXiubo Li header.change_attr = cpu_to_le64(inode_peek_iversion_raw(inode));
23555c64737dSXiubo Li
23565c64737dSXiubo Li /*
23575c64737dSXiubo Li * Always set the block_size to CEPH_FSCRYPT_BLOCK_SIZE,
23585c64737dSXiubo Li * because in MDS it may need this to do the truncate.
23595c64737dSXiubo Li */
23605c64737dSXiubo Li header.block_size = cpu_to_le32(CEPH_FSCRYPT_BLOCK_SIZE);
23615c64737dSXiubo Li
23625c64737dSXiubo Li /*
23635c64737dSXiubo Li * If we hit a hole here, we should just skip filling
23645c64737dSXiubo Li * the fscrypt for the request, because once the fscrypt
23655c64737dSXiubo Li * is enabled, the file will be split into many blocks
23665c64737dSXiubo Li * with the size of CEPH_FSCRYPT_BLOCK_SIZE, if there
23675c64737dSXiubo Li * has a hole, the hole size should be multiple of block
23685c64737dSXiubo Li * size.
23695c64737dSXiubo Li *
23705c64737dSXiubo Li * If the Rados object doesn't exist, it will be set to 0.
23715c64737dSXiubo Li */
23725c64737dSXiubo Li if (!objver) {
23735c64737dSXiubo Li dout("%s hit hole, ppos %lld < size %lld\n", __func__,
23745c64737dSXiubo Li pos, i_size);
23755c64737dSXiubo Li
23765c64737dSXiubo Li header.data_len = cpu_to_le32(8 + 8 + 4);
23775c64737dSXiubo Li header.file_offset = 0;
23785c64737dSXiubo Li ret = 0;
23795c64737dSXiubo Li } else {
23805c64737dSXiubo Li header.data_len = cpu_to_le32(8 + 8 + 4 + CEPH_FSCRYPT_BLOCK_SIZE);
23815c64737dSXiubo Li header.file_offset = cpu_to_le64(orig_pos);
23825c64737dSXiubo Li
2383295fc4aaSXiubo Li dout("%s encrypt block boff/bsize %d/%lu\n", __func__,
2384295fc4aaSXiubo Li boff, CEPH_FSCRYPT_BLOCK_SIZE);
2385295fc4aaSXiubo Li
23865c64737dSXiubo Li /* truncate and zero out the extra contents for the last block */
23875c64737dSXiubo Li memset(iov.iov_base + boff, 0, PAGE_SIZE - boff);
23885c64737dSXiubo Li
23895c64737dSXiubo Li /* encrypt the last block */
23905c64737dSXiubo Li ret = ceph_fscrypt_encrypt_block_inplace(inode, page,
23915c64737dSXiubo Li CEPH_FSCRYPT_BLOCK_SIZE,
23925c64737dSXiubo Li 0, block,
23935c64737dSXiubo Li GFP_KERNEL);
23945c64737dSXiubo Li if (ret)
23955c64737dSXiubo Li goto out;
23965c64737dSXiubo Li }
23975c64737dSXiubo Li
23985c64737dSXiubo Li /* Insert the header */
23995c64737dSXiubo Li ret = ceph_pagelist_append(pagelist, &header, sizeof(header));
24005c64737dSXiubo Li if (ret)
24015c64737dSXiubo Li goto out;
24025c64737dSXiubo Li
24035c64737dSXiubo Li if (header.block_size) {
24045c64737dSXiubo Li /* Append the last block contents to pagelist */
24055c64737dSXiubo Li ret = ceph_pagelist_append(pagelist, iov.iov_base,
24065c64737dSXiubo Li CEPH_FSCRYPT_BLOCK_SIZE);
24075c64737dSXiubo Li if (ret)
24085c64737dSXiubo Li goto out;
24095c64737dSXiubo Li }
24105c64737dSXiubo Li req->r_pagelist = pagelist;
24115c64737dSXiubo Li out:
24125c64737dSXiubo Li dout("%s %p size dropping cap refs on %s\n", __func__,
24135c64737dSXiubo Li inode, ceph_cap_string(got));
24145c64737dSXiubo Li ceph_put_cap_refs(ci, got);
24155c64737dSXiubo Li if (iov.iov_base)
24165c64737dSXiubo Li kunmap_local(iov.iov_base);
24175c64737dSXiubo Li if (page)
24185c64737dSXiubo Li __free_pages(page, 0);
24195c64737dSXiubo Li if (ret && pagelist)
24205c64737dSXiubo Li ceph_pagelist_release(pagelist);
24215c64737dSXiubo Li return ret;
24225c64737dSXiubo Li }
24235c64737dSXiubo Li
__ceph_setattr(struct inode * inode,struct iattr * attr,struct ceph_iattr * cia)24242d332d5bSJeff Layton int __ceph_setattr(struct inode *inode, struct iattr *attr,
24252d332d5bSJeff Layton struct ceph_iattr *cia)
2426355da1ebSSage Weil {
2427355da1ebSSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
2428c62498d7SJeff Layton unsigned int ia_valid = attr->ia_valid;
2429355da1ebSSage Weil struct ceph_mds_request *req;
2430*985b9ee8SXiubo Li struct ceph_mds_client *mdsc = ceph_sb_to_fs_client(inode->i_sb)->mdsc;
2431f66fd9f0SYan, Zheng struct ceph_cap_flush *prealloc_cf;
24325c64737dSXiubo Li loff_t isize = i_size_read(inode);
2433355da1ebSSage Weil int issued;
2434355da1ebSSage Weil int release = 0, dirtied = 0;
2435355da1ebSSage Weil int mask = 0;
2436355da1ebSSage Weil int err = 0;
2437fca65b4aSSage Weil int inode_dirty_flags = 0;
2438604d1b02SYan, Zheng bool lock_snap_rwsem = false;
24395c64737dSXiubo Li bool fill_fscrypt;
24405c64737dSXiubo Li int truncate_retry = 20; /* The RMW will take around 50ms */
2441355da1ebSSage Weil
24425c64737dSXiubo Li retry:
2443f66fd9f0SYan, Zheng prealloc_cf = ceph_alloc_cap_flush();
2444f66fd9f0SYan, Zheng if (!prealloc_cf)
2445f66fd9f0SYan, Zheng return -ENOMEM;
2446f66fd9f0SYan, Zheng
2447355da1ebSSage Weil req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR,
2448355da1ebSSage Weil USE_AUTH_MDS);
2449f66fd9f0SYan, Zheng if (IS_ERR(req)) {
2450f66fd9f0SYan, Zheng ceph_free_cap_flush(prealloc_cf);
2451355da1ebSSage Weil return PTR_ERR(req);
2452f66fd9f0SYan, Zheng }
2453355da1ebSSage Weil
24545c64737dSXiubo Li fill_fscrypt = false;
2455be655596SSage Weil spin_lock(&ci->i_ceph_lock);
2456355da1ebSSage Weil issued = __ceph_caps_issued(ci, NULL);
2457604d1b02SYan, Zheng
2458604d1b02SYan, Zheng if (!ci->i_head_snapc &&
2459604d1b02SYan, Zheng (issued & (CEPH_CAP_ANY_EXCL | CEPH_CAP_FILE_WR))) {
2460604d1b02SYan, Zheng lock_snap_rwsem = true;
2461604d1b02SYan, Zheng if (!down_read_trylock(&mdsc->snap_rwsem)) {
2462604d1b02SYan, Zheng spin_unlock(&ci->i_ceph_lock);
2463604d1b02SYan, Zheng down_read(&mdsc->snap_rwsem);
2464604d1b02SYan, Zheng spin_lock(&ci->i_ceph_lock);
2465604d1b02SYan, Zheng issued = __ceph_caps_issued(ci, NULL);
2466604d1b02SYan, Zheng }
2467604d1b02SYan, Zheng }
2468604d1b02SYan, Zheng
2469355da1ebSSage Weil dout("setattr %p issued %s\n", inode, ceph_cap_string(issued));
24702d332d5bSJeff Layton #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
24712d332d5bSJeff Layton if (cia && cia->fscrypt_auth) {
24722d332d5bSJeff Layton u32 len = ceph_fscrypt_auth_len(cia->fscrypt_auth);
24732d332d5bSJeff Layton
24742d332d5bSJeff Layton if (len > sizeof(*cia->fscrypt_auth)) {
24752d332d5bSJeff Layton err = -EINVAL;
24762d332d5bSJeff Layton spin_unlock(&ci->i_ceph_lock);
24772d332d5bSJeff Layton goto out;
24782d332d5bSJeff Layton }
24792d332d5bSJeff Layton
24802d332d5bSJeff Layton dout("setattr %llx:%llx fscrypt_auth len %u to %u)\n",
24812d332d5bSJeff Layton ceph_vinop(inode), ci->fscrypt_auth_len, len);
24822d332d5bSJeff Layton
24832d332d5bSJeff Layton /* It should never be re-set once set */
24842d332d5bSJeff Layton WARN_ON_ONCE(ci->fscrypt_auth);
24852d332d5bSJeff Layton
24862d332d5bSJeff Layton if (issued & CEPH_CAP_AUTH_EXCL) {
24872d332d5bSJeff Layton dirtied |= CEPH_CAP_AUTH_EXCL;
24882d332d5bSJeff Layton kfree(ci->fscrypt_auth);
24892d332d5bSJeff Layton ci->fscrypt_auth = (u8 *)cia->fscrypt_auth;
24902d332d5bSJeff Layton ci->fscrypt_auth_len = len;
24912d332d5bSJeff Layton } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
24922d332d5bSJeff Layton ci->fscrypt_auth_len != len ||
24932d332d5bSJeff Layton memcmp(ci->fscrypt_auth, cia->fscrypt_auth, len)) {
24942d332d5bSJeff Layton req->r_fscrypt_auth = cia->fscrypt_auth;
24952d332d5bSJeff Layton mask |= CEPH_SETATTR_FSCRYPT_AUTH;
24962d332d5bSJeff Layton release |= CEPH_CAP_AUTH_SHARED;
24972d332d5bSJeff Layton }
24982d332d5bSJeff Layton cia->fscrypt_auth = NULL;
24992d332d5bSJeff Layton }
25002d332d5bSJeff Layton #else
25012d332d5bSJeff Layton if (cia && cia->fscrypt_auth) {
25022d332d5bSJeff Layton err = -EINVAL;
25032d332d5bSJeff Layton spin_unlock(&ci->i_ceph_lock);
25042d332d5bSJeff Layton goto out;
25052d332d5bSJeff Layton }
25062d332d5bSJeff Layton #endif /* CONFIG_FS_ENCRYPTION */
2507355da1ebSSage Weil
2508355da1ebSSage Weil if (ia_valid & ATTR_UID) {
2509355da1ebSSage Weil dout("setattr %p uid %d -> %d\n", inode,
2510bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, inode->i_uid),
2511bd2bae6aSEric W. Biederman from_kuid(&init_user_ns, attr->ia_uid));
2512355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) {
2513355da1ebSSage Weil inode->i_uid = attr->ia_uid;
2514355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL;
2515355da1ebSSage Weil } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2516ab871b90SEric W. Biederman !uid_eq(attr->ia_uid, inode->i_uid)) {
2517ab871b90SEric W. Biederman req->r_args.setattr.uid = cpu_to_le32(
2518ab871b90SEric W. Biederman from_kuid(&init_user_ns, attr->ia_uid));
2519355da1ebSSage Weil mask |= CEPH_SETATTR_UID;
2520355da1ebSSage Weil release |= CEPH_CAP_AUTH_SHARED;
2521355da1ebSSage Weil }
2522355da1ebSSage Weil }
2523355da1ebSSage Weil if (ia_valid & ATTR_GID) {
2524355da1ebSSage Weil dout("setattr %p gid %d -> %d\n", inode,
2525bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, inode->i_gid),
2526bd2bae6aSEric W. Biederman from_kgid(&init_user_ns, attr->ia_gid));
2527355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) {
2528355da1ebSSage Weil inode->i_gid = attr->ia_gid;
2529355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL;
2530355da1ebSSage Weil } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2531ab871b90SEric W. Biederman !gid_eq(attr->ia_gid, inode->i_gid)) {
2532ab871b90SEric W. Biederman req->r_args.setattr.gid = cpu_to_le32(
2533ab871b90SEric W. Biederman from_kgid(&init_user_ns, attr->ia_gid));
2534355da1ebSSage Weil mask |= CEPH_SETATTR_GID;
2535355da1ebSSage Weil release |= CEPH_CAP_AUTH_SHARED;
2536355da1ebSSage Weil }
2537355da1ebSSage Weil }
2538355da1ebSSage Weil if (ia_valid & ATTR_MODE) {
2539355da1ebSSage Weil dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode,
2540355da1ebSSage Weil attr->ia_mode);
2541355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL) {
2542355da1ebSSage Weil inode->i_mode = attr->ia_mode;
2543355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL;
2544355da1ebSSage Weil } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 ||
2545355da1ebSSage Weil attr->ia_mode != inode->i_mode) {
25467221fe4cSGuangliang Zhao inode->i_mode = attr->ia_mode;
2547355da1ebSSage Weil req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode);
2548355da1ebSSage Weil mask |= CEPH_SETATTR_MODE;
2549355da1ebSSage Weil release |= CEPH_CAP_AUTH_SHARED;
2550355da1ebSSage Weil }
2551355da1ebSSage Weil }
2552355da1ebSSage Weil
2553355da1ebSSage Weil if (ia_valid & ATTR_ATIME) {
255413442b03SDeepa Dinamani dout("setattr %p atime %lld.%ld -> %lld.%ld\n", inode,
25559bbeab41SArnd Bergmann inode->i_atime.tv_sec, inode->i_atime.tv_nsec,
25569bbeab41SArnd Bergmann attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec);
2557355da1ebSSage Weil if (issued & CEPH_CAP_FILE_EXCL) {
2558355da1ebSSage Weil ci->i_time_warp_seq++;
2559355da1ebSSage Weil inode->i_atime = attr->ia_atime;
2560355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_EXCL;
2561355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_WR) &&
256295582b00SDeepa Dinamani timespec64_compare(&inode->i_atime,
2563355da1ebSSage Weil &attr->ia_atime) < 0) {
2564355da1ebSSage Weil inode->i_atime = attr->ia_atime;
2565355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_WR;
2566355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
256795582b00SDeepa Dinamani !timespec64_equal(&inode->i_atime, &attr->ia_atime)) {
25689bbeab41SArnd Bergmann ceph_encode_timespec64(&req->r_args.setattr.atime,
25699bbeab41SArnd Bergmann &attr->ia_atime);
2570355da1ebSSage Weil mask |= CEPH_SETATTR_ATIME;
2571be70489eSYan, Zheng release |= CEPH_CAP_FILE_SHARED |
2572be70489eSYan, Zheng CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2573355da1ebSSage Weil }
2574355da1ebSSage Weil }
2575c62498d7SJeff Layton if (ia_valid & ATTR_SIZE) {
25762d6795fbSJeff Layton dout("setattr %p size %lld -> %lld\n", inode, isize, attr->ia_size);
25775c64737dSXiubo Li /*
25785c64737dSXiubo Li * Only when the new size is smaller and not aligned to
25795c64737dSXiubo Li * CEPH_FSCRYPT_BLOCK_SIZE will the RMW is needed.
25805c64737dSXiubo Li */
25815c64737dSXiubo Li if (IS_ENCRYPTED(inode) && attr->ia_size < isize &&
25825c64737dSXiubo Li (attr->ia_size % CEPH_FSCRYPT_BLOCK_SIZE)) {
25835c64737dSXiubo Li mask |= CEPH_SETATTR_SIZE;
25845c64737dSXiubo Li release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
25855c64737dSXiubo Li CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
25865c64737dSXiubo Li set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
25875c64737dSXiubo Li mask |= CEPH_SETATTR_FSCRYPT_FILE;
25885c64737dSXiubo Li req->r_args.setattr.size =
25895c64737dSXiubo Li cpu_to_le64(round_up(attr->ia_size,
25905c64737dSXiubo Li CEPH_FSCRYPT_BLOCK_SIZE));
25915c64737dSXiubo Li req->r_args.setattr.old_size =
25925c64737dSXiubo Li cpu_to_le64(round_up(isize,
25935c64737dSXiubo Li CEPH_FSCRYPT_BLOCK_SIZE));
25945c64737dSXiubo Li req->r_fscrypt_file = attr->ia_size;
25955c64737dSXiubo Li fill_fscrypt = true;
25965c64737dSXiubo Li } else if ((issued & CEPH_CAP_FILE_EXCL) && attr->ia_size >= isize) {
2597e90334e8SXiubo Li if (attr->ia_size > isize) {
2598c62498d7SJeff Layton i_size_write(inode, attr->ia_size);
2599c62498d7SJeff Layton inode->i_blocks = calc_inode_blocks(attr->ia_size);
2600c62498d7SJeff Layton ci->i_reported_size = attr->ia_size;
2601c62498d7SJeff Layton dirtied |= CEPH_CAP_FILE_EXCL;
2602c62498d7SJeff Layton ia_valid |= ATTR_MTIME;
2603e90334e8SXiubo Li }
2604c62498d7SJeff Layton } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
26052d6795fbSJeff Layton attr->ia_size != isize) {
2606c62498d7SJeff Layton mask |= CEPH_SETATTR_SIZE;
2607c62498d7SJeff Layton release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL |
2608c62498d7SJeff Layton CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
260916be62fcSJeff Layton if (IS_ENCRYPTED(inode) && attr->ia_size) {
261016be62fcSJeff Layton set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
261116be62fcSJeff Layton mask |= CEPH_SETATTR_FSCRYPT_FILE;
261216be62fcSJeff Layton req->r_args.setattr.size =
261316be62fcSJeff Layton cpu_to_le64(round_up(attr->ia_size,
261416be62fcSJeff Layton CEPH_FSCRYPT_BLOCK_SIZE));
261516be62fcSJeff Layton req->r_args.setattr.old_size =
261616be62fcSJeff Layton cpu_to_le64(round_up(isize,
261716be62fcSJeff Layton CEPH_FSCRYPT_BLOCK_SIZE));
261816be62fcSJeff Layton req->r_fscrypt_file = attr->ia_size;
261916be62fcSJeff Layton } else {
262016be62fcSJeff Layton req->r_args.setattr.size = cpu_to_le64(attr->ia_size);
262116be62fcSJeff Layton req->r_args.setattr.old_size = cpu_to_le64(isize);
262216be62fcSJeff Layton req->r_fscrypt_file = 0;
262316be62fcSJeff Layton }
2624c62498d7SJeff Layton }
2625c62498d7SJeff Layton }
2626355da1ebSSage Weil if (ia_valid & ATTR_MTIME) {
262713442b03SDeepa Dinamani dout("setattr %p mtime %lld.%ld -> %lld.%ld\n", inode,
26289bbeab41SArnd Bergmann inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec,
26299bbeab41SArnd Bergmann attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec);
2630355da1ebSSage Weil if (issued & CEPH_CAP_FILE_EXCL) {
2631355da1ebSSage Weil ci->i_time_warp_seq++;
2632355da1ebSSage Weil inode->i_mtime = attr->ia_mtime;
2633355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_EXCL;
2634355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_WR) &&
263595582b00SDeepa Dinamani timespec64_compare(&inode->i_mtime,
2636355da1ebSSage Weil &attr->ia_mtime) < 0) {
2637355da1ebSSage Weil inode->i_mtime = attr->ia_mtime;
2638355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_WR;
2639355da1ebSSage Weil } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 ||
264095582b00SDeepa Dinamani !timespec64_equal(&inode->i_mtime, &attr->ia_mtime)) {
26419bbeab41SArnd Bergmann ceph_encode_timespec64(&req->r_args.setattr.mtime,
26429bbeab41SArnd Bergmann &attr->ia_mtime);
2643355da1ebSSage Weil mask |= CEPH_SETATTR_MTIME;
2644be70489eSYan, Zheng release |= CEPH_CAP_FILE_SHARED |
2645be70489eSYan, Zheng CEPH_CAP_FILE_RD | CEPH_CAP_FILE_WR;
2646355da1ebSSage Weil }
2647355da1ebSSage Weil }
2648355da1ebSSage Weil
2649355da1ebSSage Weil /* these do nothing */
2650355da1ebSSage Weil if (ia_valid & ATTR_CTIME) {
2651355da1ebSSage Weil bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME|
2652355da1ebSSage Weil ATTR_MODE|ATTR_UID|ATTR_GID)) == 0;
265313442b03SDeepa Dinamani dout("setattr %p ctime %lld.%ld -> %lld.%ld (%s)\n", inode,
26547795aef0SJeff Layton inode_get_ctime(inode).tv_sec,
26557795aef0SJeff Layton inode_get_ctime(inode).tv_nsec,
26569bbeab41SArnd Bergmann attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec,
2657355da1ebSSage Weil only ? "ctime only" : "ignored");
2658355da1ebSSage Weil if (only) {
2659355da1ebSSage Weil /*
2660355da1ebSSage Weil * if kernel wants to dirty ctime but nothing else,
2661355da1ebSSage Weil * we need to choose a cap to dirty under, or do
2662355da1ebSSage Weil * a almost-no-op setattr
2663355da1ebSSage Weil */
2664355da1ebSSage Weil if (issued & CEPH_CAP_AUTH_EXCL)
2665355da1ebSSage Weil dirtied |= CEPH_CAP_AUTH_EXCL;
2666355da1ebSSage Weil else if (issued & CEPH_CAP_FILE_EXCL)
2667355da1ebSSage Weil dirtied |= CEPH_CAP_FILE_EXCL;
2668355da1ebSSage Weil else if (issued & CEPH_CAP_XATTR_EXCL)
2669355da1ebSSage Weil dirtied |= CEPH_CAP_XATTR_EXCL;
2670355da1ebSSage Weil else
2671355da1ebSSage Weil mask |= CEPH_SETATTR_CTIME;
2672355da1ebSSage Weil }
2673355da1ebSSage Weil }
2674355da1ebSSage Weil if (ia_valid & ATTR_FILE)
2675355da1ebSSage Weil dout("setattr %p ATTR_FILE ... hrm!\n", inode);
2676355da1ebSSage Weil
2677355da1ebSSage Weil if (dirtied) {
2678f66fd9f0SYan, Zheng inode_dirty_flags = __ceph_mark_dirty_caps(ci, dirtied,
2679f66fd9f0SYan, Zheng &prealloc_cf);
26807795aef0SJeff Layton inode_set_ctime_to_ts(inode, attr->ia_ctime);
2681b4b924c7SJeff Layton inode_inc_iversion_raw(inode);
2682355da1ebSSage Weil }
2683355da1ebSSage Weil
2684355da1ebSSage Weil release &= issued;
2685be655596SSage Weil spin_unlock(&ci->i_ceph_lock);
26865c64737dSXiubo Li if (lock_snap_rwsem) {
2687604d1b02SYan, Zheng up_read(&mdsc->snap_rwsem);
26885c64737dSXiubo Li lock_snap_rwsem = false;
26895c64737dSXiubo Li }
2690355da1ebSSage Weil
2691fca65b4aSSage Weil if (inode_dirty_flags)
2692fca65b4aSSage Weil __mark_inode_dirty(inode, inode_dirty_flags);
2693fca65b4aSSage Weil
2694355da1ebSSage Weil if (mask) {
269570b666c3SSage Weil req->r_inode = inode;
269670b666c3SSage Weil ihold(inode);
2697355da1ebSSage Weil req->r_inode_drop = release;
2698355da1ebSSage Weil req->r_args.setattr.mask = cpu_to_le32(mask);
2699355da1ebSSage Weil req->r_num_caps = 1;
27000ed1e90aSArnd Bergmann req->r_stamp = attr->ia_ctime;
27015c64737dSXiubo Li if (fill_fscrypt) {
27025c64737dSXiubo Li err = fill_fscrypt_truncate(inode, req, attr);
27035c64737dSXiubo Li if (err)
27045c64737dSXiubo Li goto out;
2705355da1ebSSage Weil }
27065c64737dSXiubo Li
27075c64737dSXiubo Li /*
27085c64737dSXiubo Li * The truncate request will return -EAGAIN when the
27095c64737dSXiubo Li * last block has been updated just before the MDS
27105c64737dSXiubo Li * successfully gets the xlock for the FILE lock. To
27115c64737dSXiubo Li * avoid corrupting the file contents we need to retry
27125c64737dSXiubo Li * it.
27135c64737dSXiubo Li */
2714355da1ebSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req);
27155c64737dSXiubo Li if (err == -EAGAIN && truncate_retry--) {
27165c64737dSXiubo Li dout("setattr %p result=%d (%s locally, %d remote), retry it!\n",
27175c64737dSXiubo Li inode, err, ceph_cap_string(dirtied), mask);
27185c64737dSXiubo Li ceph_mdsc_put_request(req);
27195c64737dSXiubo Li ceph_free_cap_flush(prealloc_cf);
27205c64737dSXiubo Li goto retry;
27215c64737dSXiubo Li }
2722355da1ebSSage Weil }
27232d332d5bSJeff Layton out:
2724355da1ebSSage Weil dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err,
2725355da1ebSSage Weil ceph_cap_string(dirtied), mask);
2726355da1ebSSage Weil
2727355da1ebSSage Weil ceph_mdsc_put_request(req);
27288179a101SYan, Zheng ceph_free_cap_flush(prealloc_cf);
27298179a101SYan, Zheng
27308179a101SYan, Zheng if (err >= 0 && (mask & CEPH_SETATTR_SIZE))
2731b415bf4fSYan, Zheng __ceph_do_pending_vmtruncate(inode);
27328179a101SYan, Zheng
2733355da1ebSSage Weil return err;
2734355da1ebSSage Weil }
2735355da1ebSSage Weil
2736355da1ebSSage Weil /*
2737a26feccaSAndreas Gruenbacher * setattr
2738a26feccaSAndreas Gruenbacher */
ceph_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)2739c1632a0fSChristian Brauner int ceph_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
2740549c7297SChristian Brauner struct iattr *attr)
2741a26feccaSAndreas Gruenbacher {
2742fd5472edSJan Kara struct inode *inode = d_inode(dentry);
2743*985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_inode_to_fs_client(inode);
2744fd5472edSJan Kara int err;
2745fd5472edSJan Kara
2746fd5472edSJan Kara if (ceph_snap(inode) != CEPH_NOSNAP)
2747fd5472edSJan Kara return -EROFS;
2748fd5472edSJan Kara
27495d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode))
27505d6451b1SJeff Layton return -ESTALE;
27515d6451b1SJeff Layton
275294af0470SJeff Layton err = fscrypt_prepare_setattr(dentry, attr);
275394af0470SJeff Layton if (err)
275494af0470SJeff Layton return err;
275594af0470SJeff Layton
2756c1632a0fSChristian Brauner err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2757fd5472edSJan Kara if (err != 0)
2758fd5472edSJan Kara return err;
2759fd5472edSJan Kara
27602b83845fSLuis Henriques if ((attr->ia_valid & ATTR_SIZE) &&
27612d6795fbSJeff Layton attr->ia_size > max(i_size_read(inode), fsc->max_file_size))
276236a4c72dSChengguang Xu return -EFBIG;
276336a4c72dSChengguang Xu
276436a4c72dSChengguang Xu if ((attr->ia_valid & ATTR_SIZE) &&
27652b83845fSLuis Henriques ceph_quota_is_max_bytes_exceeded(inode, attr->ia_size))
27662b83845fSLuis Henriques return -EDQUOT;
27672b83845fSLuis Henriques
27682d332d5bSJeff Layton err = __ceph_setattr(inode, attr, NULL);
27698179a101SYan, Zheng
27708179a101SYan, Zheng if (err >= 0 && (attr->ia_valid & ATTR_MODE))
277113e83a49SChristian Brauner err = posix_acl_chmod(&nop_mnt_idmap, dentry, attr->ia_mode);
27728179a101SYan, Zheng
27738179a101SYan, Zheng return err;
2774a26feccaSAndreas Gruenbacher }
2775a26feccaSAndreas Gruenbacher
ceph_try_to_choose_auth_mds(struct inode * inode,int mask)27765eed80fbSXiubo Li int ceph_try_to_choose_auth_mds(struct inode *inode, int mask)
27775eed80fbSXiubo Li {
27785eed80fbSXiubo Li int issued = ceph_caps_issued(ceph_inode(inode));
27795eed80fbSXiubo Li
27805eed80fbSXiubo Li /*
27815eed80fbSXiubo Li * If any 'x' caps is issued we can just choose the auth MDS
27825eed80fbSXiubo Li * instead of the random replica MDSes. Because only when the
27835eed80fbSXiubo Li * Locker is in LOCK_EXEC state will the loner client could
27845eed80fbSXiubo Li * get the 'x' caps. And if we send the getattr requests to
27855eed80fbSXiubo Li * any replica MDS it must auth pin and tries to rdlock from
27865eed80fbSXiubo Li * the auth MDS, and then the auth MDS need to do the Locker
27875eed80fbSXiubo Li * state transition to LOCK_SYNC. And after that the lock state
27885eed80fbSXiubo Li * will change back.
27895eed80fbSXiubo Li *
27905eed80fbSXiubo Li * This cost much when doing the Locker state transition and
27915eed80fbSXiubo Li * usually will need to revoke caps from clients.
27928266c4d7SXiubo Li *
27938266c4d7SXiubo Li * And for the 'Xs' caps for getxattr we will also choose the
27948266c4d7SXiubo Li * auth MDS, because the MDS side code is buggy due to setxattr
27958266c4d7SXiubo Li * won't notify the replica MDSes when the values changed and
27968266c4d7SXiubo Li * the replica MDS will return the old values. Though we will
27978266c4d7SXiubo Li * fix it in MDS code, but this still makes sense for old ceph.
27985eed80fbSXiubo Li */
27995eed80fbSXiubo Li if (((mask & CEPH_CAP_ANY_SHARED) && (issued & CEPH_CAP_ANY_EXCL))
28008266c4d7SXiubo Li || (mask & (CEPH_STAT_RSTAT | CEPH_STAT_CAP_XATTR)))
28015eed80fbSXiubo Li return USE_AUTH_MDS;
28025eed80fbSXiubo Li else
28035eed80fbSXiubo Li return USE_ANY_MDS;
28045eed80fbSXiubo Li }
28055eed80fbSXiubo Li
2806a26feccaSAndreas Gruenbacher /*
2807355da1ebSSage Weil * Verify that we have a lease on the given mask. If not,
2808355da1ebSSage Weil * do a getattr against an mds.
2809355da1ebSSage Weil */
__ceph_do_getattr(struct inode * inode,struct page * locked_page,int mask,bool force)281001deead0SYan, Zheng int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
281101deead0SYan, Zheng int mask, bool force)
2812355da1ebSSage Weil {
2813*985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
28143d14c5d2SYehuda Sadeh struct ceph_mds_client *mdsc = fsc->mdsc;
2815355da1ebSSage Weil struct ceph_mds_request *req;
281649a9f4f6SYan, Zheng int mode;
2817355da1ebSSage Weil int err;
2818355da1ebSSage Weil
2819355da1ebSSage Weil if (ceph_snap(inode) == CEPH_SNAPDIR) {
2820355da1ebSSage Weil dout("do_getattr inode %p SNAPDIR\n", inode);
2821355da1ebSSage Weil return 0;
2822355da1ebSSage Weil }
2823355da1ebSSage Weil
282401deead0SYan, Zheng dout("do_getattr inode %p mask %s mode 0%o\n",
282501deead0SYan, Zheng inode, ceph_cap_string(mask), inode->i_mode);
28261af16d54SXiubo Li if (!force && ceph_caps_issued_mask_metric(ceph_inode(inode), mask, 1))
2827355da1ebSSage Weil return 0;
2828355da1ebSSage Weil
28295eed80fbSXiubo Li mode = ceph_try_to_choose_auth_mds(inode, mask);
283049a9f4f6SYan, Zheng req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, mode);
2831355da1ebSSage Weil if (IS_ERR(req))
2832355da1ebSSage Weil return PTR_ERR(req);
283370b666c3SSage Weil req->r_inode = inode;
283470b666c3SSage Weil ihold(inode);
2835355da1ebSSage Weil req->r_num_caps = 1;
2836355da1ebSSage Weil req->r_args.getattr.mask = cpu_to_le32(mask);
283701deead0SYan, Zheng req->r_locked_page = locked_page;
2838355da1ebSSage Weil err = ceph_mdsc_do_request(mdsc, NULL, req);
283901deead0SYan, Zheng if (locked_page && err == 0) {
284001deead0SYan, Zheng u64 inline_version = req->r_reply_info.targeti.inline_version;
284101deead0SYan, Zheng if (inline_version == 0) {
284201deead0SYan, Zheng /* the reply is supposed to contain inline data */
284301deead0SYan, Zheng err = -EINVAL;
284448490776SXiubo Li } else if (inline_version == CEPH_INLINE_NONE ||
284548490776SXiubo Li inline_version == 1) {
284601deead0SYan, Zheng err = -ENODATA;
284701deead0SYan, Zheng } else {
284801deead0SYan, Zheng err = req->r_reply_info.targeti.inline_len;
284901deead0SYan, Zheng }
285001deead0SYan, Zheng }
2851355da1ebSSage Weil ceph_mdsc_put_request(req);
2852355da1ebSSage Weil dout("do_getattr result=%d\n", err);
2853355da1ebSSage Weil return err;
2854355da1ebSSage Weil }
2855355da1ebSSage Weil
ceph_do_getvxattr(struct inode * inode,const char * name,void * value,size_t size)28566ddf5f16SMilind Changire int ceph_do_getvxattr(struct inode *inode, const char *name, void *value,
28576ddf5f16SMilind Changire size_t size)
28586ddf5f16SMilind Changire {
2859*985b9ee8SXiubo Li struct ceph_fs_client *fsc = ceph_sb_to_fs_client(inode->i_sb);
28606ddf5f16SMilind Changire struct ceph_mds_client *mdsc = fsc->mdsc;
28616ddf5f16SMilind Changire struct ceph_mds_request *req;
28626ddf5f16SMilind Changire int mode = USE_AUTH_MDS;
28636ddf5f16SMilind Changire int err;
28646ddf5f16SMilind Changire char *xattr_value;
28656ddf5f16SMilind Changire size_t xattr_value_len;
28666ddf5f16SMilind Changire
28676ddf5f16SMilind Changire req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETVXATTR, mode);
28686ddf5f16SMilind Changire if (IS_ERR(req)) {
28696ddf5f16SMilind Changire err = -ENOMEM;
28706ddf5f16SMilind Changire goto out;
28716ddf5f16SMilind Changire }
28726ddf5f16SMilind Changire
28736eb06c46SXiubo Li req->r_feature_needed = CEPHFS_FEATURE_OP_GETVXATTR;
28746ddf5f16SMilind Changire req->r_path2 = kstrdup(name, GFP_NOFS);
28756ddf5f16SMilind Changire if (!req->r_path2) {
28766ddf5f16SMilind Changire err = -ENOMEM;
28776ddf5f16SMilind Changire goto put;
28786ddf5f16SMilind Changire }
28796ddf5f16SMilind Changire
28806ddf5f16SMilind Changire ihold(inode);
28816ddf5f16SMilind Changire req->r_inode = inode;
28826ddf5f16SMilind Changire err = ceph_mdsc_do_request(mdsc, NULL, req);
28836ddf5f16SMilind Changire if (err < 0)
28846ddf5f16SMilind Changire goto put;
28856ddf5f16SMilind Changire
28866ddf5f16SMilind Changire xattr_value = req->r_reply_info.xattr_info.xattr_value;
28876ddf5f16SMilind Changire xattr_value_len = req->r_reply_info.xattr_info.xattr_value_len;
28886ddf5f16SMilind Changire
28896ddf5f16SMilind Changire dout("do_getvxattr xattr_value_len:%zu, size:%zu\n", xattr_value_len, size);
28906ddf5f16SMilind Changire
28916ddf5f16SMilind Changire err = (int)xattr_value_len;
28926ddf5f16SMilind Changire if (size == 0)
28936ddf5f16SMilind Changire goto put;
28946ddf5f16SMilind Changire
28956ddf5f16SMilind Changire if (xattr_value_len > size) {
28966ddf5f16SMilind Changire err = -ERANGE;
28976ddf5f16SMilind Changire goto put;
28986ddf5f16SMilind Changire }
28996ddf5f16SMilind Changire
29006ddf5f16SMilind Changire memcpy(value, xattr_value, xattr_value_len);
29016ddf5f16SMilind Changire put:
29026ddf5f16SMilind Changire ceph_mdsc_put_request(req);
29036ddf5f16SMilind Changire out:
29046ddf5f16SMilind Changire dout("do_getvxattr result=%d\n", err);
29056ddf5f16SMilind Changire return err;
29066ddf5f16SMilind Changire }
29076ddf5f16SMilind Changire
2908355da1ebSSage Weil
2909355da1ebSSage Weil /*
2910355da1ebSSage Weil * Check inode permissions. We verify we have a valid value for
2911355da1ebSSage Weil * the AUTH cap, then call the generic handler.
2912355da1ebSSage Weil */
ceph_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)29134609e1f1SChristian Brauner int ceph_permission(struct mnt_idmap *idmap, struct inode *inode,
2914549c7297SChristian Brauner int mask)
2915355da1ebSSage Weil {
2916b74c79e9SNick Piggin int err;
2917b74c79e9SNick Piggin
291810556cb2SAl Viro if (mask & MAY_NOT_BLOCK)
2919b74c79e9SNick Piggin return -ECHILD;
2920b74c79e9SNick Piggin
2921508b32d8SYan, Zheng err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED, false);
2922355da1ebSSage Weil
2923355da1ebSSage Weil if (!err)
29244609e1f1SChristian Brauner err = generic_permission(&nop_mnt_idmap, inode, mask);
2925355da1ebSSage Weil return err;
2926355da1ebSSage Weil }
2927355da1ebSSage Weil
2928428bb68aSJeff Layton /* Craft a mask of needed caps given a set of requested statx attrs. */
statx_to_caps(u32 want,umode_t mode)292904fabb11SJeff Layton static int statx_to_caps(u32 want, umode_t mode)
2930428bb68aSJeff Layton {
2931428bb68aSJeff Layton int mask = 0;
2932428bb68aSJeff Layton
2933f6102994SJeff Layton if (want & (STATX_MODE|STATX_UID|STATX_GID|STATX_CTIME|STATX_BTIME|STATX_CHANGE_COOKIE))
2934428bb68aSJeff Layton mask |= CEPH_CAP_AUTH_SHARED;
2935428bb68aSJeff Layton
2936f6102994SJeff Layton if (want & (STATX_NLINK|STATX_CTIME|STATX_CHANGE_COOKIE)) {
293704fabb11SJeff Layton /*
293804fabb11SJeff Layton * The link count for directories depends on inode->i_subdirs,
293904fabb11SJeff Layton * and that is only updated when Fs caps are held.
294004fabb11SJeff Layton */
294104fabb11SJeff Layton if (S_ISDIR(mode))
294204fabb11SJeff Layton mask |= CEPH_CAP_FILE_SHARED;
294304fabb11SJeff Layton else
2944428bb68aSJeff Layton mask |= CEPH_CAP_LINK_SHARED;
294504fabb11SJeff Layton }
2946428bb68aSJeff Layton
2947f6102994SJeff Layton if (want & (STATX_ATIME|STATX_MTIME|STATX_CTIME|STATX_SIZE|STATX_BLOCKS|STATX_CHANGE_COOKIE))
2948428bb68aSJeff Layton mask |= CEPH_CAP_FILE_SHARED;
2949428bb68aSJeff Layton
2950f6102994SJeff Layton if (want & (STATX_CTIME|STATX_CHANGE_COOKIE))
2951428bb68aSJeff Layton mask |= CEPH_CAP_XATTR_SHARED;
2952428bb68aSJeff Layton
2953428bb68aSJeff Layton return mask;
2954428bb68aSJeff Layton }
2955428bb68aSJeff Layton
2956355da1ebSSage Weil /*
2957428bb68aSJeff Layton * Get all the attributes. If we have sufficient caps for the requested attrs,
2958428bb68aSJeff Layton * then we can avoid talking to the MDS at all.
2959355da1ebSSage Weil */
ceph_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)2960b74d24f7SChristian Brauner int ceph_getattr(struct mnt_idmap *idmap, const struct path *path,
2961549c7297SChristian Brauner struct kstat *stat, u32 request_mask, unsigned int flags)
2962355da1ebSSage Weil {
2963a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
2964aa87052dSXiubo Li struct super_block *sb = inode->i_sb;
2965232d4b01SSage Weil struct ceph_inode_info *ci = ceph_inode(inode);
296658981784SJeff Layton u32 valid_mask = STATX_BASIC_STATS;
2967428bb68aSJeff Layton int err = 0;
2968355da1ebSSage Weil
29695d6451b1SJeff Layton if (ceph_inode_is_shutdown(inode))
29705d6451b1SJeff Layton return -ESTALE;
29715d6451b1SJeff Layton
2972428bb68aSJeff Layton /* Skip the getattr altogether if we're asked not to sync */
2973261998c3SXiubo Li if ((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) {
297404fabb11SJeff Layton err = ceph_do_getattr(inode,
297504fabb11SJeff Layton statx_to_caps(request_mask, inode->i_mode),
2976428bb68aSJeff Layton flags & AT_STATX_FORCE_SYNC);
2977428bb68aSJeff Layton if (err)
2978428bb68aSJeff Layton return err;
2979428bb68aSJeff Layton }
2980428bb68aSJeff Layton
29810d72b928SJeff Layton generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2982ebce3eb2SJeff Layton stat->ino = ceph_present_inode(inode);
298358981784SJeff Layton
298458981784SJeff Layton /*
298558981784SJeff Layton * btime on newly-allocated inodes is 0, so if this is still set to
298658981784SJeff Layton * that, then assume that it's not valid.
298758981784SJeff Layton */
298858981784SJeff Layton if (ci->i_btime.tv_sec || ci->i_btime.tv_nsec) {
298958981784SJeff Layton stat->btime = ci->i_btime;
299058981784SJeff Layton valid_mask |= STATX_BTIME;
299158981784SJeff Layton }
299258981784SJeff Layton
2993f6102994SJeff Layton if (request_mask & STATX_CHANGE_COOKIE) {
2994f6102994SJeff Layton stat->change_cookie = inode_peek_iversion_raw(inode);
2995f6102994SJeff Layton valid_mask |= STATX_CHANGE_COOKIE;
2996f6102994SJeff Layton }
2997f6102994SJeff Layton
299875c9627eSYan, Zheng if (ceph_snap(inode) == CEPH_NOSNAP)
2999aa87052dSXiubo Li stat->dev = sb->s_dev;
3000355da1ebSSage Weil else
300175c9627eSYan, Zheng stat->dev = ci->i_snapid_map ? ci->i_snapid_map->dev : 0;
300275c9627eSYan, Zheng
3003232d4b01SSage Weil if (S_ISDIR(inode->i_mode)) {
3004*985b9ee8SXiubo Li if (ceph_test_mount_opt(ceph_sb_to_fs_client(sb), RBYTES)) {
3005232d4b01SSage Weil stat->size = ci->i_rbytes;
3006aa87052dSXiubo Li } else if (ceph_snap(inode) == CEPH_SNAPDIR) {
3007aa87052dSXiubo Li struct ceph_inode_info *pci;
3008aa87052dSXiubo Li struct ceph_snap_realm *realm;
3009aa87052dSXiubo Li struct inode *parent;
3010aa87052dSXiubo Li
3011aa87052dSXiubo Li parent = ceph_lookup_inode(sb, ceph_ino(inode));
3012f86a4866SDan Carpenter if (IS_ERR(parent))
3013aa87052dSXiubo Li return PTR_ERR(parent);
3014aa87052dSXiubo Li
3015aa87052dSXiubo Li pci = ceph_inode(parent);
3016aa87052dSXiubo Li spin_lock(&pci->i_ceph_lock);
3017aa87052dSXiubo Li realm = pci->i_snap_realm;
3018aa87052dSXiubo Li if (realm)
3019aa87052dSXiubo Li stat->size = realm->num_snaps;
30201c1266bbSYehuda Sadeh else
3021aa87052dSXiubo Li stat->size = 0;
3022aa87052dSXiubo Li spin_unlock(&pci->i_ceph_lock);
3023aa87052dSXiubo Li iput(parent);
3024aa87052dSXiubo Li } else {
30251c1266bbSYehuda Sadeh stat->size = ci->i_files + ci->i_subdirs;
3026aa87052dSXiubo Li }
3027232d4b01SSage Weil stat->blocks = 0;
3028355da1ebSSage Weil stat->blksize = 65536;
30298c6286f1SLuis Henriques /*
30308c6286f1SLuis Henriques * Some applications rely on the number of st_nlink
30318c6286f1SLuis Henriques * value on directories to be either 0 (if unlinked)
30328c6286f1SLuis Henriques * or 2 + number of subdirectories.
30338c6286f1SLuis Henriques */
30348c6286f1SLuis Henriques if (stat->nlink == 1)
30358c6286f1SLuis Henriques /* '.' + '..' + subdirs */
30368c6286f1SLuis Henriques stat->nlink = 1 + 1 + ci->i_subdirs;
3037355da1ebSSage Weil }
3038428bb68aSJeff Layton
3039f6102994SJeff Layton stat->attributes |= STATX_ATTR_CHANGE_MONOTONIC;
304094af0470SJeff Layton if (IS_ENCRYPTED(inode))
304194af0470SJeff Layton stat->attributes |= STATX_ATTR_ENCRYPTED;
304294af0470SJeff Layton stat->attributes_mask |= (STATX_ATTR_CHANGE_MONOTONIC |
304394af0470SJeff Layton STATX_ATTR_ENCRYPTED);
304494af0470SJeff Layton
304558981784SJeff Layton stat->result_mask = request_mask & valid_mask;
3046355da1ebSSage Weil return err;
3047355da1ebSSage Weil }
30485d6451b1SJeff Layton
ceph_inode_shutdown(struct inode * inode)30495d6451b1SJeff Layton void ceph_inode_shutdown(struct inode *inode)
30505d6451b1SJeff Layton {
30515d6451b1SJeff Layton struct ceph_inode_info *ci = ceph_inode(inode);
30525d6451b1SJeff Layton struct rb_node *p;
30535d6451b1SJeff Layton int iputs = 0;
30545d6451b1SJeff Layton bool invalidate = false;
30555d6451b1SJeff Layton
30565d6451b1SJeff Layton spin_lock(&ci->i_ceph_lock);
30575d6451b1SJeff Layton ci->i_ceph_flags |= CEPH_I_SHUTDOWN;
30585d6451b1SJeff Layton p = rb_first(&ci->i_caps);
30595d6451b1SJeff Layton while (p) {
30605d6451b1SJeff Layton struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node);
30615d6451b1SJeff Layton
30625d6451b1SJeff Layton p = rb_next(p);
30635d6451b1SJeff Layton iputs += ceph_purge_inode_cap(inode, cap, &invalidate);
30645d6451b1SJeff Layton }
30655d6451b1SJeff Layton spin_unlock(&ci->i_ceph_lock);
30665d6451b1SJeff Layton
30675d6451b1SJeff Layton if (invalidate)
30685d6451b1SJeff Layton ceph_queue_invalidate(inode);
30695d6451b1SJeff Layton while (iputs--)
30705d6451b1SJeff Layton iput(inode);
30715d6451b1SJeff Layton }
3072