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 "../include/obd_support.h"
44 #include "../include/lustre_fid.h"
45 #include "../include/lustre_dlm.h"
46 #include "../include/lustre_ver.h"
47 #include "llite_internal.h"
49 static int ll_create_it(struct inode *dir, struct dentry *dentry,
50 struct lookup_intent *it);
52 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
53 static int ll_test_inode(struct inode *inode, void *opaque)
55 struct ll_inode_info *lli = ll_i2info(inode);
56 struct lustre_md *md = opaque;
58 if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
59 CERROR("MDS body missing FID\n");
63 if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
69 static int ll_set_inode(struct inode *inode, void *opaque)
71 struct ll_inode_info *lli = ll_i2info(inode);
72 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
74 if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
75 CERROR("MDS body missing FID\n");
79 lli->lli_fid = body->mbo_fid1;
80 if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
81 CERROR("Can not initialize inode " DFID
82 " without object type: valid = %#llx\n",
83 PFID(&lli->lli_fid), body->mbo_valid);
87 inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
88 if (unlikely(inode->i_mode == 0)) {
89 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
99 * Get an inode by inode number(@hash), which is already instantiated by
100 * the intent lookup).
102 struct inode *ll_iget(struct super_block *sb, ino_t hash,
103 struct lustre_md *md)
109 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
111 return ERR_PTR(-ENOMEM);
113 if (inode->i_state & I_NEW) {
114 rc = ll_read_inode2(inode, md);
115 if (!rc && S_ISREG(inode->i_mode) &&
116 !ll_i2info(inode)->lli_clob)
117 rc = cl_file_inode_init(inode, md);
121 * Let's clear directory lsm here, otherwise
122 * make_bad_inode() will reset the inode mode
123 * to regular, then ll_clear_inode will not
124 * be able to clear lsm_md
126 if (S_ISDIR(inode->i_mode))
127 ll_dir_clear_lsm_md(inode);
128 make_bad_inode(inode);
129 unlock_new_inode(inode);
133 unlock_new_inode(inode);
135 } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
136 rc = ll_update_inode(inode, md);
137 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
138 PFID(&md->body->mbo_fid1), inode, rc);
140 if (S_ISDIR(inode->i_mode))
141 ll_dir_clear_lsm_md(inode);
149 static void ll_invalidate_negative_children(struct inode *dir)
151 struct dentry *dentry, *tmp_subdir;
153 spin_lock(&dir->i_lock);
154 hlist_for_each_entry(dentry, &dir->i_dentry, d_u.d_alias) {
155 spin_lock(&dentry->d_lock);
156 if (!list_empty(&dentry->d_subdirs)) {
157 struct dentry *child;
159 list_for_each_entry_safe(child, tmp_subdir,
162 if (d_really_is_negative(child))
163 d_lustre_invalidate(child, 1);
166 spin_unlock(&dentry->d_lock);
168 spin_unlock(&dir->i_lock);
171 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
173 return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
176 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
177 void *data, int flag)
179 struct lustre_handle lockh;
183 case LDLM_CB_BLOCKING:
184 ldlm_lock2handle(lock, &lockh);
185 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
187 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
191 case LDLM_CB_CANCELING: {
192 struct inode *inode = ll_inode_from_resource_lock(lock);
193 __u64 bits = lock->l_policy_data.l_inodebits.bits;
195 /* Inode is set to lock->l_resource->lr_lvb_inode
196 * for mdc - bug 24555
198 LASSERT(!lock->l_ast_data);
203 /* Invalidate all dentries associated with this inode */
204 LASSERT(ldlm_is_canceling(lock));
206 if (!fid_res_name_eq(ll_inode2fid(inode),
207 &lock->l_resource->lr_name)) {
208 LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
209 PFID(ll_inode2fid(inode)), inode);
213 if (bits & MDS_INODELOCK_XATTR) {
214 if (S_ISDIR(inode->i_mode))
215 ll_i2info(inode)->lli_def_stripe_offset = -1;
216 ll_xattr_cache_destroy(inode);
217 bits &= ~MDS_INODELOCK_XATTR;
220 /* For OPEN locks we differentiate between lock modes
221 * LCK_CR, LCK_CW, LCK_PR - bug 22891
223 if (bits & MDS_INODELOCK_OPEN)
224 ll_have_md_lock(inode, &bits, lock->l_req_mode);
226 if (bits & MDS_INODELOCK_OPEN) {
229 switch (lock->l_req_mode) {
240 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
244 ll_md_real_close(inode, fmode);
247 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
248 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
249 ll_have_md_lock(inode, &bits, LCK_MINMODE);
251 if (bits & MDS_INODELOCK_LAYOUT) {
252 struct cl_object_conf conf = {
253 .coc_opc = OBJECT_CONF_INVALIDATE,
257 rc = ll_layout_conf(inode, &conf);
259 CDEBUG(D_INODE, "cannot invalidate layout of "
261 PFID(ll_inode2fid(inode)), rc);
264 if (bits & MDS_INODELOCK_UPDATE) {
265 struct ll_inode_info *lli = ll_i2info(inode);
267 spin_lock(&lli->lli_lock);
268 LTIME_S(inode->i_mtime) = 0;
269 LTIME_S(inode->i_atime) = 0;
270 LTIME_S(inode->i_ctime) = 0;
271 spin_unlock(&lli->lli_lock);
274 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
275 struct ll_inode_info *lli = ll_i2info(inode);
277 CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, pfid = "DFID"\n",
278 PFID(ll_inode2fid(inode)), lli,
279 PFID(&lli->lli_pfid));
281 truncate_inode_pages(inode->i_mapping, 0);
283 if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
284 struct inode *master_inode = NULL;
288 * This is slave inode, since all of the child
289 * dentry is connected on the master inode, so
290 * we have to invalidate the negative children
293 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
294 PFID(ll_inode2fid(inode)),
295 PFID(&lli->lli_pfid));
297 hash = cl_fid_build_ino(&lli->lli_pfid,
298 ll_need_32bit_api(ll_i2sbi(inode)));
300 * Do not lookup the inode with ilookup5,
301 * otherwise it will cause dead lock,
303 * 1. Client1 send chmod req to the MDT0, then
304 * on MDT0, it enqueues master and all of its
305 * slaves lock, (mdt_attr_set() ->
306 * mdt_lock_slaves()), after gets master and
307 * stripe0 lock, it will send the enqueue req
308 * (for stripe1) to MDT1, then MDT1 finds the
309 * lock has been granted to client2. Then MDT1
310 * sends blocking ast to client2.
312 * 2. At the same time, client2 tries to unlink
313 * the striped dir (rm -rf striped_dir), and
314 * during lookup, it will hold the master inode
315 * of the striped directory, whose inode state
316 * is NEW, then tries to revalidate all of its
317 * slaves, (ll_prep_inode()->ll_iget()->
318 * ll_read_inode2()-> ll_update_inode().). And
319 * it will be blocked on the server side because
322 * 3. Then the client get the blocking_ast req,
323 * cancel the lock, but being blocked if using
324 * ->ilookup5()), because master inode state is
327 master_inode = ilookup5_nowait(inode->i_sb,
329 ll_test_inode_by_fid,
330 (void *)&lli->lli_pfid);
332 ll_invalidate_negative_children(master_inode);
336 ll_invalidate_negative_children(inode);
340 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
341 inode->i_sb->s_root &&
342 !is_root_inode(inode))
343 ll_invalidate_aliases(inode);
355 __u32 ll_i2suppgid(struct inode *i)
357 if (in_group_p(i->i_gid))
358 return (__u32)from_kgid(&init_user_ns, i->i_gid);
363 /* Pack the required supplementary groups into the supplied groups array.
364 * If we don't need to use the groups from the target inode(s) then we
365 * instead pack one or more groups from the user's supplementary group
366 * array in case it might be useful. Not needed if doing an MDS-side upcall.
368 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
372 suppgids[0] = ll_i2suppgid(i1);
375 suppgids[1] = ll_i2suppgid(i2);
381 * try to reuse three types of dentry:
382 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
383 * by concurrent .revalidate).
384 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
385 * be cleared by others calling d_lustre_revalidate).
386 * 3. DISCONNECTED alias.
388 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
390 struct dentry *alias, *discon_alias, *invalid_alias;
392 if (hlist_empty(&inode->i_dentry))
396 invalid_alias = NULL;
398 spin_lock(&inode->i_lock);
399 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
400 LASSERT(alias != dentry);
402 spin_lock(&alias->d_lock);
403 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
404 S_ISDIR(inode->i_mode))
405 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
406 discon_alias = alias;
407 else if (alias->d_parent == dentry->d_parent &&
408 alias->d_name.hash == dentry->d_name.hash &&
409 alias->d_name.len == dentry->d_name.len &&
410 memcmp(alias->d_name.name, dentry->d_name.name,
411 dentry->d_name.len) == 0)
412 invalid_alias = alias;
413 spin_unlock(&alias->d_lock);
418 alias = invalid_alias ?: discon_alias ?: NULL;
420 spin_lock(&alias->d_lock);
422 spin_unlock(&alias->d_lock);
424 spin_unlock(&inode->i_lock);
430 * Similar to d_splice_alias(), but lustre treats invalid alias
431 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
433 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
436 struct dentry *new = ll_find_alias(inode, de);
442 "Reuse dentry %p inode %p refc %d flags %#x\n",
443 new, d_inode(new), d_count(new), new->d_flags);
448 CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
449 de, d_inode(de), d_count(de), de->d_flags);
453 static int ll_lookup_it_finish(struct ptlrpc_request *request,
454 struct lookup_intent *it,
455 struct inode *parent, struct dentry **de)
457 struct inode *inode = NULL;
460 struct dentry *alias;
462 /* NB 1 request reference will be taken away by ll_intent_lock()
465 CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
467 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
468 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
472 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
474 /* We used to query real size from OSTs here, but actually
475 * this is not needed. For stat() calls size would be updated
476 * from subsequent do_revalidate()->ll_inode_revalidate_it() in
478 * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
479 * Everybody else who needs correct file size would call
480 * ll_glimpse_size or some equivalent themselves anyway.
485 alias = ll_splice_alias(inode, *de);
492 if (!it_disposition(it, DISP_LOOKUP_NEG)) {
493 /* we have lookup look - unhide dentry */
494 if (bits & MDS_INODELOCK_LOOKUP)
495 d_lustre_revalidate(*de);
496 } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
497 /* If file created on server, don't depend on parent UPDATE
498 * lock to unhide it. It is left hidden and next lookup can
499 * find it in ll_splice_alias.
501 /* Check that parent has UPDATE lock. */
502 struct lookup_intent parent_it = {
504 .it_lock_handle = 0 };
505 struct lu_fid fid = ll_i2info(parent)->lli_fid;
507 /* If it is striped directory, get the real stripe parent */
508 if (unlikely(ll_i2info(parent)->lli_lsm_md)) {
509 rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
510 ll_i2info(parent)->lli_lsm_md,
512 (*de)->d_name.len, &fid);
517 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
519 d_lustre_revalidate(*de);
520 ll_intent_release(&parent_it);
525 if (rc != 0 && it->it_op & IT_OPEN)
526 ll_open_cleanup((*de)->d_sb, request);
531 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
532 struct lookup_intent *it)
534 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
535 struct dentry *save = dentry, *retval;
536 struct ptlrpc_request *req = NULL;
537 struct md_op_data *op_data = NULL;
542 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
543 return ERR_PTR(-ENAMETOOLONG);
545 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),intent=%s\n",
546 dentry, PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
548 if (d_mountpoint(dentry))
549 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
551 if (!it || it->it_op == IT_GETXATTR)
554 if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
555 rc = ll_statahead(parent, &dentry, 0);
565 if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE &&
566 dentry->d_sb->s_flags & MS_RDONLY)
567 return ERR_PTR(-EROFS);
569 if (it->it_op & IT_CREAT)
570 opc = LUSTRE_OPC_CREATE;
572 opc = LUSTRE_OPC_ANY;
574 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
575 dentry->d_name.len, 0, opc, NULL);
577 return (void *)op_data;
579 /* enforce umask if acl disabled or MDS doesn't support umask */
580 if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
581 it->it_create_mode &= ~current_umask();
583 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
584 &ll_md_blocking_ast, 0);
586 * If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
587 * client does not know which suppgid should be sent to the MDS, or
588 * some other(s) changed the target file's GID after this RPC sent
589 * to the MDS with the suppgid as the original GID, then we should
590 * try again with right suppgid.
592 if (rc == -EACCES && it->it_op & IT_OPEN &&
593 it_disposition(it, DISP_OPEN_DENY)) {
594 struct mdt_body *body;
598 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
599 if (op_data->op_suppgids[0] == body->mbo_gid ||
600 op_data->op_suppgids[1] == body->mbo_gid ||
601 !in_group_p(make_kgid(&init_user_ns, body->mbo_gid))) {
602 retval = ERR_PTR(-EACCES);
606 fid_zero(&op_data->op_fid2);
607 op_data->op_suppgids[1] = body->mbo_gid;
608 ptlrpc_req_finished(req);
610 ll_intent_release(it);
611 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
612 ll_md_blocking_ast, 0);
616 retval = ERR_PTR(rc);
620 rc = ll_lookup_it_finish(req, it, parent, &dentry);
622 ll_intent_release(it);
623 retval = ERR_PTR(rc);
627 inode = d_inode(dentry);
628 if ((it->it_op & IT_OPEN) && inode &&
629 !S_ISREG(inode->i_mode) &&
630 !S_ISDIR(inode->i_mode)) {
631 ll_release_openhandle(inode, it);
633 ll_lookup_finish_locks(it, inode);
640 if (op_data && !IS_ERR(op_data))
641 ll_finish_md_op_data(op_data);
643 ptlrpc_req_finished(req);
647 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
650 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
653 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),flags=%u\n",
654 dentry, PFID(ll_inode2fid(parent)), parent, flags);
656 /* Optimize away (CREATE && !OPEN). Let .create handle the race.
657 * but only if we have write permissions there, otherwise we need
658 * to proceed with lookup. LU-4185
660 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
661 (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
664 if (flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
668 de = ll_lookup_it(parent, dentry, itp);
671 ll_intent_release(itp);
677 * For cached negative dentry and new dentry, handle lookup/create/open
680 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
681 struct file *file, unsigned open_flags,
682 umode_t mode, int *opened)
684 struct lookup_intent *it;
688 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),file %p,open_flags %x,mode %x opened %d\n",
689 dentry, PFID(ll_inode2fid(dir)), dir, file, open_flags, mode,
692 /* Only negative dentries enter here */
693 LASSERT(!d_inode(dentry));
695 if (!d_in_lookup(dentry)) {
696 /* A valid negative dentry that just passed revalidation,
697 * there's little point to try and open it server-side,
698 * even though there's a minuscle chance it might succeed.
699 * Either way it's a valid race to just return -ENOENT here.
701 if (!(open_flags & O_CREAT))
704 /* Otherwise we just unhash it to be rehashed afresh via
705 * lookup if necessary
710 it = kzalloc(sizeof(*it), GFP_NOFS);
715 if (open_flags & O_CREAT)
716 it->it_op |= IT_CREAT;
717 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
718 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
719 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
721 /* Dentry added to dcache tree in ll_lookup_it */
722 de = ll_lookup_it(dir, dentry, it);
729 if (it_disposition(it, DISP_OPEN_CREATE)) {
730 /* Dentry instantiated in ll_create_it. */
731 rc = ll_create_it(dir, dentry, it);
733 /* We dget in ll_splice_alias. */
739 *opened |= FILE_CREATED;
741 if (d_really_is_positive(dentry) && it_disposition(it, DISP_OPEN_OPEN)) {
743 if (S_ISFIFO(d_inode(dentry)->i_mode)) {
744 /* We cannot call open here as it might
745 * deadlock. This case is unreachable in
746 * practice because of OBD_CONNECT_NODEVOH.
748 rc = finish_no_open(file, de);
750 file->private_data = it;
751 rc = finish_open(file, dentry, NULL, opened);
752 /* We dget in ll_splice_alias. finish_open takes
753 * care of dget for fd open.
759 rc = finish_no_open(file, de);
764 ll_intent_release(it);
770 /* We depend on "mode" being set with the proper file type/umask by now */
771 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
773 struct inode *inode = NULL;
774 struct ptlrpc_request *request = NULL;
775 struct ll_sb_info *sbi = ll_i2sbi(dir);
778 LASSERT(it && it->it_disposition);
780 LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
781 request = it->it_request;
782 it_clear_disposition(it, DISP_ENQ_CREATE_REF);
783 rc = ll_prep_inode(&inode, request, dir->i_sb, it);
789 LASSERT(hlist_empty(&inode->i_dentry));
791 /* We asked for a lock on the directory, but were granted a
792 * lock on the inode. Since we finally have an inode pointer,
793 * stuff it in the lock.
795 CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
796 PFID(ll_inode2fid(dir)), inode);
797 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
799 ptlrpc_req_finished(request);
804 * By the time this is called, we already have created the directory cache
805 * entry for the new file, but it is so far negative - it has no inode.
807 * We defer creating the OBD object(s) until open, to keep the intent and
808 * non-intent code paths similar, and also because we do not have the MDS
809 * inode number before calling ll_create_node() (which is needed for LOV),
810 * so we would need to do yet another RPC to the MDS to store the LOV EA
811 * data on the MDS. If needed, we would pass the PACKED lmm as data and
812 * lmm_size in datalen (the MDS still has code which will handle that).
814 * If the create succeeds, we fill in the inode information
815 * with d_instantiate().
817 static int ll_create_it(struct inode *dir, struct dentry *dentry,
818 struct lookup_intent *it)
823 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), intent=%s\n",
824 dentry, PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
826 rc = it_open_error(DISP_OPEN_CREATE, it);
830 inode = ll_create_node(dir, it);
832 return PTR_ERR(inode);
834 d_instantiate(dentry, inode);
836 return ll_init_security(dentry, inode, dir);
839 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
841 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
845 if (body->mbo_valid & OBD_MD_FLMTIME &&
846 body->mbo_mtime > LTIME_S(inode->i_mtime)) {
847 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to %llu\n",
848 PFID(ll_inode2fid(inode)), LTIME_S(inode->i_mtime),
850 LTIME_S(inode->i_mtime) = body->mbo_mtime;
852 if (body->mbo_valid & OBD_MD_FLCTIME &&
853 body->mbo_ctime > LTIME_S(inode->i_ctime))
854 LTIME_S(inode->i_ctime) = body->mbo_ctime;
857 static int ll_new_node(struct inode *dir, struct dentry *dentry,
858 const char *tgt, umode_t mode, int rdev,
861 struct ptlrpc_request *request = NULL;
862 struct md_op_data *op_data;
863 struct inode *inode = NULL;
864 struct ll_sb_info *sbi = ll_i2sbi(dir);
869 tgt_len = strlen(tgt) + 1;
871 op_data = ll_prep_md_op_data(NULL, dir, NULL,
875 if (IS_ERR(op_data)) {
876 err = PTR_ERR(op_data);
880 err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
881 from_kuid(&init_user_ns, current_fsuid()),
882 from_kgid(&init_user_ns, current_fsgid()),
883 cfs_curproc_cap_pack(), rdev, &request);
884 ll_finish_md_op_data(op_data);
885 if (err < 0 && err != -EREMOTE)
889 * If the client doesn't know where to create a subdirectory (or
890 * in case of a race that sends the RPC to the wrong MDS), the
891 * MDS will return -EREMOTE and the client will fetch the layout
892 * of the directory, then create the directory on the right MDT.
894 if (unlikely(err == -EREMOTE)) {
895 struct ll_inode_info *lli = ll_i2info(dir);
896 struct lmv_user_md *lum;
899 ptlrpc_req_finished(request);
902 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
905 /* Update stripe_offset and retry */
906 lli->lli_def_stripe_offset = lum->lum_stripe_offset;
907 } else if (err2 == -ENODATA &&
908 lli->lli_def_stripe_offset != -1) {
910 * If there are no default stripe EA on the MDT, but the
911 * client has default stripe, then it probably means
912 * default stripe EA has just been deleted.
914 lli->lli_def_stripe_offset = -1;
919 ptlrpc_req_finished(request);
924 ll_update_times(request, dir);
926 err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
930 d_instantiate(dentry, inode);
932 err = ll_init_security(dentry, inode, dir);
935 ptlrpc_req_finished(request);
940 static int ll_mknod(struct inode *dir, struct dentry *dchild,
941 umode_t mode, dev_t rdev)
945 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p) mode %o dev %x\n",
946 dchild, PFID(ll_inode2fid(dir)), dir, mode,
947 old_encode_dev(rdev));
949 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
950 mode &= ~current_umask();
952 switch (mode & S_IFMT) {
954 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
960 err = ll_new_node(dir, dchild, NULL, mode,
961 old_encode_dev(rdev),
972 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
978 * Plain create. Intent create is handled in atomic_open.
980 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
981 umode_t mode, bool want_excl)
985 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p), flags=%u, excl=%d\n",
986 dentry, PFID(ll_inode2fid(dir)), dir, mode, want_excl);
988 rc = ll_mknod(dir, dentry, mode, 0);
990 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
992 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
993 dentry, d_unhashed(dentry));
998 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1000 struct ptlrpc_request *request = NULL;
1001 struct md_op_data *op_data;
1004 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
1005 dchild, dir->i_ino, dir->i_generation, dir);
1007 op_data = ll_prep_md_op_data(NULL, dir, NULL,
1008 dchild->d_name.name,
1010 0, LUSTRE_OPC_ANY, NULL);
1011 if (IS_ERR(op_data))
1012 return PTR_ERR(op_data);
1014 if (dchild->d_inode)
1015 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1017 op_data->op_fid2 = op_data->op_fid3;
1018 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1019 ll_finish_md_op_data(op_data);
1023 ll_update_times(request, dir);
1024 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1027 ptlrpc_req_finished(request);
1031 static int ll_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1035 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir"DFID"(%p)\n",
1036 dentry, PFID(ll_inode2fid(dir)), dir);
1038 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1039 mode &= ~current_umask();
1040 mode = (mode & (0777 | S_ISVTX)) | S_IFDIR;
1042 err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1044 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1049 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1051 struct ptlrpc_request *request = NULL;
1052 struct md_op_data *op_data;
1055 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p)\n",
1056 dchild, PFID(ll_inode2fid(dir)), dir);
1058 op_data = ll_prep_md_op_data(NULL, dir, NULL,
1059 dchild->d_name.name,
1061 S_IFDIR, LUSTRE_OPC_ANY, NULL);
1062 if (IS_ERR(op_data))
1063 return PTR_ERR(op_data);
1065 if (dchild->d_inode)
1066 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1068 op_data->op_fid2 = op_data->op_fid3;
1069 rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1070 ll_finish_md_op_data(op_data);
1072 ll_update_times(request, dir);
1073 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1076 ptlrpc_req_finished(request);
1080 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1081 const char *oldname)
1085 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, dir="DFID"(%p),target=%.*s\n",
1086 dentry, PFID(ll_inode2fid(dir)), dir, 3000, oldname);
1088 err = ll_new_node(dir, dentry, oldname, S_IFLNK | 0777,
1089 0, LUSTRE_OPC_SYMLINK);
1092 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1097 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1098 struct dentry *new_dentry)
1100 struct inode *src = d_inode(old_dentry);
1101 struct ll_sb_info *sbi = ll_i2sbi(dir);
1102 struct ptlrpc_request *request = NULL;
1103 struct md_op_data *op_data;
1106 CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), target=%pd\n",
1107 PFID(ll_inode2fid(src)), src, PFID(ll_inode2fid(dir)), dir,
1110 op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1111 new_dentry->d_name.len,
1112 0, LUSTRE_OPC_ANY, NULL);
1113 if (IS_ERR(op_data))
1114 return PTR_ERR(op_data);
1116 err = md_link(sbi->ll_md_exp, op_data, &request);
1117 ll_finish_md_op_data(op_data);
1121 ll_update_times(request, dir);
1122 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1124 ptlrpc_req_finished(request);
1128 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1129 struct inode *tgt, struct dentry *tgt_dchild,
1132 struct ptlrpc_request *request = NULL;
1133 struct ll_sb_info *sbi = ll_i2sbi(src);
1134 struct md_op_data *op_data;
1141 "VFS Op:oldname=%pd, src_dir="DFID"(%p), newname=%pd, tgt_dir="DFID"(%p)\n",
1142 src_dchild, PFID(ll_inode2fid(src)), src,
1143 tgt_dchild, PFID(ll_inode2fid(tgt)), tgt);
1145 op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1146 LUSTRE_OPC_ANY, NULL);
1147 if (IS_ERR(op_data))
1148 return PTR_ERR(op_data);
1150 if (src_dchild->d_inode)
1151 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1152 if (tgt_dchild->d_inode)
1153 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1155 err = md_rename(sbi->ll_md_exp, op_data,
1156 src_dchild->d_name.name,
1157 src_dchild->d_name.len,
1158 tgt_dchild->d_name.name,
1159 tgt_dchild->d_name.len, &request);
1160 ll_finish_md_op_data(op_data);
1162 ll_update_times(request, src);
1163 ll_update_times(request, tgt);
1164 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1167 ptlrpc_req_finished(request);
1169 d_move(src_dchild, tgt_dchild);
1173 const struct inode_operations ll_dir_inode_operations = {
1175 .atomic_open = ll_atomic_open,
1176 .lookup = ll_lookup_nd,
1177 .create = ll_create_nd,
1178 /* We need all these non-raw things for NFSD, to not patch it. */
1179 .unlink = ll_unlink,
1182 .symlink = ll_symlink,
1184 .rename = ll_rename,
1185 .setattr = ll_setattr,
1186 .getattr = ll_getattr,
1187 .permission = ll_inode_permission,
1188 .listxattr = ll_listxattr,
1189 .get_acl = ll_get_acl,
1192 const struct inode_operations ll_special_inode_operations = {
1193 .setattr = ll_setattr,
1194 .getattr = ll_getattr,
1195 .permission = ll_inode_permission,
1196 .listxattr = ll_listxattr,
1197 .get_acl = ll_get_acl,