4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <linux/sched.h>
36 #include <linux/quotaops.h>
37 #include <linux/highmem.h>
38 #include <linux/pagemap.h>
39 #include <linux/security.h>
41 #define DEBUG_SUBSYSTEM S_LLITE
43 #include <obd_support.h>
44 #include <lustre_fid.h>
45 #include <lustre_dlm.h>
46 #include "llite_internal.h"
48 static int ll_create_it(struct inode *dir, struct dentry *dentry,
49 struct lookup_intent *it);
51 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
52 static int ll_test_inode(struct inode *inode, void *opaque)
54 struct ll_inode_info *lli = ll_i2info(inode);
55 struct lustre_md *md = opaque;
57 if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
58 CERROR("MDS body missing FID\n");
62 if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
68 static int ll_set_inode(struct inode *inode, void *opaque)
70 struct ll_inode_info *lli = ll_i2info(inode);
71 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
73 if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
74 CERROR("MDS body missing FID\n");
78 lli->lli_fid = body->mbo_fid1;
79 if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
80 CERROR("Can not initialize inode " DFID
81 " without object type: valid = %#llx\n",
82 PFID(&lli->lli_fid), body->mbo_valid);
86 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
87 if (unlikely(inode->i_mode == 0)) {
88 CERROR("Invalid inode " DFID " type\n", PFID(&lli->lli_fid));
98 * Get an inode by inode number(@hash), which is already instantiated by
101 struct inode *ll_iget(struct super_block *sb, ino_t hash,
102 struct lustre_md *md)
108 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
110 return ERR_PTR(-ENOMEM);
112 if (inode->i_state & I_NEW) {
113 rc = ll_read_inode2(inode, md);
114 if (!rc && S_ISREG(inode->i_mode) &&
115 !ll_i2info(inode)->lli_clob)
116 rc = cl_file_inode_init(inode, md);
120 * Let's clear directory lsm here, otherwise
121 * make_bad_inode() will reset the inode mode
122 * to regular, then ll_clear_inode will not
123 * be able to clear lsm_md
125 if (S_ISDIR(inode->i_mode))
126 ll_dir_clear_lsm_md(inode);
127 make_bad_inode(inode);
128 unlock_new_inode(inode);
132 unlock_new_inode(inode);
134 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
135 rc = ll_update_inode(inode, md);
136 CDEBUG(D_VFSTRACE, "got inode: " DFID "(%p): rc = %d\n",
137 PFID(&md->body->mbo_fid1), inode, rc);
139 if (S_ISDIR(inode->i_mode))
140 ll_dir_clear_lsm_md(inode);
148 static void ll_invalidate_negative_children(struct inode *dir)
150 struct dentry *dentry, *tmp_subdir;
152 spin_lock(&dir->i_lock);
153 hlist_for_each_entry(dentry, &dir->i_dentry, d_u.d_alias) {
154 spin_lock(&dentry->d_lock);
155 if (!list_empty(&dentry->d_subdirs)) {
156 struct dentry *child;
158 list_for_each_entry_safe(child, tmp_subdir,
161 if (d_really_is_negative(child))
162 d_lustre_invalidate(child, 1);
165 spin_unlock(&dentry->d_lock);
167 spin_unlock(&dir->i_lock);
170 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
172 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
175 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
176 void *data, int flag)
178 struct lustre_handle lockh;
182 case LDLM_CB_BLOCKING:
183 ldlm_lock2handle(lock, &lockh);
184 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
186 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
190 case LDLM_CB_CANCELING: {
191 struct inode *inode = ll_inode_from_resource_lock(lock);
192 __u64 bits = lock->l_policy_data.l_inodebits.bits;
194 /* Inode is set to lock->l_resource->lr_lvb_inode
195 * for mdc - bug 24555
197 LASSERT(!lock->l_ast_data);
202 /* Invalidate all dentries associated with this inode */
203 LASSERT(ldlm_is_canceling(lock));
205 if (!fid_res_name_eq(ll_inode2fid(inode),
206 &lock->l_resource->lr_name)) {
207 LDLM_ERROR(lock, "data mismatch with object " DFID "(%p)",
208 PFID(ll_inode2fid(inode)), inode);
212 if (bits & MDS_INODELOCK_XATTR) {
213 if (S_ISDIR(inode->i_mode))
214 ll_i2info(inode)->lli_def_stripe_offset = -1;
215 ll_xattr_cache_destroy(inode);
216 bits &= ~MDS_INODELOCK_XATTR;
219 /* For OPEN locks we differentiate between lock modes
220 * LCK_CR, LCK_CW, LCK_PR - bug 22891
222 if (bits & MDS_INODELOCK_OPEN)
223 ll_have_md_lock(inode, &bits, lock->l_req_mode);
225 if (bits & MDS_INODELOCK_OPEN) {
228 switch (lock->l_req_mode) {
239 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
243 ll_md_real_close(inode, fmode);
246 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
247 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
248 ll_have_md_lock(inode, &bits, LCK_MINMODE);
250 if (bits & MDS_INODELOCK_LAYOUT) {
251 struct cl_object_conf conf = {
252 .coc_opc = OBJECT_CONF_INVALIDATE,
256 rc = ll_layout_conf(inode, &conf);
258 CDEBUG(D_INODE, "cannot invalidate layout of "
260 PFID(ll_inode2fid(inode)), rc);
263 if (bits & MDS_INODELOCK_UPDATE) {
264 struct ll_inode_info *lli = ll_i2info(inode);
266 spin_lock(&lli->lli_lock);
267 LTIME_S(inode->i_mtime) = 0;
268 LTIME_S(inode->i_atime) = 0;
269 LTIME_S(inode->i_ctime) = 0;
270 spin_unlock(&lli->lli_lock);
273 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
274 struct ll_inode_info *lli = ll_i2info(inode);
276 CDEBUG(D_INODE, "invalidating inode " DFID " lli = %p, pfid = " DFID "\n",
277 PFID(ll_inode2fid(inode)), lli,
278 PFID(&lli->lli_pfid));
280 truncate_inode_pages(inode->i_mapping, 0);
282 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
283 struct inode *master_inode = NULL;
287 * This is slave inode, since all of the child
288 * dentry is connected on the master inode, so
289 * we have to invalidate the negative children
292 CDEBUG(D_INODE, "Invalidate s" DFID " m" DFID "\n",
293 PFID(ll_inode2fid(inode)),
294 PFID(&lli->lli_pfid));
296 hash = cl_fid_build_ino(&lli->lli_pfid,
297 ll_need_32bit_api(ll_i2sbi(inode)));
299 * Do not lookup the inode with ilookup5,
300 * otherwise it will cause dead lock,
302 * 1. Client1 send chmod req to the MDT0, then
303 * on MDT0, it enqueues master and all of its
304 * slaves lock, (mdt_attr_set() ->
305 * mdt_lock_slaves()), after gets master and
306 * stripe0 lock, it will send the enqueue req
307 * (for stripe1) to MDT1, then MDT1 finds the
308 * lock has been granted to client2. Then MDT1
309 * sends blocking ast to client2.
311 * 2. At the same time, client2 tries to unlink
312 * the striped dir (rm -rf striped_dir), and
313 * during lookup, it will hold the master inode
314 * of the striped directory, whose inode state
315 * is NEW, then tries to revalidate all of its
316 * slaves, (ll_prep_inode()->ll_iget()->
317 * ll_read_inode2()-> ll_update_inode().). And
318 * it will be blocked on the server side because
321 * 3. Then the client get the blocking_ast req,
322 * cancel the lock, but being blocked if using
323 * ->ilookup5()), because master inode state is
326 master_inode = ilookup5_nowait(inode->i_sb,
328 ll_test_inode_by_fid,
329 (void *)&lli->lli_pfid);
331 ll_invalidate_negative_children(master_inode);
335 ll_invalidate_negative_children(inode);
339 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
340 inode->i_sb->s_root &&
341 !is_root_inode(inode))
342 ll_invalidate_aliases(inode);
354 __u32 ll_i2suppgid(struct inode *i)
356 if (in_group_p(i->i_gid))
357 return (__u32)from_kgid(&init_user_ns, i->i_gid);
362 /* Pack the required supplementary groups into the supplied groups array.
363 * If we don't need to use the groups from the target inode(s) then we
364 * instead pack one or more groups from the user's supplementary group
365 * array in case it might be useful. Not needed if doing an MDS-side upcall.
367 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
371 suppgids[0] = ll_i2suppgid(i1);
374 suppgids[1] = ll_i2suppgid(i2);
380 * try to reuse three types of dentry:
381 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
382 * by concurrent .revalidate).
383 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
384 * be cleared by others calling d_lustre_revalidate).
385 * 3. DISCONNECTED alias.
387 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
389 struct dentry *alias, *discon_alias, *invalid_alias;
391 if (hlist_empty(&inode->i_dentry))
395 invalid_alias = NULL;
397 spin_lock(&inode->i_lock);
398 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
399 LASSERT(alias != dentry);
401 spin_lock(&alias->d_lock);
402 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
403 S_ISDIR(inode->i_mode))
404 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
405 discon_alias = alias;
406 else if (alias->d_parent == dentry->d_parent &&
407 alias->d_name.hash == dentry->d_name.hash &&
408 alias->d_name.len == dentry->d_name.len &&
409 memcmp(alias->d_name.name, dentry->d_name.name,
410 dentry->d_name.len) == 0)
411 invalid_alias = alias;
412 spin_unlock(&alias->d_lock);
417 alias = invalid_alias ?: discon_alias ?: NULL;
419 spin_lock(&alias->d_lock);
421 spin_unlock(&alias->d_lock);
423 spin_unlock(&inode->i_lock);
429 * Similar to d_splice_alias(), but lustre treats invalid alias
430 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
432 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
435 struct dentry *new = ll_find_alias(inode, de);
441 "Reuse dentry %p inode %p refc %d flags %#x\n",
442 new, d_inode(new), d_count(new), new->d_flags);
447 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
448 de, d_inode(de), d_count(de), de->d_flags);
452 static int ll_lookup_it_finish(struct ptlrpc_request *request,
453 struct lookup_intent *it,
454 struct inode *parent, struct dentry **de)
456 struct inode *inode = NULL;
459 struct dentry *alias;
461 /* NB 1 request reference will be taken away by ll_intent_lock()
464 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
466 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
467 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
471 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
473 /* We used to query real size from OSTs here, but actually
474 * this is not needed. For stat() calls size would be updated
475 * from subsequent do_revalidate()->ll_inode_revalidate_it() in
477 * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
478 * Everybody else who needs correct file size would call
479 * ll_glimpse_size or some equivalent themselves anyway.
484 alias = ll_splice_alias(inode, *de);
491 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
492 /* We have the "lookup" lock, so unhide dentry */
493 if (bits & MDS_INODELOCK_LOOKUP)
494 d_lustre_revalidate(*de);
495 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
496 /* If file created on server, don't depend on parent UPDATE
497 * lock to unhide it. It is left hidden and next lookup can
498 * find it in ll_splice_alias.
500 /* Check that parent has UPDATE lock. */
501 struct lookup_intent parent_it = {
503 .it_lock_handle = 0 };
504 struct lu_fid fid = ll_i2info(parent)->lli_fid;
506 /* If it is striped directory, get the real stripe parent */
507 if (unlikely(ll_i2info(parent)->lli_lsm_md)) {
508 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
509 ll_i2info(parent)->lli_lsm_md,
511 (*de)->d_name.len, &fid);
516 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
518 d_lustre_revalidate(*de);
519 ll_intent_release(&parent_it);
524 if (rc != 0 && it->it_op & IT_OPEN)
525 ll_open_cleanup((*de)->d_sb, request);
530 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
531 struct lookup_intent *it)
533 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
534 struct dentry *save = dentry, *retval;
535 struct ptlrpc_request *req = NULL;
536 struct md_op_data *op_data = NULL;
541 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
542 return ERR_PTR(-ENAMETOOLONG);
544 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),intent=%s\n",
545 dentry, PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
547 if (d_mountpoint(dentry))
548 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
550 if (!it || it->it_op == IT_GETXATTR)
553 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
554 rc = ll_statahead(parent, &dentry, 0);
564 if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE && sb_rdonly(dentry->d_sb))
565 return ERR_PTR(-EROFS);
567 if (it->it_op & IT_CREAT)
568 opc = LUSTRE_OPC_CREATE;
570 opc = LUSTRE_OPC_ANY;
572 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
573 dentry->d_name.len, 0, opc, NULL);
575 return (void *)op_data;
577 /* enforce umask if acl disabled or MDS doesn't support umask */
578 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
579 it->it_create_mode &= ~current_umask();
581 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
582 &ll_md_blocking_ast, 0);
584 * If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
585 * client does not know which suppgid should be sent to the MDS, or
586 * some other(s) changed the target file's GID after this RPC sent
587 * to the MDS with the suppgid as the original GID, then we should
588 * try again with right suppgid.
590 if (rc == -EACCES && it->it_op & IT_OPEN &&
591 it_disposition(it, DISP_OPEN_DENY)) {
592 struct mdt_body *body;
596 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
597 if (op_data->op_suppgids[0] == body->mbo_gid ||
598 op_data->op_suppgids[1] == body->mbo_gid ||
599 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid))) {
600 retval = ERR_PTR(-EACCES);
604 fid_zero(&op_data->op_fid2);
605 op_data->op_suppgids[1] = body->mbo_gid;
606 ptlrpc_req_finished(req);
608 ll_intent_release(it);
609 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
610 ll_md_blocking_ast, 0);
614 retval = ERR_PTR(rc);
618 rc = ll_lookup_it_finish(req, it, parent, &dentry);
620 ll_intent_release(it);
621 retval = ERR_PTR(rc);
625 inode = d_inode(dentry);
626 if ((it->it_op & IT_OPEN) && inode &&
627 !S_ISREG(inode->i_mode) &&
628 !S_ISDIR(inode->i_mode)) {
629 ll_release_openhandle(inode, it);
631 ll_lookup_finish_locks(it, inode);
638 if (op_data && !IS_ERR(op_data))
639 ll_finish_md_op_data(op_data);
641 ptlrpc_req_finished(req);
645 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
648 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
651 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),flags=%u\n",
652 dentry, PFID(ll_inode2fid(parent)), parent, flags);
654 /* Optimize away (CREATE && !OPEN). Let .create handle the race.
655 * but only if we have write permissions there, otherwise we need
656 * to proceed with lookup. LU-4185
658 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
659 (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
662 if (flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
666 de = ll_lookup_it(parent, dentry, itp);
669 ll_intent_release(itp);
675 * For cached negative dentry and new dentry, handle lookup/create/open
678 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
679 struct file *file, unsigned int open_flags,
680 umode_t mode, int *opened)
682 struct lookup_intent *it;
686 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),file %p,open_flags %x,mode %x opened %d\n",
687 dentry, PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
690 /* Only negative dentries enter here */
691 LASSERT(!d_inode(dentry));
693 if (!d_in_lookup(dentry)) {
694 /* A valid negative dentry that just passed revalidation,
695 * there's little point to try and open it server-side,
696 * even though there's a minuscle chance it might succeed.
697 * Either way it's a valid race to just return -ENOENT here.
699 if (!(open_flags & O_CREAT))
702 /* Otherwise we just unhash it to be rehashed afresh via
703 * lookup if necessary
708 it = kzalloc(sizeof(*it), GFP_NOFS);
713 if (open_flags & O_CREAT)
714 it->it_op |= IT_CREAT;
715 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
716 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
717 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
719 /* Dentry added to dcache tree in ll_lookup_it */
720 de = ll_lookup_it(dir, dentry, it);
727 if (it_disposition(it, DISP_OPEN_CREATE)) {
728 /* Dentry instantiated in ll_create_it. */
729 rc = ll_create_it(dir, dentry, it);
731 /* We dget in ll_splice_alias. */
737 *opened |= FILE_CREATED;
739 if (d_really_is_positive(dentry) && it_disposition(it, DISP_OPEN_OPEN)) {
741 if (S_ISFIFO(d_inode(dentry)->i_mode)) {
742 /* We cannot call open here as it might
743 * deadlock. This case is unreachable in
744 * practice because of OBD_CONNECT_NODEVOH.
746 rc = finish_no_open(file, de);
748 file->private_data = it;
749 rc = finish_open(file, dentry, NULL, opened);
750 /* We dget in ll_splice_alias. finish_open takes
751 * care of dget for fd open.
757 rc = finish_no_open(file, de);
762 ll_intent_release(it);
768 /* We depend on "mode" being set with the proper file type/umask by now */
769 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
771 struct inode *inode = NULL;
772 struct ptlrpc_request *request = NULL;
773 struct ll_sb_info *sbi = ll_i2sbi(dir);
776 LASSERT(it && it->it_disposition);
778 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
779 request = it->it_request;
780 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
781 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
787 LASSERT(hlist_empty(&inode->i_dentry));
789 /* We asked for a lock on the directory, but were granted a
790 * lock on the inode. Since we finally have an inode pointer,
791 * stuff it in the lock.
793 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode " DFID "(%p)\n",
794 PFID(ll_inode2fid(dir)), inode);
795 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
797 ptlrpc_req_finished(request);
802 * By the time this is called, we already have created the directory cache
803 * entry for the new file, but it is so far negative - it has no inode.
805 * We defer creating the OBD object(s) until open, to keep the intent and
806 * non-intent code paths similar, and also because we do not have the MDS
807 * inode number before calling ll_create_node() (which is needed for LOV),
808 * so we would need to do yet another RPC to the MDS to store the LOV EA
809 * data on the MDS. If needed, we would pass the PACKED lmm as data and
810 * lmm_size in datalen (the MDS still has code which will handle that).
812 * If the create succeeds, we fill in the inode information
813 * with d_instantiate().
815 static int ll_create_it(struct inode *dir, struct dentry *dentry,
816 struct lookup_intent *it)
821 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p), intent=%s\n",
822 dentry, PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
824 rc = it_open_error(DISP_OPEN_CREATE, it);
828 inode = ll_create_node(dir, it);
830 return PTR_ERR(inode);
832 d_instantiate(dentry, inode);
834 return ll_init_security(dentry, inode, dir);
837 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
839 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
843 if (body->mbo_valid & OBD_MD_FLMTIME &&
844 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
845 CDEBUG(D_INODE, "setting fid " DFID " mtime from %lu to %llu\n",
846 PFID(ll_inode2fid(inode)), LTIME_S(inode->i_mtime),
848 LTIME_S(inode->i_mtime) = body->mbo_mtime;
850 if (body->mbo_valid & OBD_MD_FLCTIME &&
851 body->mbo_ctime > LTIME_S(inode->i_ctime))
852 LTIME_S(inode->i_ctime) = body->mbo_ctime;
855 static int ll_new_node(struct inode *dir, struct dentry *dentry,
856 const char *tgt, umode_t mode, int rdev,
859 struct ptlrpc_request *request = NULL;
860 struct md_op_data *op_data;
861 struct inode *inode = NULL;
862 struct ll_sb_info *sbi = ll_i2sbi(dir);
867 tgt_len = strlen(tgt) + 1;
869 op_data = ll_prep_md_op_data(NULL, dir, NULL,
873 if (IS_ERR(op_data)) {
874 err = PTR_ERR(op_data);
878 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
879 from_kuid(&init_user_ns, current_fsuid()),
880 from_kgid(&init_user_ns, current_fsgid()),
881 cfs_curproc_cap_pack(), rdev, &request);
882 ll_finish_md_op_data(op_data);
883 if (err < 0 && err != -EREMOTE)
887 * If the client doesn't know where to create a subdirectory (or
888 * in case of a race that sends the RPC to the wrong MDS), the
889 * MDS will return -EREMOTE and the client will fetch the layout
890 * of the directory, then create the directory on the right MDT.
892 if (unlikely(err == -EREMOTE)) {
893 struct ll_inode_info *lli = ll_i2info(dir);
894 struct lmv_user_md *lum;
897 ptlrpc_req_finished(request);
900 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
903 /* Update stripe_offset and retry */
904 lli->lli_def_stripe_offset = lum->lum_stripe_offset;
905 } else if (err2 == -ENODATA &&
906 lli->lli_def_stripe_offset != -1) {
908 * If there are no default stripe EA on the MDT, but the
909 * client has default stripe, then it probably means
910 * default stripe EA has just been deleted.
912 lli->lli_def_stripe_offset = -1;
917 ptlrpc_req_finished(request);
922 ll_update_times(request, dir);
924 err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
928 d_instantiate(dentry, inode);
930 err = ll_init_security(dentry, inode, dir);
933 ptlrpc_req_finished(request);
938 static int ll_mknod(struct inode *dir, struct dentry *dchild,
939 umode_t mode, dev_t rdev)
943 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p) mode %o dev %x\n",
944 dchild, PFID(ll_inode2fid(dir)), dir, mode,
945 old_encode_dev(rdev));
947 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
948 mode &= ~current_umask();
950 switch (mode & S_IFMT) {
952 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
958 err = ll_new_node(dir, dchild, NULL, mode,
959 old_encode_dev(rdev),
970 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
976 * Plain create. Intent create is handled in atomic_open.
978 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
979 umode_t mode, bool want_excl)
983 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p), flags=%u, excl=%d\n",
984 dentry, PFID(ll_inode2fid(dir)), dir, mode, want_excl);
986 rc = ll_mknod(dir, dentry, mode, 0);
988 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
990 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
991 dentry, d_unhashed(dentry));
996 static int ll_unlink(struct inode *dir, struct dentry *dchild)
998 struct ptlrpc_request *request = NULL;
999 struct md_op_data *op_data;
1002 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
1003 dchild, dir->i_ino, dir->i_generation, dir);
1005 op_data = ll_prep_md_op_data(NULL, dir, NULL,
1006 dchild->d_name.name,
1008 0, LUSTRE_OPC_ANY, NULL);
1009 if (IS_ERR(op_data))
1010 return PTR_ERR(op_data);
1012 if (dchild->d_inode)
1013 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1015 op_data->op_fid2 = op_data->op_fid3;
1016 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1017 ll_finish_md_op_data(op_data);
1021 ll_update_times(request, dir);
1022 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1025 ptlrpc_req_finished(request);
1029 static int ll_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1033 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir" DFID "(%p)\n",
1034 dentry, PFID(ll_inode2fid(dir)), dir);
1036 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1037 mode &= ~current_umask();
1038 mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
1040 err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1042 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1047 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1049 struct ptlrpc_request *request = NULL;
1050 struct md_op_data *op_data;
1053 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p)\n",
1054 dchild, PFID(ll_inode2fid(dir)), dir);
1056 op_data = ll_prep_md_op_data(NULL, dir, NULL,
1057 dchild->d_name.name,
1059 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1060 if (IS_ERR(op_data))
1061 return PTR_ERR(op_data);
1063 if (dchild->d_inode)
1064 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1066 op_data->op_fid2 = op_data->op_fid3;
1067 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1068 ll_finish_md_op_data(op_data);
1070 ll_update_times(request, dir);
1071 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1074 ptlrpc_req_finished(request);
1078 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1079 const char *oldname)
1083 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir=" DFID "(%p),target=%.*s\n",
1084 dentry, PFID(ll_inode2fid(dir)), dir, 3000, oldname);
1086 err = ll_new_node(dir, dentry, oldname, S_IFLNK | 0777,
1087 0, LUSTRE_OPC_SYMLINK);
1090 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1095 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1096 struct dentry *new_dentry)
1098 struct inode *src = d_inode(old_dentry);
1099 struct ll_sb_info *sbi = ll_i2sbi(dir);
1100 struct ptlrpc_request *request = NULL;
1101 struct md_op_data *op_data;
1104 CDEBUG(D_VFSTRACE, "VFS Op: inode=" DFID "(%p), dir=" DFID "(%p), target=%pd\n",
1105 PFID(ll_inode2fid(src)), src, PFID(ll_inode2fid(dir)), dir,
1108 op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1109 new_dentry->d_name.len,
1110 0, LUSTRE_OPC_ANY, NULL);
1111 if (IS_ERR(op_data))
1112 return PTR_ERR(op_data);
1114 err = md_link(sbi->ll_md_exp, op_data, &request);
1115 ll_finish_md_op_data(op_data);
1119 ll_update_times(request, dir);
1120 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1122 ptlrpc_req_finished(request);
1126 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1127 struct inode *tgt, struct dentry *tgt_dchild,
1130 struct ptlrpc_request *request = NULL;
1131 struct ll_sb_info *sbi = ll_i2sbi(src);
1132 struct md_op_data *op_data;
1139 "VFS Op:oldname=%pd, src_dir=" DFID "(%p), newname=%pd, tgt_dir=" DFID "(%p)\n",
1140 src_dchild, PFID(ll_inode2fid(src)), src,
1141 tgt_dchild, PFID(ll_inode2fid(tgt)), tgt);
1143 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1144 LUSTRE_OPC_ANY, NULL);
1145 if (IS_ERR(op_data))
1146 return PTR_ERR(op_data);
1148 if (src_dchild->d_inode)
1149 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1150 if (tgt_dchild->d_inode)
1151 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1153 err = md_rename(sbi->ll_md_exp, op_data,
1154 src_dchild->d_name.name,
1155 src_dchild->d_name.len,
1156 tgt_dchild->d_name.name,
1157 tgt_dchild->d_name.len, &request);
1158 ll_finish_md_op_data(op_data);
1160 ll_update_times(request, src);
1161 ll_update_times(request, tgt);
1162 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1165 ptlrpc_req_finished(request);
1167 d_move(src_dchild, tgt_dchild);
1171 const struct inode_operations ll_dir_inode_operations = {
1173 .atomic_open = ll_atomic_open,
1174 .lookup = ll_lookup_nd,
1175 .create = ll_create_nd,
1176 /* We need all these non-raw things for NFSD, to not patch it. */
1177 .unlink = ll_unlink,
1180 .symlink = ll_symlink,
1182 .rename = ll_rename,
1183 .setattr = ll_setattr,
1184 .getattr = ll_getattr,
1185 .permission = ll_inode_permission,
1186 .listxattr = ll_listxattr,
1187 .get_acl = ll_get_acl,
1190 const struct inode_operations ll_special_inode_operations = {
1191 .setattr = ll_setattr,
1192 .getattr = ll_getattr,
1193 .permission = ll_inode_permission,
1194 .listxattr = ll_listxattr,
1195 .get_acl = ll_get_acl,