Merge branch 'for-3.16' of git://linux-nfs.org/~bfields/linux
[deliverable/linux.git] / drivers / staging / lustre / lustre / llite / namei.c
1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2011, 2012, Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 */
36
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/quotaops.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
43 #include <linux/security.h>
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
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"
53
54 static int ll_create_it(struct inode *, struct dentry *,
55 int, struct lookup_intent *);
56
57 /*
58 * Check if we have something mounted at the named dchild.
59 * In such a case there would always be dentry present.
60 */
61 static int ll_d_mountpoint(struct dentry *dparent, struct dentry *dchild,
62 struct qstr *name)
63 {
64 int mounted = 0;
65
66 if (unlikely(dchild)) {
67 mounted = d_mountpoint(dchild);
68 } else if (dparent) {
69 dchild = d_lookup(dparent, name);
70 if (dchild) {
71 mounted = d_mountpoint(dchild);
72 dput(dchild);
73 }
74 }
75 return mounted;
76 }
77
78 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
79 static int ll_test_inode(struct inode *inode, void *opaque)
80 {
81 struct ll_inode_info *lli = ll_i2info(inode);
82 struct lustre_md *md = opaque;
83
84 if (unlikely(!(md->body->valid & OBD_MD_FLID))) {
85 CERROR("MDS body missing FID\n");
86 return 0;
87 }
88
89 if (!lu_fid_eq(&lli->lli_fid, &md->body->fid1))
90 return 0;
91
92 return 1;
93 }
94
95 static int ll_set_inode(struct inode *inode, void *opaque)
96 {
97 struct ll_inode_info *lli = ll_i2info(inode);
98 struct mdt_body *body = ((struct lustre_md *)opaque)->body;
99
100 if (unlikely(!(body->valid & OBD_MD_FLID))) {
101 CERROR("MDS body missing FID\n");
102 return -EINVAL;
103 }
104
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);
109 return -EINVAL;
110 }
111
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));
115 return -EINVAL;
116 }
117
118 ll_lli_init(lli);
119
120 return 0;
121 }
122
123
124 /*
125 * Get an inode by inode number (already instantiated by the intent lookup).
126 * Returns inode or NULL
127 */
128 struct inode *ll_iget(struct super_block *sb, ino_t hash,
129 struct lustre_md *md)
130 {
131 struct inode *inode;
132
133 LASSERT(hash != 0);
134 inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
135
136 if (inode) {
137 if (inode->i_state & I_NEW) {
138 int rc = 0;
139
140 ll_read_inode2(inode, md);
141 if (S_ISREG(inode->i_mode) &&
142 ll_i2info(inode)->lli_clob == NULL) {
143 CDEBUG(D_INODE,
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);
148 }
149 if (rc != 0) {
150 make_bad_inode(inode);
151 unlock_new_inode(inode);
152 iput(inode);
153 inode = ERR_PTR(rc);
154 } else
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));
160 }
161 return inode;
162 }
163
164 static void ll_invalidate_negative_children(struct inode *dir)
165 {
166 struct dentry *dentry, *tmp_subdir;
167 struct ll_d_hlist_node *p;
168
169 ll_lock_dcache(dir);
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;
174
175 list_for_each_entry_safe(child, tmp_subdir,
176 &dentry->d_subdirs,
177 d_u.d_child) {
178 if (child->d_inode == NULL)
179 d_lustre_invalidate(child, 1);
180 }
181 }
182 spin_unlock(&dentry->d_lock);
183 }
184 ll_unlock_dcache(dir);
185 }
186
187 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
188 void *data, int flag)
189 {
190 struct lustre_handle lockh;
191 int rc;
192
193 switch (flag) {
194 case LDLM_CB_BLOCKING:
195 ldlm_lock2handle(lock, &lockh);
196 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
197 if (rc < 0) {
198 CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
199 return rc;
200 }
201 break;
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;
205
206 /* Inode is set to lock->l_resource->lr_lvb_inode
207 * for mdc - bug 24555 */
208 LASSERT(lock->l_ast_data == NULL);
209
210 if (inode == NULL)
211 break;
212
213 /* Invalidate all dentries associated with this inode */
214 LASSERT(lock->l_flags & LDLM_FL_CANCELING);
215
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);
220 LBUG();
221 }
222
223 if (bits & MDS_INODELOCK_XATTR) {
224 ll_xattr_cache_destroy(inode);
225 bits &= ~MDS_INODELOCK_XATTR;
226 }
227
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);
232
233 if (bits & MDS_INODELOCK_OPEN) {
234 fmode_t fmode;
235
236 switch (lock->l_req_mode) {
237 case LCK_CW:
238 fmode = FMODE_WRITE;
239 break;
240 case LCK_PR:
241 fmode = FMODE_EXEC;
242 break;
243 case LCK_CR:
244 fmode = FMODE_READ;
245 break;
246 default:
247 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
248 LBUG();
249 }
250
251 ll_md_real_close(inode, fmode);
252 }
253
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);
257
258 if (bits & MDS_INODELOCK_LAYOUT) {
259 struct cl_object_conf conf = {
260 .coc_opc = OBJECT_CONF_INVALIDATE,
261 .coc_inode = inode,
262 };
263
264 rc = ll_layout_conf(inode, &conf);
265 if (rc < 0)
266 CDEBUG(D_INODE, "cannot invalidate layout of "
267 DFID": rc = %d\n",
268 PFID(ll_inode2fid(inode)), rc);
269 }
270
271 if (bits & MDS_INODELOCK_UPDATE) {
272 struct ll_inode_info *lli = ll_i2info(inode);
273
274 spin_lock(&lli->lli_lock);
275 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
276 spin_unlock(&lli->lli_lock);
277 }
278
279 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
280 CDEBUG(D_INODE, "invalidating inode %lu\n",
281 inode->i_ino);
282 truncate_inode_pages(inode->i_mapping, 0);
283 ll_invalidate_negative_children(inode);
284 }
285
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);
290
291 iput(inode);
292 break;
293 }
294 default:
295 LBUG();
296 }
297
298 return 0;
299 }
300
301 __u32 ll_i2suppgid(struct inode *i)
302 {
303 if (in_group_p(i->i_gid))
304 return (__u32)from_kgid(&init_user_ns, i->i_gid);
305 else
306 return (__u32)(-1);
307 }
308
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)
314 {
315 #if 0
316 int i;
317 #endif
318
319 LASSERT(i1 != NULL);
320 LASSERT(suppgids != NULL);
321
322 suppgids[0] = ll_i2suppgid(i1);
323
324 if (i2)
325 suppgids[1] = ll_i2suppgid(i2);
326 else
327 suppgids[1] = -1;
328
329 #if 0
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];
334 continue;
335 }
336 if (suppgids[1] == -1) {
337 if (current_groups[i] != suppgids[0])
338 suppgids[1] = current_groups[i];
339 continue;
340 }
341 break;
342 }
343 #endif
344 }
345
346 /*
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.
353 */
354 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
355 {
356 struct dentry *alias, *discon_alias, *invalid_alias;
357 struct ll_d_hlist_node *p;
358
359 if (ll_d_hlist_empty(&inode->i_dentry))
360 return NULL;
361
362 discon_alias = invalid_alias = NULL;
363
364 ll_lock_dcache(inode);
365 ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry, d_alias) {
366 LASSERT(alias != dentry);
367
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);
379
380 if (invalid_alias)
381 break;
382 }
383 alias = invalid_alias ?: discon_alias ?: NULL;
384 if (alias) {
385 spin_lock(&alias->d_lock);
386 dget_dlock(alias);
387 spin_unlock(&alias->d_lock);
388 }
389 ll_unlock_dcache(inode);
390
391 return alias;
392 }
393
394 /*
395 * Similar to d_splice_alias(), but lustre treats invalid alias
396 * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
397 */
398 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
399 {
400 struct dentry *new;
401 int rc;
402
403 if (inode) {
404 new = ll_find_alias(inode, de);
405 if (new) {
406 rc = ll_d_init(new);
407 if (rc < 0) {
408 dput(new);
409 return ERR_PTR(rc);
410 }
411 d_move(new, de);
412 iput(inode);
413 CDEBUG(D_DENTRY,
414 "Reuse dentry %p inode %p refc %d flags %#x\n",
415 new, new->d_inode, d_count(new), new->d_flags);
416 return new;
417 }
418 }
419 rc = ll_d_init(de);
420 if (rc < 0)
421 return ERR_PTR(rc);
422 d_add(de, inode);
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);
425 return de;
426 }
427
428 static int ll_lookup_it_finish(struct ptlrpc_request *request,
429 struct lookup_intent *it,
430 struct inode *parent, struct dentry **de)
431 {
432 struct inode *inode = NULL;
433 __u64 bits = 0;
434 int rc;
435
436 /* NB 1 request reference will be taken away by ll_intent_lock()
437 * when I return */
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);
442 if (rc)
443 return rc;
444
445 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
446
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
450 2.4 and
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. */
455 }
456
457 /* Only hash *de if it is unhashed (new dentry).
458 * Atoimc_open may passing hashed dentries for open.
459 */
460 if (d_unhashed(*de)) {
461 struct dentry *alias;
462
463 alias = ll_splice_alias(inode, *de);
464 if (IS_ERR(alias))
465 return PTR_ERR(alias);
466 *de = 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);
473 }
474
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.
483 */
484 /* Check that parent has UPDATE lock. */
485 struct lookup_intent parent_it = {
486 .it_op = IT_GETATTR,
487 .d.lustre.it_lock_handle = 0 };
488
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);
493 }
494 }
495
496 return 0;
497 }
498
499 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
500 struct lookup_intent *it, int lookup_flags)
501 {
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;
506 __u32 opc;
507 int rc;
508
509 if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
510 return ERR_PTR(-ENAMETOOLONG);
511
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));
515
516 if (d_mountpoint(dentry))
517 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
518
519 if (it == NULL || it->it_op == IT_GETXATTR)
520 it = &lookup_it;
521
522 if (it->it_op == IT_GETATTR) {
523 rc = ll_statahead_enter(parent, &dentry, 0);
524 if (rc == 1) {
525 if (dentry == save)
526 GOTO(out, retval = NULL);
527 GOTO(out, retval = dentry);
528 }
529 }
530
531 if (it->it_op & IT_CREAT)
532 opc = LUSTRE_OPC_CREATE;
533 else
534 opc = LUSTRE_OPC_ANY;
535
536 op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
537 dentry->d_name.len, lookup_flags, opc,
538 NULL);
539 if (IS_ERR(op_data))
540 return (void *)op_data;
541
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();
545
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);
549 if (rc < 0)
550 GOTO(out, retval = ERR_PTR(rc));
551
552 rc = ll_lookup_it_finish(req, it, parent, &dentry);
553 if (rc != 0) {
554 ll_intent_release(it);
555 GOTO(out, retval = ERR_PTR(rc));
556 }
557
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);
562 }
563 ll_lookup_finish_locks(it, dentry);
564
565 if (dentry == save)
566 GOTO(out, retval = NULL);
567 else
568 GOTO(out, retval = dentry);
569 out:
570 if (req)
571 ptlrpc_req_finished(req);
572 if (it->it_op == IT_GETATTR && (retval == NULL || retval == dentry))
573 ll_statahead_mark(parent, dentry);
574 return retval;
575 }
576
577 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
578 unsigned int flags)
579 {
580 struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
581 struct dentry *de;
582
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);
586
587 /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
588 if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
589 return NULL;
590
591 if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
592 itp = NULL;
593 else
594 itp = &it;
595 de = ll_lookup_it(parent, dentry, itp, 0);
596
597 if (itp != NULL)
598 ll_intent_release(itp);
599
600 return de;
601 }
602
603 /*
604 * For cached negative dentry and new dentry, handle lookup/create/open
605 * together.
606 */
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)
610 {
611 struct lookup_intent *it;
612 struct dentry *de;
613 long long lookup_flags = LOOKUP_OPEN;
614 int rc = 0;
615
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);
620
621 OBD_ALLOC(it, sizeof(*it));
622 if (!it)
623 return -ENOMEM;
624
625 it->it_op = IT_OPEN;
626 if (open_flags & O_CREAT) {
627 it->it_op |= IT_CREAT;
628 lookup_flags |= LOOKUP_CREATE;
629 }
630 it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
631 it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
632
633 /* Dentry added to dcache tree in ll_lookup_it */
634 de = ll_lookup_it(dir, dentry, it, lookup_flags);
635 if (IS_ERR(de))
636 rc = PTR_ERR(de);
637 else if (de != NULL)
638 dentry = de;
639
640 if (!rc) {
641 if (it_disposition(it, DISP_OPEN_CREATE)) {
642 /* Dentry instantiated in ll_create_it. */
643 rc = ll_create_it(dir, dentry, mode, it);
644 if (rc) {
645 /* We dget in ll_splice_alias. */
646 if (de != NULL)
647 dput(de);
648 goto out_release;
649 }
650
651 *opened |= FILE_CREATED;
652 }
653 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
654 /* Open dentry. */
655 if (S_ISFIFO(dentry->d_inode->i_mode)) {
656 /* We cannot call open here as it would
657 * deadlock.
658 */
659 if (it_disposition(it, DISP_ENQ_OPEN_REF))
660 ptlrpc_req_finished(
661 (struct ptlrpc_request *)
662 it->d.lustre.it_data);
663 rc = finish_no_open(file, de);
664 } else {
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.
669 */
670 if (de != NULL)
671 dput(de);
672 }
673 } else {
674 rc = finish_no_open(file, de);
675 }
676 }
677
678 out_release:
679 ll_intent_release(it);
680 OBD_FREE(it, sizeof(*it));
681
682 return rc;
683 }
684
685
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)
688 {
689 struct inode *inode = NULL;
690 struct ptlrpc_request *request = NULL;
691 struct ll_sb_info *sbi = ll_i2sbi(dir);
692 int rc;
693
694 LASSERT(it && it->d.lustre.it_disposition);
695
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);
700 if (rc)
701 GOTO(out, inode = ERR_PTR(rc));
702
703 LASSERT(ll_d_hlist_empty(&inode->i_dentry));
704
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);
711 out:
712 ptlrpc_req_finished(request);
713 return inode;
714 }
715
716 /*
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.
719 *
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).
726 *
727 * If the create succeeds, we fill in the inode information
728 * with d_instantiate().
729 */
730 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
731 struct lookup_intent *it)
732 {
733 struct inode *inode;
734 int rc = 0;
735
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));
739
740 rc = it_open_error(DISP_OPEN_CREATE, it);
741 if (rc)
742 return rc;
743
744 inode = ll_create_node(dir, it);
745 if (IS_ERR(inode))
746 return PTR_ERR(inode);
747
748 d_instantiate(dentry, inode);
749 return 0;
750 }
751
752 static void ll_update_times(struct ptlrpc_request *request,
753 struct inode *inode)
754 {
755 struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
756 &RMF_MDT_BODY);
757
758 LASSERT(body);
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;
764 }
765 if (body->valid & OBD_MD_FLCTIME &&
766 body->ctime > LTIME_S(inode->i_ctime))
767 LTIME_S(inode->i_ctime) = body->ctime;
768 }
769
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)
773 {
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);
778 int tgt_len = 0;
779 int err;
780
781 if (unlikely(tgt != NULL))
782 tgt_len = strlen(tgt) + 1;
783
784 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
785 name->len, 0, opc, NULL);
786 if (IS_ERR(op_data))
787 GOTO(err_exit, err = PTR_ERR(op_data));
788
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);
794 if (err)
795 GOTO(err_exit, err);
796
797 ll_update_times(request, dir);
798
799 if (dchild) {
800 err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
801 if (err)
802 GOTO(err_exit, err);
803
804 d_instantiate(dchild, inode);
805 }
806 err_exit:
807 ptlrpc_req_finished(request);
808
809 return err;
810 }
811
812 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
813 unsigned rdev, struct dentry *dchild)
814 {
815 int err;
816
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,
819 mode, rdev);
820
821 if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
822 mode &= ~current_umask();
823
824 switch (mode & S_IFMT) {
825 case 0:
826 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
827 case S_IFREG:
828 case S_IFCHR:
829 case S_IFBLK:
830 case S_IFIFO:
831 case S_IFSOCK:
832 err = ll_new_node(dir, name, NULL, mode, rdev, dchild,
833 LUSTRE_OPC_MKNOD);
834 break;
835 case S_IFDIR:
836 err = -EPERM;
837 break;
838 default:
839 err = -EINVAL;
840 }
841
842 if (!err)
843 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
844
845 return err;
846 }
847
848 /*
849 * Plain create. Intent create is handled in atomic_open.
850 */
851 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
852 umode_t mode, bool want_excl)
853 {
854 int rc;
855
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);
860
861 rc = ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
862
863 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
864
865 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
866 dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
867
868 return rc;
869 }
870
871 static int ll_symlink_generic(struct inode *dir, struct qstr *name,
872 const char *tgt, struct dentry *dchild)
873 {
874 int err;
875
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,
878 dir, 3000, tgt);
879
880 err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
881 0, dchild, LUSTRE_OPC_SYMLINK);
882
883 if (!err)
884 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
885
886 return err;
887 }
888
889 static int ll_link_generic(struct inode *src, struct inode *dir,
890 struct qstr *name, struct dentry *dchild)
891 {
892 struct ll_sb_info *sbi = ll_i2sbi(dir);
893 struct ptlrpc_request *request = NULL;
894 struct md_op_data *op_data;
895 int err;
896
897 CDEBUG(D_VFSTRACE,
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);
901
902 op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
903 0, LUSTRE_OPC_ANY, NULL);
904 if (IS_ERR(op_data))
905 return PTR_ERR(op_data);
906
907 err = md_link(sbi->ll_md_exp, op_data, &request);
908 ll_finish_md_op_data(op_data);
909 if (err)
910 GOTO(out, err);
911
912 ll_update_times(request, dir);
913 ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
914 out:
915 ptlrpc_req_finished(request);
916 return err;
917 }
918
919 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
920 int mode, struct dentry *dchild)
921
922 {
923 int err;
924
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);
927
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);
932
933 if (!err)
934 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
935
936 return err;
937 }
938
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,
942 struct lu_fid *fid)
943 {
944 struct dentry *parent, *child;
945
946 parent = ll_d_hlist_entry(dir->i_dentry, struct dentry, d_alias);
947 child = d_lookup(parent, name);
948 if (child) {
949 if (child->d_inode)
950 *fid = *ll_inode2fid(child->d_inode);
951 dput(child);
952 }
953 }
954
955 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
956 struct dentry *dchild, struct qstr *name)
957 {
958 struct ptlrpc_request *request = NULL;
959 struct md_op_data *op_data;
960 int rc;
961
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);
964
965 if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
966 return -EBUSY;
967
968 op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
969 S_IFDIR, LUSTRE_OPC_ANY, NULL);
970 if (IS_ERR(op_data))
971 return PTR_ERR(op_data);
972
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);
977 if (rc == 0) {
978 ll_update_times(request, dir);
979 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
980 }
981
982 ptlrpc_req_finished(request);
983 return rc;
984 }
985
986 /**
987 * Remove dir entry
988 **/
989 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
990 {
991 struct ptlrpc_request *request = NULL;
992 struct md_op_data *op_data;
993 int rc;
994
995 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
996 namelen, name, dir->i_ino, dir->i_generation, dir);
997
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);
1005 if (rc == 0) {
1006 ll_update_times(request, dir);
1007 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1008 }
1009
1010 ptlrpc_req_finished(request);
1011 return rc;
1012 }
1013
1014 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
1015 {
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 };
1020 struct obdo *oa;
1021 struct obd_capa *oc = NULL;
1022 int rc;
1023
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))
1027 return 0;
1028
1029 if (body->eadatasize == 0) {
1030 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1031 GOTO(out, rc = -EPROTO);
1032 }
1033
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,
1039 body->eadatasize);
1040 LASSERT(eadata != NULL);
1041
1042 rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
1043 if (rc < 0) {
1044 CERROR("obd_unpackmd: %d\n", rc);
1045 GOTO(out, rc);
1046 }
1047 LASSERT(rc >= sizeof(*lsm));
1048
1049 OBDO_ALLOC(oa);
1050 if (oa == NULL)
1051 GOTO(out_free_memmd, rc = -ENOMEM);
1052
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;
1056
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,
1061 &RMF_LOGCOOKIES,
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;
1067 }
1068 }
1069
1070 if (body->valid & OBD_MD_FLOSSCAPA) {
1071 rc = md_unpack_capa(ll_i2mdexp(dir), request, &RMF_CAPA2, &oc);
1072 if (rc)
1073 GOTO(out_free_memmd, rc);
1074 }
1075
1076 rc = obd_destroy(NULL, ll_i2dtexp(dir), oa, lsm, &oti,
1077 ll_i2mdexp(dir), oc);
1078 capa_put(oc);
1079 if (rc)
1080 CERROR("obd destroy objid "DOSTID" error %d\n",
1081 POSTID(&lsm->lsm_oi), rc);
1082 out_free_memmd:
1083 obd_free_memmd(ll_i2dtexp(dir), &lsm);
1084 OBDO_FREE(oa);
1085 out:
1086 return rc;
1087 }
1088
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
1092 * too. b=20433 */
1093 static int ll_unlink_generic(struct inode *dir, struct dentry *dparent,
1094 struct dentry *dchild, struct qstr *name)
1095 {
1096 struct ptlrpc_request *request = NULL;
1097 struct md_op_data *op_data;
1098 int rc;
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);
1101
1102 /*
1103 * XXX: unlink bind mountpoint maybe call to here,
1104 * just check it as vfs_unlink does.
1105 */
1106 if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
1107 return -EBUSY;
1108
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);
1113
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);
1118 if (rc)
1119 GOTO(out, rc);
1120
1121 ll_update_times(request, dir);
1122 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1123
1124 rc = ll_objects_destroy(request, dir);
1125 out:
1126 ptlrpc_req_finished(request);
1127 return rc;
1128 }
1129
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)
1134 {
1135 struct ptlrpc_request *request = NULL;
1136 struct ll_sb_info *sbi = ll_i2sbi(src);
1137 struct md_op_data *op_data;
1138 int err;
1139
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);
1144
1145 if (unlikely(ll_d_mountpoint(src_dparent, src_dchild, src_name) ||
1146 ll_d_mountpoint(tgt_dparent, tgt_dchild, tgt_name)))
1147 return -EBUSY;
1148
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);
1153
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);
1160 if (!err) {
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);
1165 }
1166
1167 ptlrpc_req_finished(request);
1168
1169 return err;
1170 }
1171
1172 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1173 dev_t rdev)
1174 {
1175 return ll_mknod_generic(dir, &dchild->d_name, mode,
1176 old_encode_dev(rdev), dchild);
1177 }
1178
1179 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1180 {
1181 return ll_unlink_generic(dir, NULL, dentry, &dentry->d_name);
1182 }
1183
1184 static int ll_mkdir(struct inode *dir, struct dentry *dentry, ll_umode_t mode)
1185 {
1186 return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1187 }
1188
1189 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1190 {
1191 return ll_rmdir_generic(dir, NULL, dentry, &dentry->d_name);
1192 }
1193
1194 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1195 const char *oldname)
1196 {
1197 return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1198 }
1199
1200 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1201 struct dentry *new_dentry)
1202 {
1203 return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name,
1204 new_dentry);
1205 }
1206
1207 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1208 struct inode *new_dir, struct dentry *new_dentry)
1209 {
1210 int err;
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);
1215 if (!err) {
1216 d_move(old_dentry, new_dentry);
1217 }
1218 return err;
1219 }
1220
1221 const struct inode_operations ll_dir_inode_operations = {
1222 .mknod = ll_mknod,
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,
1228 .mkdir = ll_mkdir,
1229 .rmdir = ll_rmdir,
1230 .symlink = ll_symlink,
1231 .link = ll_link,
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,
1241 };
1242
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,
1252 };
This page took 0.057494 seconds and 6 git commands to generate.