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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
40 #include <linux/quotaops.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
43 #include <linux/security.h>
45 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_lite.h>
50 #include <lustre_dlm.h>
51 #include <lustre_ver.h>
52 #include "llite_internal.h"
54 static int ll_create_it(struct inode
*, struct dentry
*,
55 int, struct lookup_intent
*);
58 * Check if we have something mounted at the named dchild.
59 * In such a case there would always be dentry present.
61 static int ll_d_mountpoint(struct dentry
*dparent
, struct dentry
*dchild
,
66 if (unlikely(dchild
)) {
67 mounted
= d_mountpoint(dchild
);
69 dchild
= d_lookup(dparent
, name
);
71 mounted
= d_mountpoint(dchild
);
78 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
79 static int ll_test_inode(struct inode
*inode
, void *opaque
)
81 struct ll_inode_info
*lli
= ll_i2info(inode
);
82 struct lustre_md
*md
= opaque
;
84 if (unlikely(!(md
->body
->valid
& OBD_MD_FLID
))) {
85 CERROR("MDS body missing FID\n");
89 if (!lu_fid_eq(&lli
->lli_fid
, &md
->body
->fid1
))
95 static int ll_set_inode(struct inode
*inode
, void *opaque
)
97 struct ll_inode_info
*lli
= ll_i2info(inode
);
98 struct mdt_body
*body
= ((struct lustre_md
*)opaque
)->body
;
100 if (unlikely(!(body
->valid
& OBD_MD_FLID
))) {
101 CERROR("MDS body missing FID\n");
105 lli
->lli_fid
= body
->fid1
;
106 if (unlikely(!(body
->valid
& OBD_MD_FLTYPE
))) {
107 CERROR("Can not initialize inode "DFID
" without object type: "
108 "valid = "LPX64
"\n", PFID(&lli
->lli_fid
), body
->valid
);
112 inode
->i_mode
= (inode
->i_mode
& ~S_IFMT
) | (body
->mode
& S_IFMT
);
113 if (unlikely(inode
->i_mode
== 0)) {
114 CERROR("Invalid inode "DFID
" type\n", PFID(&lli
->lli_fid
));
125 * Get an inode by inode number (already instantiated by the intent lookup).
126 * Returns inode or NULL
128 struct inode
*ll_iget(struct super_block
*sb
, ino_t hash
,
129 struct lustre_md
*md
)
134 inode
= iget5_locked(sb
, hash
, ll_test_inode
, ll_set_inode
, md
);
137 if (inode
->i_state
& I_NEW
) {
140 ll_read_inode2(inode
, md
);
141 if (S_ISREG(inode
->i_mode
) &&
142 ll_i2info(inode
)->lli_clob
== NULL
) {
144 "%s: apply lsm %p to inode "DFID
".\n",
145 ll_get_fsname(sb
, NULL
, 0), md
->lsm
,
146 PFID(ll_inode2fid(inode
)));
147 rc
= cl_file_inode_init(inode
, md
);
150 make_bad_inode(inode
);
151 unlock_new_inode(inode
);
155 unlock_new_inode(inode
);
156 } else if (!(inode
->i_state
& (I_FREEING
| I_CLEAR
)))
157 ll_update_inode(inode
, md
);
158 CDEBUG(D_VFSTRACE
, "got inode: %p for "DFID
"\n",
159 inode
, PFID(&md
->body
->fid1
));
164 static void ll_invalidate_negative_children(struct inode
*dir
)
166 struct dentry
*dentry
, *tmp_subdir
;
167 struct ll_d_hlist_node
*p
;
170 ll_d_hlist_for_each_entry(dentry
, p
, &dir
->i_dentry
, d_alias
) {
171 spin_lock(&dentry
->d_lock
);
172 if (!list_empty(&dentry
->d_subdirs
)) {
173 struct dentry
*child
;
175 list_for_each_entry_safe(child
, tmp_subdir
,
178 if (child
->d_inode
== NULL
)
179 d_lustre_invalidate(child
, 1);
182 spin_unlock(&dentry
->d_lock
);
184 ll_unlock_dcache(dir
);
187 int ll_md_blocking_ast(struct ldlm_lock
*lock
, struct ldlm_lock_desc
*desc
,
188 void *data
, int flag
)
190 struct lustre_handle lockh
;
194 case LDLM_CB_BLOCKING
:
195 ldlm_lock2handle(lock
, &lockh
);
196 rc
= ldlm_cli_cancel(&lockh
, LCF_ASYNC
);
198 CDEBUG(D_INODE
, "ldlm_cli_cancel: rc = %d\n", rc
);
202 case LDLM_CB_CANCELING
: {
203 struct inode
*inode
= ll_inode_from_resource_lock(lock
);
204 __u64 bits
= lock
->l_policy_data
.l_inodebits
.bits
;
206 /* Inode is set to lock->l_resource->lr_lvb_inode
207 * for mdc - bug 24555 */
208 LASSERT(lock
->l_ast_data
== NULL
);
213 /* Invalidate all dentries associated with this inode */
214 LASSERT(lock
->l_flags
& LDLM_FL_CANCELING
);
216 if (!fid_res_name_eq(ll_inode2fid(inode
),
217 &lock
->l_resource
->lr_name
)) {
218 LDLM_ERROR(lock
, "data mismatch with object "DFID
"(%p)",
219 PFID(ll_inode2fid(inode
)), inode
);
223 if (bits
& MDS_INODELOCK_XATTR
) {
224 ll_xattr_cache_destroy(inode
);
225 bits
&= ~MDS_INODELOCK_XATTR
;
228 /* For OPEN locks we differentiate between lock modes
229 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
230 if (bits
& MDS_INODELOCK_OPEN
)
231 ll_have_md_lock(inode
, &bits
, lock
->l_req_mode
);
233 if (bits
& MDS_INODELOCK_OPEN
) {
236 switch (lock
->l_req_mode
) {
247 LDLM_ERROR(lock
, "bad lock mode for OPEN lock");
251 ll_md_real_close(inode
, fmode
);
254 if (bits
& (MDS_INODELOCK_LOOKUP
| MDS_INODELOCK_UPDATE
|
255 MDS_INODELOCK_LAYOUT
| MDS_INODELOCK_PERM
))
256 ll_have_md_lock(inode
, &bits
, LCK_MINMODE
);
258 if (bits
& MDS_INODELOCK_LAYOUT
) {
259 struct cl_object_conf conf
= {
260 .coc_opc
= OBJECT_CONF_INVALIDATE
,
264 rc
= ll_layout_conf(inode
, &conf
);
266 CDEBUG(D_INODE
, "cannot invalidate layout of "
268 PFID(ll_inode2fid(inode
)), rc
);
271 if (bits
& MDS_INODELOCK_UPDATE
) {
272 struct ll_inode_info
*lli
= ll_i2info(inode
);
274 spin_lock(&lli
->lli_lock
);
275 lli
->lli_flags
&= ~LLIF_MDS_SIZE_LOCK
;
276 spin_unlock(&lli
->lli_lock
);
279 if ((bits
& MDS_INODELOCK_UPDATE
) && S_ISDIR(inode
->i_mode
)) {
280 CDEBUG(D_INODE
, "invalidating inode %lu\n",
282 truncate_inode_pages(inode
->i_mapping
, 0);
283 ll_invalidate_negative_children(inode
);
286 if ((bits
& (MDS_INODELOCK_LOOKUP
| MDS_INODELOCK_PERM
)) &&
287 inode
->i_sb
->s_root
!= NULL
&&
288 inode
!= inode
->i_sb
->s_root
->d_inode
)
289 ll_invalidate_aliases(inode
);
301 __u32
ll_i2suppgid(struct inode
*i
)
303 if (in_group_p(i
->i_gid
))
304 return (__u32
)from_kgid(&init_user_ns
, i
->i_gid
);
309 /* Pack the required supplementary groups into the supplied groups array.
310 * If we don't need to use the groups from the target inode(s) then we
311 * instead pack one or more groups from the user's supplementary group
312 * array in case it might be useful. Not needed if doing an MDS-side upcall. */
313 void ll_i2gids(__u32
*suppgids
, struct inode
*i1
, struct inode
*i2
)
320 LASSERT(suppgids
!= NULL
);
322 suppgids
[0] = ll_i2suppgid(i1
);
325 suppgids
[1] = ll_i2suppgid(i2
);
330 for (i
= 0; i
< current_ngroups
; i
++) {
331 if (suppgids
[0] == -1) {
332 if (current_groups
[i
] != suppgids
[1])
333 suppgids
[0] = current_groups
[i
];
336 if (suppgids
[1] == -1) {
337 if (current_groups
[i
] != suppgids
[0])
338 suppgids
[1] = current_groups
[i
];
347 * try to reuse three types of dentry:
348 * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
349 * by concurrent .revalidate).
350 * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
351 * be cleared by others calling d_lustre_revalidate).
352 * 3. DISCONNECTED alias.
354 static struct dentry
*ll_find_alias(struct inode
*inode
, struct dentry
*dentry
)
356 struct dentry
*alias
, *discon_alias
, *invalid_alias
;
357 struct ll_d_hlist_node
*p
;
359 if (ll_d_hlist_empty(&inode
->i_dentry
))
362 discon_alias
= invalid_alias
= NULL
;
364 ll_lock_dcache(inode
);
365 ll_d_hlist_for_each_entry(alias
, p
, &inode
->i_dentry
, d_alias
) {
366 LASSERT(alias
!= dentry
);
368 spin_lock(&alias
->d_lock
);
369 if (alias
->d_flags
& DCACHE_DISCONNECTED
)
370 /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
371 discon_alias
= alias
;
372 else if (alias
->d_parent
== dentry
->d_parent
&&
373 alias
->d_name
.hash
== dentry
->d_name
.hash
&&
374 alias
->d_name
.len
== dentry
->d_name
.len
&&
375 memcmp(alias
->d_name
.name
, dentry
->d_name
.name
,
376 dentry
->d_name
.len
) == 0)
377 invalid_alias
= alias
;
378 spin_unlock(&alias
->d_lock
);
383 alias
= invalid_alias
?: discon_alias
?: NULL
;
385 spin_lock(&alias
->d_lock
);
387 spin_unlock(&alias
->d_lock
);
389 ll_unlock_dcache(inode
);
395 * Similar to d_splice_alias(), but lustre treats invalid alias
396 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
398 struct dentry
*ll_splice_alias(struct inode
*inode
, struct dentry
*de
)
404 new = ll_find_alias(inode
, de
);
414 "Reuse dentry %p inode %p refc %d flags %#x\n",
415 new, new->d_inode
, d_count(new), new->d_flags
);
423 CDEBUG(D_DENTRY
, "Add dentry %p inode %p refc %d flags %#x\n",
424 de
, de
->d_inode
, d_count(de
), de
->d_flags
);
428 static int ll_lookup_it_finish(struct ptlrpc_request
*request
,
429 struct lookup_intent
*it
,
430 struct inode
*parent
, struct dentry
**de
)
432 struct inode
*inode
= NULL
;
436 /* NB 1 request reference will be taken away by ll_intent_lock()
438 CDEBUG(D_DENTRY
, "it %p it_disposition %x\n", it
,
439 it
->d
.lustre
.it_disposition
);
440 if (!it_disposition(it
, DISP_LOOKUP_NEG
)) {
441 rc
= ll_prep_inode(&inode
, request
, (*de
)->d_sb
, it
);
445 ll_set_lock_data(ll_i2sbi(parent
)->ll_md_exp
, inode
, it
, &bits
);
447 /* We used to query real size from OSTs here, but actually
448 this is not needed. For stat() calls size would be updated
449 from subsequent do_revalidate()->ll_inode_revalidate_it() in
451 vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
452 Everybody else who needs correct file size would call
453 ll_glimpse_size or some equivalent themselves anyway.
454 Also see bug 7198. */
457 /* Only hash *de if it is unhashed (new dentry).
458 * Atoimc_open may passing hashed dentries for open.
460 if (d_unhashed(*de
)) {
461 struct dentry
*alias
;
463 alias
= ll_splice_alias(inode
, *de
);
465 return PTR_ERR(alias
);
467 } else if (!it_disposition(it
, DISP_LOOKUP_NEG
) &&
468 !it_disposition(it
, DISP_OPEN_CREATE
)) {
469 /* With DISP_OPEN_CREATE dentry will
470 instantiated in ll_create_it. */
471 LASSERT((*de
)->d_inode
== NULL
);
472 d_instantiate(*de
, inode
);
475 if (!it_disposition(it
, DISP_LOOKUP_NEG
)) {
476 /* we have lookup look - unhide dentry */
477 if (bits
& MDS_INODELOCK_LOOKUP
)
478 d_lustre_revalidate(*de
);
479 } else if (!it_disposition(it
, DISP_OPEN_CREATE
)) {
480 /* If file created on server, don't depend on parent UPDATE
481 * lock to unhide it. It is left hidden and next lookup can
482 * find it in ll_splice_alias.
484 /* Check that parent has UPDATE lock. */
485 struct lookup_intent parent_it
= {
487 .d
.lustre
.it_lock_handle
= 0 };
489 if (md_revalidate_lock(ll_i2mdexp(parent
), &parent_it
,
490 &ll_i2info(parent
)->lli_fid
, NULL
)) {
491 d_lustre_revalidate(*de
);
492 ll_intent_release(&parent_it
);
499 static struct dentry
*ll_lookup_it(struct inode
*parent
, struct dentry
*dentry
,
500 struct lookup_intent
*it
, int lookup_flags
)
502 struct lookup_intent lookup_it
= { .it_op
= IT_LOOKUP
};
503 struct dentry
*save
= dentry
, *retval
;
504 struct ptlrpc_request
*req
= NULL
;
505 struct md_op_data
*op_data
;
509 if (dentry
->d_name
.len
> ll_i2sbi(parent
)->ll_namelen
)
510 return ERR_PTR(-ENAMETOOLONG
);
512 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
513 dentry
->d_name
.len
, dentry
->d_name
.name
, parent
->i_ino
,
514 parent
->i_generation
, parent
, LL_IT2STR(it
));
516 if (d_mountpoint(dentry
))
517 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it
));
519 if (it
== NULL
|| it
->it_op
== IT_GETXATTR
)
522 if (it
->it_op
== IT_GETATTR
) {
523 rc
= ll_statahead_enter(parent
, &dentry
, 0);
526 GOTO(out
, retval
= NULL
);
527 GOTO(out
, retval
= dentry
);
531 if (it
->it_op
& IT_CREAT
)
532 opc
= LUSTRE_OPC_CREATE
;
534 opc
= LUSTRE_OPC_ANY
;
536 op_data
= ll_prep_md_op_data(NULL
, parent
, NULL
, dentry
->d_name
.name
,
537 dentry
->d_name
.len
, lookup_flags
, opc
,
540 return (void *)op_data
;
542 /* enforce umask if acl disabled or MDS doesn't support umask */
543 if (!IS_POSIXACL(parent
) || !exp_connect_umask(ll_i2mdexp(parent
)))
544 it
->it_create_mode
&= ~current_umask();
546 rc
= md_intent_lock(ll_i2mdexp(parent
), op_data
, NULL
, 0, it
,
547 lookup_flags
, &req
, ll_md_blocking_ast
, 0);
548 ll_finish_md_op_data(op_data
);
550 GOTO(out
, retval
= ERR_PTR(rc
));
552 rc
= ll_lookup_it_finish(req
, it
, parent
, &dentry
);
554 ll_intent_release(it
);
555 GOTO(out
, retval
= ERR_PTR(rc
));
558 if ((it
->it_op
& IT_OPEN
) && dentry
->d_inode
&&
559 !S_ISREG(dentry
->d_inode
->i_mode
) &&
560 !S_ISDIR(dentry
->d_inode
->i_mode
)) {
561 ll_release_openhandle(dentry
, it
);
563 ll_lookup_finish_locks(it
, dentry
);
566 GOTO(out
, retval
= NULL
);
568 GOTO(out
, retval
= dentry
);
571 ptlrpc_req_finished(req
);
572 if (it
->it_op
== IT_GETATTR
&& (retval
== NULL
|| retval
== dentry
))
573 ll_statahead_mark(parent
, dentry
);
577 static struct dentry
*ll_lookup_nd(struct inode
*parent
, struct dentry
*dentry
,
580 struct lookup_intent
*itp
, it
= { .it_op
= IT_GETATTR
};
583 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p),flags=%u\n",
584 dentry
->d_name
.len
, dentry
->d_name
.name
, parent
->i_ino
,
585 parent
->i_generation
, parent
, flags
);
587 /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
588 if ((flags
& LOOKUP_CREATE
) && !(flags
& LOOKUP_OPEN
))
591 if (flags
& (LOOKUP_PARENT
|LOOKUP_OPEN
|LOOKUP_CREATE
))
595 de
= ll_lookup_it(parent
, dentry
, itp
, 0);
598 ll_intent_release(itp
);
604 * For cached negative dentry and new dentry, handle lookup/create/open
607 static int ll_atomic_open(struct inode
*dir
, struct dentry
*dentry
,
608 struct file
*file
, unsigned open_flags
,
609 umode_t mode
, int *opened
)
611 struct lookup_intent
*it
;
613 long long lookup_flags
= LOOKUP_OPEN
;
616 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p),file %p,"
617 "open_flags %x,mode %x opened %d\n",
618 dentry
->d_name
.len
, dentry
->d_name
.name
, dir
->i_ino
,
619 dir
->i_generation
, dir
, file
, open_flags
, mode
, *opened
);
621 OBD_ALLOC(it
, sizeof(*it
));
626 if (open_flags
& O_CREAT
) {
627 it
->it_op
|= IT_CREAT
;
628 lookup_flags
|= LOOKUP_CREATE
;
630 it
->it_create_mode
= (mode
& S_IALLUGO
) | S_IFREG
;
631 it
->it_flags
= (open_flags
& ~O_ACCMODE
) | OPEN_FMODE(open_flags
);
633 /* Dentry added to dcache tree in ll_lookup_it */
634 de
= ll_lookup_it(dir
, dentry
, it
, lookup_flags
);
641 if (it_disposition(it
, DISP_OPEN_CREATE
)) {
642 /* Dentry instantiated in ll_create_it. */
643 rc
= ll_create_it(dir
, dentry
, mode
, it
);
645 /* We dget in ll_splice_alias. */
651 *opened
|= FILE_CREATED
;
653 if (dentry
->d_inode
&& it_disposition(it
, DISP_OPEN_OPEN
)) {
655 if (S_ISFIFO(dentry
->d_inode
->i_mode
)) {
656 /* We cannot call open here as it would
659 if (it_disposition(it
, DISP_ENQ_OPEN_REF
))
661 (struct ptlrpc_request
*)
662 it
->d
.lustre
.it_data
);
663 rc
= finish_no_open(file
, de
);
665 file
->private_data
= it
;
666 rc
= finish_open(file
, dentry
, NULL
, opened
);
667 /* We dget in ll_splice_alias. finish_open takes
668 * care of dget for fd open.
674 rc
= finish_no_open(file
, de
);
679 ll_intent_release(it
);
680 OBD_FREE(it
, sizeof(*it
));
686 /* We depend on "mode" being set with the proper file type/umask by now */
687 static struct inode
*ll_create_node(struct inode
*dir
, struct lookup_intent
*it
)
689 struct inode
*inode
= NULL
;
690 struct ptlrpc_request
*request
= NULL
;
691 struct ll_sb_info
*sbi
= ll_i2sbi(dir
);
694 LASSERT(it
&& it
->d
.lustre
.it_disposition
);
696 LASSERT(it_disposition(it
, DISP_ENQ_CREATE_REF
));
697 request
= it
->d
.lustre
.it_data
;
698 it_clear_disposition(it
, DISP_ENQ_CREATE_REF
);
699 rc
= ll_prep_inode(&inode
, request
, dir
->i_sb
, it
);
701 GOTO(out
, inode
= ERR_PTR(rc
));
703 LASSERT(ll_d_hlist_empty(&inode
->i_dentry
));
705 /* We asked for a lock on the directory, but were granted a
706 * lock on the inode. Since we finally have an inode pointer,
707 * stuff it in the lock. */
708 CDEBUG(D_DLMTRACE
, "setting l_ast_data to inode %p (%lu/%u)\n",
709 inode
, inode
->i_ino
, inode
->i_generation
);
710 ll_set_lock_data(sbi
->ll_md_exp
, inode
, it
, NULL
);
712 ptlrpc_req_finished(request
);
717 * By the time this is called, we already have created the directory cache
718 * entry for the new file, but it is so far negative - it has no inode.
720 * We defer creating the OBD object(s) until open, to keep the intent and
721 * non-intent code paths similar, and also because we do not have the MDS
722 * inode number before calling ll_create_node() (which is needed for LOV),
723 * so we would need to do yet another RPC to the MDS to store the LOV EA
724 * data on the MDS. If needed, we would pass the PACKED lmm as data and
725 * lmm_size in datalen (the MDS still has code which will handle that).
727 * If the create succeeds, we fill in the inode information
728 * with d_instantiate().
730 static int ll_create_it(struct inode
*dir
, struct dentry
*dentry
, int mode
,
731 struct lookup_intent
*it
)
736 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
737 dentry
->d_name
.len
, dentry
->d_name
.name
, dir
->i_ino
,
738 dir
->i_generation
, dir
, LL_IT2STR(it
));
740 rc
= it_open_error(DISP_OPEN_CREATE
, it
);
744 inode
= ll_create_node(dir
, it
);
746 return PTR_ERR(inode
);
748 d_instantiate(dentry
, inode
);
752 static void ll_update_times(struct ptlrpc_request
*request
,
755 struct mdt_body
*body
= req_capsule_server_get(&request
->rq_pill
,
759 if (body
->valid
& OBD_MD_FLMTIME
&&
760 body
->mtime
> LTIME_S(inode
->i_mtime
)) {
761 CDEBUG(D_INODE
, "setting ino %lu mtime from %lu to "LPU64
"\n",
762 inode
->i_ino
, LTIME_S(inode
->i_mtime
), body
->mtime
);
763 LTIME_S(inode
->i_mtime
) = body
->mtime
;
765 if (body
->valid
& OBD_MD_FLCTIME
&&
766 body
->ctime
> LTIME_S(inode
->i_ctime
))
767 LTIME_S(inode
->i_ctime
) = body
->ctime
;
770 static int ll_new_node(struct inode
*dir
, struct qstr
*name
,
771 const char *tgt
, int mode
, int rdev
,
772 struct dentry
*dchild
, __u32 opc
)
774 struct ptlrpc_request
*request
= NULL
;
775 struct md_op_data
*op_data
;
776 struct inode
*inode
= NULL
;
777 struct ll_sb_info
*sbi
= ll_i2sbi(dir
);
781 if (unlikely(tgt
!= NULL
))
782 tgt_len
= strlen(tgt
) + 1;
784 op_data
= ll_prep_md_op_data(NULL
, dir
, NULL
, name
->name
,
785 name
->len
, 0, opc
, NULL
);
787 GOTO(err_exit
, err
= PTR_ERR(op_data
));
789 err
= md_create(sbi
->ll_md_exp
, op_data
, tgt
, tgt_len
, mode
,
790 from_kuid(&init_user_ns
, current_fsuid()),
791 from_kgid(&init_user_ns
, current_fsgid()),
792 cfs_curproc_cap_pack(), rdev
, &request
);
793 ll_finish_md_op_data(op_data
);
797 ll_update_times(request
, dir
);
800 err
= ll_prep_inode(&inode
, request
, dchild
->d_sb
, NULL
);
804 d_instantiate(dchild
, inode
);
807 ptlrpc_req_finished(request
);
812 static int ll_mknod_generic(struct inode
*dir
, struct qstr
*name
, int mode
,
813 unsigned rdev
, struct dentry
*dchild
)
817 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
818 name
->len
, name
->name
, dir
->i_ino
, dir
->i_generation
, dir
,
821 if (!IS_POSIXACL(dir
) || !exp_connect_umask(ll_i2mdexp(dir
)))
822 mode
&= ~current_umask();
824 switch (mode
& S_IFMT
) {
826 mode
|= S_IFREG
; /* for mode = 0 case, fallthrough */
832 err
= ll_new_node(dir
, name
, NULL
, mode
, rdev
, dchild
,
843 ll_stats_ops_tally(ll_i2sbi(dir
), LPROC_LL_MKNOD
, 1);
849 * Plain create. Intent create is handled in atomic_open.
851 static int ll_create_nd(struct inode
*dir
, struct dentry
*dentry
,
852 umode_t mode
, bool want_excl
)
856 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p),"
857 "flags=%u, excl=%d\n",
858 dentry
->d_name
.len
, dentry
->d_name
.name
, dir
->i_ino
,
859 dir
->i_generation
, dir
, mode
, want_excl
);
861 rc
= ll_mknod_generic(dir
, &dentry
->d_name
, mode
, 0, dentry
);
863 ll_stats_ops_tally(ll_i2sbi(dir
), LPROC_LL_CREATE
, 1);
865 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s, unhashed %d\n",
866 dentry
->d_name
.len
, dentry
->d_name
.name
, d_unhashed(dentry
));
871 static int ll_symlink_generic(struct inode
*dir
, struct qstr
*name
,
872 const char *tgt
, struct dentry
*dchild
)
876 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
877 name
->len
, name
->name
, dir
->i_ino
, dir
->i_generation
,
880 err
= ll_new_node(dir
, name
, (char *)tgt
, S_IFLNK
| S_IRWXUGO
,
881 0, dchild
, LUSTRE_OPC_SYMLINK
);
884 ll_stats_ops_tally(ll_i2sbi(dir
), LPROC_LL_SYMLINK
, 1);
889 static int ll_link_generic(struct inode
*src
, struct inode
*dir
,
890 struct qstr
*name
, struct dentry
*dchild
)
892 struct ll_sb_info
*sbi
= ll_i2sbi(dir
);
893 struct ptlrpc_request
*request
= NULL
;
894 struct md_op_data
*op_data
;
898 "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
899 src
->i_ino
, src
->i_generation
, src
, dir
->i_ino
,
900 dir
->i_generation
, dir
, name
->len
, name
->name
);
902 op_data
= ll_prep_md_op_data(NULL
, src
, dir
, name
->name
, name
->len
,
903 0, LUSTRE_OPC_ANY
, NULL
);
905 return PTR_ERR(op_data
);
907 err
= md_link(sbi
->ll_md_exp
, op_data
, &request
);
908 ll_finish_md_op_data(op_data
);
912 ll_update_times(request
, dir
);
913 ll_stats_ops_tally(sbi
, LPROC_LL_LINK
, 1);
915 ptlrpc_req_finished(request
);
919 static int ll_mkdir_generic(struct inode
*dir
, struct qstr
*name
,
920 int mode
, struct dentry
*dchild
)
925 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
926 name
->len
, name
->name
, dir
->i_ino
, dir
->i_generation
, dir
);
928 if (!IS_POSIXACL(dir
) || !exp_connect_umask(ll_i2mdexp(dir
)))
929 mode
&= ~current_umask();
930 mode
= (mode
& (S_IRWXUGO
|S_ISVTX
)) | S_IFDIR
;
931 err
= ll_new_node(dir
, name
, NULL
, mode
, 0, dchild
, LUSTRE_OPC_MKDIR
);
934 ll_stats_ops_tally(ll_i2sbi(dir
), LPROC_LL_MKDIR
, 1);
939 /* Try to find the child dentry by its name.
940 If found, put the result fid into @fid. */
941 static void ll_get_child_fid(struct inode
* dir
, struct qstr
*name
,
944 struct dentry
*parent
, *child
;
946 parent
= ll_d_hlist_entry(dir
->i_dentry
, struct dentry
, d_alias
);
947 child
= d_lookup(parent
, name
);
950 *fid
= *ll_inode2fid(child
->d_inode
);
955 static int ll_rmdir_generic(struct inode
*dir
, struct dentry
*dparent
,
956 struct dentry
*dchild
, struct qstr
*name
)
958 struct ptlrpc_request
*request
= NULL
;
959 struct md_op_data
*op_data
;
962 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
963 name
->len
, name
->name
, dir
->i_ino
, dir
->i_generation
, dir
);
965 if (unlikely(ll_d_mountpoint(dparent
, dchild
, name
)))
968 op_data
= ll_prep_md_op_data(NULL
, dir
, NULL
, name
->name
, name
->len
,
969 S_IFDIR
, LUSTRE_OPC_ANY
, NULL
);
971 return PTR_ERR(op_data
);
973 ll_get_child_fid(dir
, name
, &op_data
->op_fid3
);
974 op_data
->op_fid2
= op_data
->op_fid3
;
975 rc
= md_unlink(ll_i2sbi(dir
)->ll_md_exp
, op_data
, &request
);
976 ll_finish_md_op_data(op_data
);
978 ll_update_times(request
, dir
);
979 ll_stats_ops_tally(ll_i2sbi(dir
), LPROC_LL_RMDIR
, 1);
982 ptlrpc_req_finished(request
);
989 int ll_rmdir_entry(struct inode
*dir
, char *name
, int namelen
)
991 struct ptlrpc_request
*request
= NULL
;
992 struct md_op_data
*op_data
;
995 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
996 namelen
, name
, dir
->i_ino
, dir
->i_generation
, dir
);
998 op_data
= ll_prep_md_op_data(NULL
, dir
, NULL
, name
, strlen(name
),
999 S_IFDIR
, LUSTRE_OPC_ANY
, NULL
);
1000 if (IS_ERR(op_data
))
1001 return PTR_ERR(op_data
);
1002 op_data
->op_cli_flags
|= CLI_RM_ENTRY
;
1003 rc
= md_unlink(ll_i2sbi(dir
)->ll_md_exp
, op_data
, &request
);
1004 ll_finish_md_op_data(op_data
);
1006 ll_update_times(request
, dir
);
1007 ll_stats_ops_tally(ll_i2sbi(dir
), LPROC_LL_RMDIR
, 1);
1010 ptlrpc_req_finished(request
);
1014 int ll_objects_destroy(struct ptlrpc_request
*request
, struct inode
*dir
)
1016 struct mdt_body
*body
;
1017 struct lov_mds_md
*eadata
;
1018 struct lov_stripe_md
*lsm
= NULL
;
1019 struct obd_trans_info oti
= { 0 };
1021 struct obd_capa
*oc
= NULL
;
1024 /* req is swabbed so this is safe */
1025 body
= req_capsule_server_get(&request
->rq_pill
, &RMF_MDT_BODY
);
1026 if (!(body
->valid
& OBD_MD_FLEASIZE
))
1029 if (body
->eadatasize
== 0) {
1030 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1031 GOTO(out
, rc
= -EPROTO
);
1034 /* The MDS sent back the EA because we unlinked the last reference
1035 * to this file. Use this EA to unlink the objects on the OST.
1036 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
1037 * check it is complete and sensible. */
1038 eadata
= req_capsule_server_sized_get(&request
->rq_pill
, &RMF_MDT_MD
,
1040 LASSERT(eadata
!= NULL
);
1042 rc
= obd_unpackmd(ll_i2dtexp(dir
), &lsm
, eadata
, body
->eadatasize
);
1044 CERROR("obd_unpackmd: %d\n", rc
);
1047 LASSERT(rc
>= sizeof(*lsm
));
1051 GOTO(out_free_memmd
, rc
= -ENOMEM
);
1053 oa
->o_oi
= lsm
->lsm_oi
;
1054 oa
->o_mode
= body
->mode
& S_IFMT
;
1055 oa
->o_valid
= OBD_MD_FLID
| OBD_MD_FLTYPE
| OBD_MD_FLGROUP
;
1057 if (body
->valid
& OBD_MD_FLCOOKIE
) {
1058 oa
->o_valid
|= OBD_MD_FLCOOKIE
;
1059 oti
.oti_logcookies
=
1060 req_capsule_server_sized_get(&request
->rq_pill
,
1062 sizeof(struct llog_cookie
) *
1063 lsm
->lsm_stripe_count
);
1064 if (oti
.oti_logcookies
== NULL
) {
1065 oa
->o_valid
&= ~OBD_MD_FLCOOKIE
;
1066 body
->valid
&= ~OBD_MD_FLCOOKIE
;
1070 if (body
->valid
& OBD_MD_FLOSSCAPA
) {
1071 rc
= md_unpack_capa(ll_i2mdexp(dir
), request
, &RMF_CAPA2
, &oc
);
1073 GOTO(out_free_memmd
, rc
);
1076 rc
= obd_destroy(NULL
, ll_i2dtexp(dir
), oa
, lsm
, &oti
,
1077 ll_i2mdexp(dir
), oc
);
1080 CERROR("obd destroy objid "DOSTID
" error %d\n",
1081 POSTID(&lsm
->lsm_oi
), rc
);
1083 obd_free_memmd(ll_i2dtexp(dir
), &lsm
);
1089 /* ll_unlink_generic() doesn't update the inode with the new link count.
1090 * Instead, ll_ddelete() and ll_d_iput() will update it based upon if there
1091 * is any lock existing. They will recycle dentries and inodes based upon locks
1093 static int ll_unlink_generic(struct inode
*dir
, struct dentry
*dparent
,
1094 struct dentry
*dchild
, struct qstr
*name
)
1096 struct ptlrpc_request
*request
= NULL
;
1097 struct md_op_data
*op_data
;
1099 CDEBUG(D_VFSTRACE
, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1100 name
->len
, name
->name
, dir
->i_ino
, dir
->i_generation
, dir
);
1103 * XXX: unlink bind mountpoint maybe call to here,
1104 * just check it as vfs_unlink does.
1106 if (unlikely(ll_d_mountpoint(dparent
, dchild
, name
)))
1109 op_data
= ll_prep_md_op_data(NULL
, dir
, NULL
, name
->name
,
1110 name
->len
, 0, LUSTRE_OPC_ANY
, NULL
);
1111 if (IS_ERR(op_data
))
1112 return PTR_ERR(op_data
);
1114 ll_get_child_fid(dir
, name
, &op_data
->op_fid3
);
1115 op_data
->op_fid2
= op_data
->op_fid3
;
1116 rc
= md_unlink(ll_i2sbi(dir
)->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(ll_i2sbi(dir
), LPROC_LL_UNLINK
, 1);
1124 rc
= ll_objects_destroy(request
, dir
);
1126 ptlrpc_req_finished(request
);
1130 static int ll_rename_generic(struct inode
*src
, struct dentry
*src_dparent
,
1131 struct dentry
*src_dchild
, struct qstr
*src_name
,
1132 struct inode
*tgt
, struct dentry
*tgt_dparent
,
1133 struct dentry
*tgt_dchild
, struct qstr
*tgt_name
)
1135 struct ptlrpc_request
*request
= NULL
;
1136 struct ll_sb_info
*sbi
= ll_i2sbi(src
);
1137 struct md_op_data
*op_data
;
1140 CDEBUG(D_VFSTRACE
,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1141 "tgt_dir=%lu/%u(%p)\n", src_name
->len
, src_name
->name
,
1142 src
->i_ino
, src
->i_generation
, src
, tgt_name
->len
,
1143 tgt_name
->name
, tgt
->i_ino
, tgt
->i_generation
, tgt
);
1145 if (unlikely(ll_d_mountpoint(src_dparent
, src_dchild
, src_name
) ||
1146 ll_d_mountpoint(tgt_dparent
, tgt_dchild
, tgt_name
)))
1149 op_data
= ll_prep_md_op_data(NULL
, src
, tgt
, NULL
, 0, 0,
1150 LUSTRE_OPC_ANY
, NULL
);
1151 if (IS_ERR(op_data
))
1152 return PTR_ERR(op_data
);
1154 ll_get_child_fid(src
, src_name
, &op_data
->op_fid3
);
1155 ll_get_child_fid(tgt
, tgt_name
, &op_data
->op_fid4
);
1156 err
= md_rename(sbi
->ll_md_exp
, op_data
,
1157 src_name
->name
, src_name
->len
,
1158 tgt_name
->name
, tgt_name
->len
, &request
);
1159 ll_finish_md_op_data(op_data
);
1161 ll_update_times(request
, src
);
1162 ll_update_times(request
, tgt
);
1163 ll_stats_ops_tally(sbi
, LPROC_LL_RENAME
, 1);
1164 err
= ll_objects_destroy(request
, src
);
1167 ptlrpc_req_finished(request
);
1172 static int ll_mknod(struct inode
*dir
, struct dentry
*dchild
, ll_umode_t mode
,
1175 return ll_mknod_generic(dir
, &dchild
->d_name
, mode
,
1176 old_encode_dev(rdev
), dchild
);
1179 static int ll_unlink(struct inode
* dir
, struct dentry
*dentry
)
1181 return ll_unlink_generic(dir
, NULL
, dentry
, &dentry
->d_name
);
1184 static int ll_mkdir(struct inode
*dir
, struct dentry
*dentry
, ll_umode_t mode
)
1186 return ll_mkdir_generic(dir
, &dentry
->d_name
, mode
, dentry
);
1189 static int ll_rmdir(struct inode
*dir
, struct dentry
*dentry
)
1191 return ll_rmdir_generic(dir
, NULL
, dentry
, &dentry
->d_name
);
1194 static int ll_symlink(struct inode
*dir
, struct dentry
*dentry
,
1195 const char *oldname
)
1197 return ll_symlink_generic(dir
, &dentry
->d_name
, oldname
, dentry
);
1200 static int ll_link(struct dentry
*old_dentry
, struct inode
*dir
,
1201 struct dentry
*new_dentry
)
1203 return ll_link_generic(old_dentry
->d_inode
, dir
, &new_dentry
->d_name
,
1207 static int ll_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
1208 struct inode
*new_dir
, struct dentry
*new_dentry
)
1211 err
= ll_rename_generic(old_dir
, NULL
,
1212 old_dentry
, &old_dentry
->d_name
,
1213 new_dir
, NULL
, new_dentry
,
1214 &new_dentry
->d_name
);
1216 d_move(old_dentry
, new_dentry
);
1221 const struct inode_operations ll_dir_inode_operations
= {
1223 .atomic_open
= ll_atomic_open
,
1224 .lookup
= ll_lookup_nd
,
1225 .create
= ll_create_nd
,
1226 /* We need all these non-raw things for NFSD, to not patch it. */
1227 .unlink
= ll_unlink
,
1230 .symlink
= ll_symlink
,
1232 .rename
= ll_rename
,
1233 .setattr
= ll_setattr
,
1234 .getattr
= ll_getattr
,
1235 .permission
= ll_inode_permission
,
1236 .setxattr
= ll_setxattr
,
1237 .getxattr
= ll_getxattr
,
1238 .listxattr
= ll_listxattr
,
1239 .removexattr
= ll_removexattr
,
1240 .get_acl
= ll_get_acl
,
1243 const struct inode_operations ll_special_inode_operations
= {
1244 .setattr
= ll_setattr
,
1245 .getattr
= ll_getattr
,
1246 .permission
= ll_inode_permission
,
1247 .setxattr
= ll_setxattr
,
1248 .getxattr
= ll_getxattr
,
1249 .listxattr
= ll_listxattr
,
1250 .removexattr
= ll_removexattr
,
1251 .get_acl
= ll_get_acl
,