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