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