staging/lustre/llite: Adjust comments to better conform to coding style
[cascardo/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, 2015, 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 "../include/obd_support.h"
48 #include "../include/lustre_fid.h"
49 #include "../include/lustre_lite.h"
50 #include "../include/lustre_dlm.h"
51 #include "../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 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
58 static int ll_test_inode(struct inode *inode, void *opaque)
59 {
60         struct ll_inode_info *lli = ll_i2info(inode);
61         struct lustre_md     *md = opaque;
62
63         if (unlikely(!(md->body->valid & OBD_MD_FLID))) {
64                 CERROR("MDS body missing FID\n");
65                 return 0;
66         }
67
68         if (!lu_fid_eq(&lli->lli_fid, &md->body->fid1))
69                 return 0;
70
71         return 1;
72 }
73
74 static int ll_set_inode(struct inode *inode, void *opaque)
75 {
76         struct ll_inode_info *lli = ll_i2info(inode);
77         struct mdt_body *body = ((struct lustre_md *)opaque)->body;
78
79         if (unlikely(!(body->valid & OBD_MD_FLID))) {
80                 CERROR("MDS body missing FID\n");
81                 return -EINVAL;
82         }
83
84         lli->lli_fid = body->fid1;
85         if (unlikely(!(body->valid & OBD_MD_FLTYPE))) {
86                 CERROR("Can not initialize inode " DFID
87                        " without object type: valid = %#llx\n",
88                        PFID(&lli->lli_fid), body->valid);
89                 return -EINVAL;
90         }
91
92         inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mode & S_IFMT);
93         if (unlikely(inode->i_mode == 0)) {
94                 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
95                 return -EINVAL;
96         }
97
98         ll_lli_init(lli);
99
100         return 0;
101 }
102
103 /*
104  * Get an inode by inode number (already instantiated by the intent lookup).
105  * Returns inode or NULL
106  */
107 struct inode *ll_iget(struct super_block *sb, ino_t hash,
108                       struct lustre_md *md)
109 {
110         struct inode     *inode;
111
112         LASSERT(hash != 0);
113         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
114
115         if (inode) {
116                 if (inode->i_state & I_NEW) {
117                         int rc = 0;
118
119                         ll_read_inode2(inode, md);
120                         if (S_ISREG(inode->i_mode) &&
121                             !ll_i2info(inode)->lli_clob) {
122                                 CDEBUG(D_INODE,
123                                         "%s: apply lsm %p to inode "DFID".\n",
124                                         ll_get_fsname(sb, NULL, 0), md->lsm,
125                                         PFID(ll_inode2fid(inode)));
126                                 rc = cl_file_inode_init(inode, md);
127                         }
128                         if (rc != 0) {
129                                 iget_failed(inode);
130                                 inode = ERR_PTR(rc);
131                         } else
132                                 unlock_new_inode(inode);
133                 } else if (!(inode->i_state & (I_FREEING | I_CLEAR)))
134                         ll_update_inode(inode, md);
135                 CDEBUG(D_VFSTRACE, "got inode: %p for "DFID"\n",
136                        inode, PFID(&md->body->fid1));
137         }
138         return inode;
139 }
140
141 static void ll_invalidate_negative_children(struct inode *dir)
142 {
143         struct dentry *dentry, *tmp_subdir;
144
145         ll_lock_dcache(dir);
146         hlist_for_each_entry(dentry, &dir->i_dentry, d_u.d_alias) {
147                 spin_lock(&dentry->d_lock);
148                 if (!list_empty(&dentry->d_subdirs)) {
149                         struct dentry *child;
150
151                         list_for_each_entry_safe(child, tmp_subdir,
152                                                  &dentry->d_subdirs,
153                                                  d_child) {
154                                 if (d_really_is_negative(child))
155                                         d_lustre_invalidate(child, 1);
156                         }
157                 }
158                 spin_unlock(&dentry->d_lock);
159         }
160         ll_unlock_dcache(dir);
161 }
162
163 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
164                        void *data, int flag)
165 {
166         struct lustre_handle lockh;
167         int rc;
168
169         switch (flag) {
170         case LDLM_CB_BLOCKING:
171                 ldlm_lock2handle(lock, &lockh);
172                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
173                 if (rc < 0) {
174                         CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
175                         return rc;
176                 }
177                 break;
178         case LDLM_CB_CANCELING: {
179                 struct inode *inode = ll_inode_from_resource_lock(lock);
180                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
181
182                 /* Inode is set to lock->l_resource->lr_lvb_inode
183                  * for mdc - bug 24555
184                  */
185                 LASSERT(!lock->l_ast_data);
186
187                 if (!inode)
188                         break;
189
190                 /* Invalidate all dentries associated with this inode */
191                 LASSERT(lock->l_flags & LDLM_FL_CANCELING);
192
193                 if (!fid_res_name_eq(ll_inode2fid(inode),
194                                      &lock->l_resource->lr_name)) {
195                         LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
196                                    PFID(ll_inode2fid(inode)), inode);
197                         LBUG();
198                 }
199
200                 if (bits & MDS_INODELOCK_XATTR) {
201                         ll_xattr_cache_destroy(inode);
202                         bits &= ~MDS_INODELOCK_XATTR;
203                 }
204
205                 /* For OPEN locks we differentiate between lock modes
206                  * LCK_CR, LCK_CW, LCK_PR - bug 22891
207                  */
208                 if (bits & MDS_INODELOCK_OPEN)
209                         ll_have_md_lock(inode, &bits, lock->l_req_mode);
210
211                 if (bits & MDS_INODELOCK_OPEN) {
212                         fmode_t fmode;
213
214                         switch (lock->l_req_mode) {
215                         case LCK_CW:
216                                 fmode = FMODE_WRITE;
217                                 break;
218                         case LCK_PR:
219                                 fmode = FMODE_EXEC;
220                                 break;
221                         case LCK_CR:
222                                 fmode = FMODE_READ;
223                                 break;
224                         default:
225                                 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
226                                 LBUG();
227                         }
228
229                         ll_md_real_close(inode, fmode);
230                 }
231
232                 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
233                             MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
234                         ll_have_md_lock(inode, &bits, LCK_MINMODE);
235
236                 if (bits & MDS_INODELOCK_LAYOUT) {
237                         struct cl_object_conf conf = {
238                                 .coc_opc = OBJECT_CONF_INVALIDATE,
239                                 .coc_inode = inode,
240                         };
241
242                         rc = ll_layout_conf(inode, &conf);
243                         if (rc < 0)
244                                 CDEBUG(D_INODE, "cannot invalidate layout of "
245                                        DFID": rc = %d\n",
246                                        PFID(ll_inode2fid(inode)), rc);
247                 }
248
249                 if (bits & MDS_INODELOCK_UPDATE) {
250                         struct ll_inode_info *lli = ll_i2info(inode);
251
252                         spin_lock(&lli->lli_lock);
253                         lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
254                         spin_unlock(&lli->lli_lock);
255                 }
256
257                 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
258                         CDEBUG(D_INODE, "invalidating inode %lu\n",
259                                inode->i_ino);
260                         truncate_inode_pages(inode->i_mapping, 0);
261                         ll_invalidate_negative_children(inode);
262                 }
263
264                 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
265                     inode->i_sb->s_root &&
266                     !is_root_inode(inode))
267                         ll_invalidate_aliases(inode);
268
269                 iput(inode);
270                 break;
271         }
272         default:
273                 LBUG();
274         }
275
276         return 0;
277 }
278
279 __u32 ll_i2suppgid(struct inode *i)
280 {
281         if (in_group_p(i->i_gid))
282                 return (__u32)from_kgid(&init_user_ns, i->i_gid);
283         else
284                 return (__u32)(-1);
285 }
286
287 /* Pack the required supplementary groups into the supplied groups array.
288  * If we don't need to use the groups from the target inode(s) then we
289  * instead pack one or more groups from the user's supplementary group
290  * array in case it might be useful.  Not needed if doing an MDS-side upcall.
291  */
292 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
293 {
294         LASSERT(i1);
295
296         suppgids[0] = ll_i2suppgid(i1);
297
298         if (i2)
299                 suppgids[1] = ll_i2suppgid(i2);
300                 else
301                         suppgids[1] = -1;
302 }
303
304 /*
305  * try to reuse three types of dentry:
306  * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
307  *    by concurrent .revalidate).
308  * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
309  *    be cleared by others calling d_lustre_revalidate).
310  * 3. DISCONNECTED alias.
311  */
312 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
313 {
314         struct dentry *alias, *discon_alias, *invalid_alias;
315
316         if (hlist_empty(&inode->i_dentry))
317                 return NULL;
318
319         discon_alias = invalid_alias = NULL;
320
321         ll_lock_dcache(inode);
322         hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
323                 LASSERT(alias != dentry);
324
325                 spin_lock(&alias->d_lock);
326                 if (alias->d_flags & DCACHE_DISCONNECTED)
327                         /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
328                         discon_alias = alias;
329                 else if (alias->d_parent == dentry->d_parent         &&
330                          alias->d_name.hash == dentry->d_name.hash       &&
331                          alias->d_name.len == dentry->d_name.len         &&
332                          memcmp(alias->d_name.name, dentry->d_name.name,
333                                 dentry->d_name.len) == 0)
334                         invalid_alias = alias;
335                 spin_unlock(&alias->d_lock);
336
337                 if (invalid_alias)
338                         break;
339         }
340         alias = invalid_alias ?: discon_alias ?: NULL;
341         if (alias) {
342                 spin_lock(&alias->d_lock);
343                 dget_dlock(alias);
344                 spin_unlock(&alias->d_lock);
345         }
346         ll_unlock_dcache(inode);
347
348         return alias;
349 }
350
351 /*
352  * Similar to d_splice_alias(), but lustre treats invalid alias
353  * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
354  */
355 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
356 {
357         struct dentry *new;
358         int rc;
359
360         if (inode) {
361                 new = ll_find_alias(inode, de);
362                 if (new) {
363                         rc = ll_d_init(new);
364                         if (rc < 0) {
365                                 dput(new);
366                                 return ERR_PTR(rc);
367                         }
368                         d_move(new, de);
369                         iput(inode);
370                         CDEBUG(D_DENTRY,
371                                "Reuse dentry %p inode %p refc %d flags %#x\n",
372                               new, d_inode(new), d_count(new), new->d_flags);
373                         return new;
374                 }
375         }
376         rc = ll_d_init(de);
377         if (rc < 0)
378                 return ERR_PTR(rc);
379         d_add(de, inode);
380         CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
381                de, d_inode(de), d_count(de), de->d_flags);
382         return de;
383 }
384
385 static int ll_lookup_it_finish(struct ptlrpc_request *request,
386                                struct lookup_intent *it,
387                                struct inode *parent, struct dentry **de)
388 {
389         struct inode *inode = NULL;
390         __u64 bits = 0;
391         int rc = 0;
392
393         /* NB 1 request reference will be taken away by ll_intent_lock()
394          * when I return
395          */
396         CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
397                it->d.lustre.it_disposition);
398         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
399                 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
400                 if (rc)
401                         return rc;
402
403                 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
404
405                 /* We used to query real size from OSTs here, but actually
406                  * this is not needed. For stat() calls size would be updated
407                  * from subsequent do_revalidate()->ll_inode_revalidate_it() in
408                  * 2.4 and
409                  * vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
410                  * Everybody else who needs correct file size would call
411                  * ll_glimpse_size or some equivalent themselves anyway.
412                  * Also see bug 7198.
413                  */
414         }
415
416         /* Only hash *de if it is unhashed (new dentry).
417          * Atoimc_open may passing hashed dentries for open.
418          */
419         if (d_unhashed(*de)) {
420                 struct dentry *alias;
421
422                 alias = ll_splice_alias(inode, *de);
423                 if (IS_ERR(alias)) {
424                         rc = PTR_ERR(alias);
425                         goto out;
426                 }
427                 *de = alias;
428         } else if (!it_disposition(it, DISP_LOOKUP_NEG)  &&
429                    !it_disposition(it, DISP_OPEN_CREATE)) {
430                 /* With DISP_OPEN_CREATE dentry will be
431                  * instantiated in ll_create_it.
432                  */
433                 LASSERT(!d_inode(*de));
434                 d_instantiate(*de, inode);
435         }
436
437         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
438                 /* we have lookup look - unhide dentry */
439                 if (bits & MDS_INODELOCK_LOOKUP)
440                         d_lustre_revalidate(*de);
441         } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
442                 /* If file created on server, don't depend on parent UPDATE
443                  * lock to unhide it. It is left hidden and next lookup can
444                  * find it in ll_splice_alias.
445                  */
446                 /* Check that parent has UPDATE lock. */
447                 struct lookup_intent parent_it = {
448                                         .it_op = IT_GETATTR,
449                                         .d.lustre.it_lock_handle = 0 };
450
451                 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it,
452                                        &ll_i2info(parent)->lli_fid, NULL)) {
453                         d_lustre_revalidate(*de);
454                         ll_intent_release(&parent_it);
455                 }
456         }
457
458 out:
459         if (rc != 0 && it->it_op & IT_OPEN)
460                 ll_open_cleanup((*de)->d_sb, request);
461
462         return rc;
463 }
464
465 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
466                                    struct lookup_intent *it, int lookup_flags)
467 {
468         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
469         struct dentry *save = dentry, *retval;
470         struct ptlrpc_request *req = NULL;
471         struct inode *inode;
472         struct md_op_data *op_data;
473         __u32 opc;
474         int rc;
475
476         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
477                 return ERR_PTR(-ENAMETOOLONG);
478
479         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),intent=%s\n",
480                dentry, parent->i_ino,
481                parent->i_generation, parent, LL_IT2STR(it));
482
483         if (d_mountpoint(dentry))
484                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
485
486         if (!it || it->it_op == IT_GETXATTR)
487                 it = &lookup_it;
488
489         if (it->it_op == IT_GETATTR) {
490                 rc = ll_statahead_enter(parent, &dentry, 0);
491                 if (rc == 1) {
492                         if (dentry == save)
493                                 retval = NULL;
494                         else
495                                 retval = dentry;
496                         goto out;
497                 }
498         }
499
500         if (it->it_op & IT_CREAT)
501                 opc = LUSTRE_OPC_CREATE;
502         else
503                 opc = LUSTRE_OPC_ANY;
504
505         op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
506                                      dentry->d_name.len, lookup_flags, opc,
507                                      NULL);
508         if (IS_ERR(op_data))
509                 return (void *)op_data;
510
511         /* enforce umask if acl disabled or MDS doesn't support umask */
512         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
513                 it->it_create_mode &= ~current_umask();
514
515         rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
516                             lookup_flags, &req, ll_md_blocking_ast, 0);
517         ll_finish_md_op_data(op_data);
518         if (rc < 0) {
519                 retval = ERR_PTR(rc);
520                 goto out;
521         }
522
523         rc = ll_lookup_it_finish(req, it, parent, &dentry);
524         if (rc != 0) {
525                 ll_intent_release(it);
526                 retval = ERR_PTR(rc);
527                 goto out;
528         }
529
530         inode = d_inode(dentry);
531         if ((it->it_op & IT_OPEN) && inode &&
532             !S_ISREG(inode->i_mode) &&
533             !S_ISDIR(inode->i_mode)) {
534                 ll_release_openhandle(inode, it);
535         }
536         ll_lookup_finish_locks(it, inode);
537
538         if (dentry == save)
539                 retval = NULL;
540         else
541                 retval = dentry;
542  out:
543         if (req)
544                 ptlrpc_req_finished(req);
545         if (it->it_op == IT_GETATTR && (!retval || retval == dentry))
546                 ll_statahead_mark(parent, dentry);
547         return retval;
548 }
549
550 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
551                                    unsigned int flags)
552 {
553         struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
554         struct dentry *de;
555
556         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),flags=%u\n",
557                dentry, parent->i_ino,
558                parent->i_generation, parent, flags);
559
560         /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
561         if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
562                 return NULL;
563
564         if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
565                 itp = NULL;
566         else
567                 itp = &it;
568         de = ll_lookup_it(parent, dentry, itp, 0);
569
570         if (itp)
571                 ll_intent_release(itp);
572
573         return de;
574 }
575
576 /*
577  * For cached negative dentry and new dentry, handle lookup/create/open
578  * together.
579  */
580 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
581                           struct file *file, unsigned open_flags,
582                           umode_t mode, int *opened)
583 {
584         struct lookup_intent *it;
585         struct dentry *de;
586         long long lookup_flags = LOOKUP_OPEN;
587         int rc = 0;
588
589         CDEBUG(D_VFSTRACE,
590                "VFS Op:name=%pd,dir=%lu/%u(%p),file %p,open_flags %x,mode %x opened %d\n",
591                dentry, dir->i_ino,
592                dir->i_generation, dir, file, open_flags, mode, *opened);
593
594         it = kzalloc(sizeof(*it), GFP_NOFS);
595         if (!it)
596                 return -ENOMEM;
597
598         it->it_op = IT_OPEN;
599         if (open_flags & O_CREAT) {
600                 it->it_op |= IT_CREAT;
601                 lookup_flags |= LOOKUP_CREATE;
602         }
603         it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
604         it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
605
606         /* Dentry added to dcache tree in ll_lookup_it */
607         de = ll_lookup_it(dir, dentry, it, lookup_flags);
608         if (IS_ERR(de))
609                 rc = PTR_ERR(de);
610         else if (de)
611                 dentry = de;
612
613         if (!rc) {
614                 if (it_disposition(it, DISP_OPEN_CREATE)) {
615                         /* Dentry instantiated in ll_create_it. */
616                         rc = ll_create_it(dir, dentry, mode, it);
617                         if (rc) {
618                                 /* We dget in ll_splice_alias. */
619                                 if (de)
620                                         dput(de);
621                                 goto out_release;
622                         }
623
624                         *opened |= FILE_CREATED;
625                 }
626                 if (d_really_is_positive(dentry) && it_disposition(it, DISP_OPEN_OPEN)) {
627                         /* Open dentry. */
628                         if (S_ISFIFO(d_inode(dentry)->i_mode)) {
629                                 /* We cannot call open here as it would
630                                  * deadlock.
631                                  */
632                                 if (it_disposition(it, DISP_ENQ_OPEN_REF))
633                                         ptlrpc_req_finished(
634                                                        (struct ptlrpc_request *)
635                                                           it->d.lustre.it_data);
636                                 rc = finish_no_open(file, de);
637                         } else {
638                                 file->private_data = it;
639                                 rc = finish_open(file, dentry, NULL, opened);
640                                 /* We dget in ll_splice_alias. finish_open takes
641                                  * care of dget for fd open.
642                                  */
643                                 if (de)
644                                         dput(de);
645                         }
646                 } else {
647                         rc = finish_no_open(file, de);
648                 }
649         }
650
651 out_release:
652         ll_intent_release(it);
653         kfree(it);
654
655         return rc;
656 }
657
658 /* We depend on "mode" being set with the proper file type/umask by now */
659 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
660 {
661         struct inode *inode = NULL;
662         struct ptlrpc_request *request = NULL;
663         struct ll_sb_info *sbi = ll_i2sbi(dir);
664         int rc;
665
666         LASSERT(it && it->d.lustre.it_disposition);
667
668         LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
669         request = it->d.lustre.it_data;
670         it_clear_disposition(it, DISP_ENQ_CREATE_REF);
671         rc = ll_prep_inode(&inode, request, dir->i_sb, it);
672         if (rc) {
673                 inode = ERR_PTR(rc);
674                 goto out;
675         }
676
677         LASSERT(hlist_empty(&inode->i_dentry));
678
679         /* We asked for a lock on the directory, but were granted a
680          * lock on the inode.  Since we finally have an inode pointer,
681          * stuff it in the lock.
682          */
683         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
684                inode, inode->i_ino, inode->i_generation);
685         ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
686  out:
687         ptlrpc_req_finished(request);
688         return inode;
689 }
690
691 /*
692  * By the time this is called, we already have created the directory cache
693  * entry for the new file, but it is so far negative - it has no inode.
694  *
695  * We defer creating the OBD object(s) until open, to keep the intent and
696  * non-intent code paths similar, and also because we do not have the MDS
697  * inode number before calling ll_create_node() (which is needed for LOV),
698  * so we would need to do yet another RPC to the MDS to store the LOV EA
699  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
700  * lmm_size in datalen (the MDS still has code which will handle that).
701  *
702  * If the create succeeds, we fill in the inode information
703  * with d_instantiate().
704  */
705 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
706                         struct lookup_intent *it)
707 {
708         struct inode *inode;
709         int rc = 0;
710
711         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),intent=%s\n",
712                dentry, dir->i_ino,
713                dir->i_generation, dir, LL_IT2STR(it));
714
715         rc = it_open_error(DISP_OPEN_CREATE, it);
716         if (rc)
717                 return rc;
718
719         inode = ll_create_node(dir, it);
720         if (IS_ERR(inode))
721                 return PTR_ERR(inode);
722
723         d_instantiate(dentry, inode);
724         return 0;
725 }
726
727 static void ll_update_times(struct ptlrpc_request *request,
728                             struct inode *inode)
729 {
730         struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
731                                                        &RMF_MDT_BODY);
732
733         LASSERT(body);
734         if (body->valid & OBD_MD_FLMTIME &&
735             body->mtime > LTIME_S(inode->i_mtime)) {
736                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
737                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
738                 LTIME_S(inode->i_mtime) = body->mtime;
739         }
740         if (body->valid & OBD_MD_FLCTIME &&
741             body->ctime > LTIME_S(inode->i_ctime))
742                 LTIME_S(inode->i_ctime) = body->ctime;
743 }
744
745 static int ll_new_node(struct inode *dir, struct dentry *dentry,
746                        const char *tgt, int mode, int rdev,
747                        __u32 opc)
748 {
749         struct ptlrpc_request *request = NULL;
750         struct md_op_data *op_data;
751         struct inode *inode = NULL;
752         struct ll_sb_info *sbi = ll_i2sbi(dir);
753         int tgt_len = 0;
754         int err;
755
756         if (unlikely(tgt))
757                 tgt_len = strlen(tgt) + 1;
758
759         op_data = ll_prep_md_op_data(NULL, dir, NULL,
760                                      dentry->d_name.name,
761                                      dentry->d_name.len,
762                                      0, opc, NULL);
763         if (IS_ERR(op_data)) {
764                 err = PTR_ERR(op_data);
765                 goto err_exit;
766         }
767
768         err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
769                         from_kuid(&init_user_ns, current_fsuid()),
770                         from_kgid(&init_user_ns, current_fsgid()),
771                         cfs_curproc_cap_pack(), rdev, &request);
772         ll_finish_md_op_data(op_data);
773         if (err)
774                 goto err_exit;
775
776         ll_update_times(request, dir);
777
778         err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
779         if (err)
780                 goto err_exit;
781
782         d_instantiate(dentry, inode);
783 err_exit:
784         ptlrpc_req_finished(request);
785
786         return err;
787 }
788
789 static int ll_mknod(struct inode *dir, struct dentry *dchild,
790                     umode_t mode, dev_t rdev)
791 {
792         int err;
793
794         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p) mode %o dev %x\n",
795                dchild, dir->i_ino, dir->i_generation, dir,
796                mode, old_encode_dev(rdev));
797
798         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
799                 mode &= ~current_umask();
800
801         switch (mode & S_IFMT) {
802         case 0:
803                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
804         case S_IFREG:
805         case S_IFCHR:
806         case S_IFBLK:
807         case S_IFIFO:
808         case S_IFSOCK:
809                 err = ll_new_node(dir, dchild, NULL, mode,
810                                   old_encode_dev(rdev),
811                                   LUSTRE_OPC_MKNOD);
812                 break;
813         case S_IFDIR:
814                 err = -EPERM;
815                 break;
816         default:
817                 err = -EINVAL;
818         }
819
820         if (!err)
821                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
822
823         return err;
824 }
825
826 /*
827  * Plain create. Intent create is handled in atomic_open.
828  */
829 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
830                         umode_t mode, bool want_excl)
831 {
832         int rc;
833
834         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),flags=%u, excl=%d\n",
835                dentry, dir->i_ino,
836                dir->i_generation, dir, mode, want_excl);
837
838         rc = ll_mknod(dir, dentry, mode, 0);
839
840         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
841
842         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
843                dentry, d_unhashed(dentry));
844
845         return rc;
846 }
847
848 static inline void ll_get_child_fid(struct dentry *child, struct lu_fid *fid)
849 {
850         if (d_really_is_positive(child))
851                 *fid = *ll_inode2fid(d_inode(child));
852 }
853
854 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
855 {
856         struct mdt_body *body;
857         struct lov_mds_md *eadata;
858         struct lov_stripe_md *lsm = NULL;
859         struct obd_trans_info oti = { 0 };
860         struct obdo *oa;
861         int rc;
862
863         /* req is swabbed so this is safe */
864         body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
865         if (!(body->valid & OBD_MD_FLEASIZE))
866                 return 0;
867
868         if (body->eadatasize == 0) {
869                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
870                 rc = -EPROTO;
871                 goto out;
872         }
873
874         /* The MDS sent back the EA because we unlinked the last reference
875          * to this file. Use this EA to unlink the objects on the OST.
876          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
877          * check it is complete and sensible.
878          */
879         eadata = req_capsule_server_sized_get(&request->rq_pill, &RMF_MDT_MD,
880                                               body->eadatasize);
881         LASSERT(eadata);
882
883         rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
884         if (rc < 0) {
885                 CERROR("obd_unpackmd: %d\n", rc);
886                 goto out;
887         }
888         LASSERT(rc >= sizeof(*lsm));
889
890         oa = kmem_cache_alloc(obdo_cachep, GFP_NOFS | __GFP_ZERO);
891         if (!oa) {
892                 rc = -ENOMEM;
893                 goto out_free_memmd;
894         }
895
896         oa->o_oi = lsm->lsm_oi;
897         oa->o_mode = body->mode & S_IFMT;
898         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
899
900         if (body->valid & OBD_MD_FLCOOKIE) {
901                 oa->o_valid |= OBD_MD_FLCOOKIE;
902                 oti.oti_logcookies =
903                         req_capsule_server_sized_get(&request->rq_pill,
904                                                      &RMF_LOGCOOKIES,
905                                                    sizeof(struct llog_cookie) *
906                                                      lsm->lsm_stripe_count);
907                 if (!oti.oti_logcookies) {
908                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
909                         body->valid &= ~OBD_MD_FLCOOKIE;
910                 }
911         }
912
913         rc = obd_destroy(NULL, ll_i2dtexp(dir), oa, lsm, &oti,
914                          ll_i2mdexp(dir));
915         if (rc)
916                 CERROR("obd destroy objid "DOSTID" error %d\n",
917                        POSTID(&lsm->lsm_oi), rc);
918 out_free_memmd:
919         obd_free_memmd(ll_i2dtexp(dir), &lsm);
920         kmem_cache_free(obdo_cachep, oa);
921 out:
922         return rc;
923 }
924
925 /* ll_unlink() doesn't update the inode with the new link count.
926  * Instead, ll_ddelete() and ll_d_iput() will update it based upon if there
927  * is any lock existing. They will recycle dentries and inodes based upon locks
928  * too. b=20433
929  */
930 static int ll_unlink(struct inode *dir, struct dentry *dentry)
931 {
932         struct ptlrpc_request *request = NULL;
933         struct md_op_data *op_data;
934         int rc;
935
936         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
937                dentry, dir->i_ino, dir->i_generation, dir);
938
939         op_data = ll_prep_md_op_data(NULL, dir, NULL,
940                                      dentry->d_name.name,
941                                      dentry->d_name.len,
942                                      0, LUSTRE_OPC_ANY, NULL);
943         if (IS_ERR(op_data))
944                 return PTR_ERR(op_data);
945
946         ll_get_child_fid(dentry, &op_data->op_fid3);
947         op_data->op_fid2 = op_data->op_fid3;
948         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
949         ll_finish_md_op_data(op_data);
950         if (rc)
951                 goto out;
952
953         ll_update_times(request, dir);
954         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
955
956         rc = ll_objects_destroy(request, dir);
957  out:
958         ptlrpc_req_finished(request);
959         return rc;
960 }
961
962 static int ll_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
963 {
964         int err;
965
966         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
967                dentry, dir->i_ino, dir->i_generation, dir);
968
969         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
970                 mode &= ~current_umask();
971         mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
972         err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
973
974         if (!err)
975                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
976
977         return err;
978 }
979
980 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
981 {
982         struct ptlrpc_request *request = NULL;
983         struct md_op_data *op_data;
984         int rc;
985
986         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
987                dentry, dir->i_ino, dir->i_generation, dir);
988
989         op_data = ll_prep_md_op_data(NULL, dir, NULL,
990                                      dentry->d_name.name,
991                                      dentry->d_name.len,
992                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
993         if (IS_ERR(op_data))
994                 return PTR_ERR(op_data);
995
996         ll_get_child_fid(dentry, &op_data->op_fid3);
997         op_data->op_fid2 = op_data->op_fid3;
998         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
999         ll_finish_md_op_data(op_data);
1000         if (rc == 0) {
1001                 ll_update_times(request, dir);
1002                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1003         }
1004
1005         ptlrpc_req_finished(request);
1006         return rc;
1007 }
1008
1009 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1010                       const char *oldname)
1011 {
1012         int err;
1013
1014         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),target=%.*s\n",
1015                dentry, dir->i_ino, dir->i_generation,
1016                dir, 3000, oldname);
1017
1018         err = ll_new_node(dir, dentry, oldname, S_IFLNK | S_IRWXUGO,
1019                         0, LUSTRE_OPC_SYMLINK);
1020
1021         if (!err)
1022                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1023
1024         return err;
1025 }
1026
1027 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1028                    struct dentry *new_dentry)
1029 {
1030         struct inode *src = d_inode(old_dentry);
1031         struct ll_sb_info *sbi = ll_i2sbi(dir);
1032         struct ptlrpc_request *request = NULL;
1033         struct md_op_data *op_data;
1034         int err;
1035
1036         CDEBUG(D_VFSTRACE,
1037                "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%pd\n",
1038                src->i_ino, src->i_generation, src, dir->i_ino,
1039                dir->i_generation, dir, new_dentry);
1040
1041         op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1042                                      new_dentry->d_name.len,
1043                                      0, LUSTRE_OPC_ANY, NULL);
1044         if (IS_ERR(op_data))
1045                 return PTR_ERR(op_data);
1046
1047         err = md_link(sbi->ll_md_exp, op_data, &request);
1048         ll_finish_md_op_data(op_data);
1049         if (err)
1050                 goto out;
1051
1052         ll_update_times(request, dir);
1053         ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1054 out:
1055         ptlrpc_req_finished(request);
1056         return err;
1057 }
1058
1059 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1060                      struct inode *new_dir, struct dentry *new_dentry)
1061 {
1062         struct ptlrpc_request *request = NULL;
1063         struct ll_sb_info *sbi = ll_i2sbi(old_dir);
1064         struct md_op_data *op_data;
1065         int err;
1066
1067         CDEBUG(D_VFSTRACE,
1068                "VFS Op:oldname=%pd,src_dir=%lu/%u(%p),newname=%pd,tgt_dir=%lu/%u(%p)\n",
1069                old_dentry, old_dir->i_ino, old_dir->i_generation, old_dir,
1070                new_dentry, new_dir->i_ino, new_dir->i_generation, new_dir);
1071
1072         op_data = ll_prep_md_op_data(NULL, old_dir, new_dir, NULL, 0, 0,
1073                                      LUSTRE_OPC_ANY, NULL);
1074         if (IS_ERR(op_data))
1075                 return PTR_ERR(op_data);
1076
1077         ll_get_child_fid(old_dentry, &op_data->op_fid3);
1078         ll_get_child_fid(new_dentry, &op_data->op_fid4);
1079         err = md_rename(sbi->ll_md_exp, op_data,
1080                         old_dentry->d_name.name,
1081                         old_dentry->d_name.len,
1082                         new_dentry->d_name.name,
1083                         new_dentry->d_name.len, &request);
1084         ll_finish_md_op_data(op_data);
1085         if (!err) {
1086                 ll_update_times(request, old_dir);
1087                 ll_update_times(request, new_dir);
1088                 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1089                 err = ll_objects_destroy(request, old_dir);
1090         }
1091
1092         ptlrpc_req_finished(request);
1093         if (!err)
1094                 d_move(old_dentry, new_dentry);
1095         return err;
1096 }
1097
1098 const struct inode_operations ll_dir_inode_operations = {
1099         .mknod        = ll_mknod,
1100         .atomic_open        = ll_atomic_open,
1101         .lookup      = ll_lookup_nd,
1102         .create      = ll_create_nd,
1103         /* We need all these non-raw things for NFSD, to not patch it. */
1104         .unlink      = ll_unlink,
1105         .mkdir        = ll_mkdir,
1106         .rmdir        = ll_rmdir,
1107         .symlink            = ll_symlink,
1108         .link          = ll_link,
1109         .rename      = ll_rename,
1110         .setattr            = ll_setattr,
1111         .getattr            = ll_getattr,
1112         .permission      = ll_inode_permission,
1113         .setxattr          = ll_setxattr,
1114         .getxattr          = ll_getxattr,
1115         .listxattr        = ll_listxattr,
1116         .removexattr    = ll_removexattr,
1117         .get_acl            = ll_get_acl,
1118 };
1119
1120 const struct inode_operations ll_special_inode_operations = {
1121         .setattr        = ll_setattr,
1122         .getattr        = ll_getattr,
1123         .permission     = ll_inode_permission,
1124         .setxattr       = ll_setxattr,
1125         .getxattr       = ll_getxattr,
1126         .listxattr      = ll_listxattr,
1127         .removexattr    = ll_removexattr,
1128         .get_acl            = ll_get_acl,
1129 };