Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / lustre / lustre / llite / namei.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/quotaops.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
43 #include <linux/security.h>
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include "../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 /*
105  * Get an inode by inode number (already instantiated by the intent lookup).
106  * Returns inode or NULL
107  */
108 struct inode *ll_iget(struct super_block *sb, ino_t hash,
109                       struct lustre_md *md)
110 {
111         struct inode     *inode;
112
113         LASSERT(hash != 0);
114         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
115
116         if (inode) {
117                 if (inode->i_state & I_NEW) {
118                         int rc = 0;
119
120                         ll_read_inode2(inode, md);
121                         if (S_ISREG(inode->i_mode) &&
122                             ll_i2info(inode)->lli_clob == NULL) {
123                                 CDEBUG(D_INODE,
124                                         "%s: apply lsm %p to inode "DFID".\n",
125                                         ll_get_fsname(sb, NULL, 0), md->lsm,
126                                         PFID(ll_inode2fid(inode)));
127                                 rc = cl_file_inode_init(inode, md);
128                         }
129                         if (rc != 0) {
130                                 make_bad_inode(inode);
131                                 unlock_new_inode(inode);
132                                 iput(inode);
133                                 inode = ERR_PTR(rc);
134                         } else
135                                 unlock_new_inode(inode);
136                 } else if (!(inode->i_state & (I_FREEING | I_CLEAR)))
137                         ll_update_inode(inode, md);
138                 CDEBUG(D_VFSTRACE, "got inode: %p for "DFID"\n",
139                        inode, PFID(&md->body->fid1));
140         }
141         return inode;
142 }
143
144 static void ll_invalidate_negative_children(struct inode *dir)
145 {
146         struct dentry *dentry, *tmp_subdir;
147         struct ll_d_hlist_node *p;
148
149         ll_lock_dcache(dir);
150         ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry, d_u.d_alias) {
151                 spin_lock(&dentry->d_lock);
152                 if (!list_empty(&dentry->d_subdirs)) {
153                         struct dentry *child;
154
155                         list_for_each_entry_safe(child, tmp_subdir,
156                                                  &dentry->d_subdirs,
157                                                  d_child) {
158                                 if (d_really_is_negative(child))
159                                         d_lustre_invalidate(child, 1);
160                         }
161                 }
162                 spin_unlock(&dentry->d_lock);
163         }
164         ll_unlock_dcache(dir);
165 }
166
167 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
168                        void *data, int flag)
169 {
170         struct lustre_handle lockh;
171         int rc;
172
173         switch (flag) {
174         case LDLM_CB_BLOCKING:
175                 ldlm_lock2handle(lock, &lockh);
176                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
177                 if (rc < 0) {
178                         CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
179                         return rc;
180                 }
181                 break;
182         case LDLM_CB_CANCELING: {
183                 struct inode *inode = ll_inode_from_resource_lock(lock);
184                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
185
186                 /* Inode is set to lock->l_resource->lr_lvb_inode
187                  * for mdc - bug 24555 */
188                 LASSERT(lock->l_ast_data == NULL);
189
190                 if (inode == NULL)
191                         break;
192
193                 /* Invalidate all dentries associated with this inode */
194                 LASSERT(lock->l_flags & LDLM_FL_CANCELING);
195
196                 if (!fid_res_name_eq(ll_inode2fid(inode),
197                                      &lock->l_resource->lr_name)) {
198                         LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
199                                    PFID(ll_inode2fid(inode)), inode);
200                         LBUG();
201                 }
202
203                 if (bits & MDS_INODELOCK_XATTR) {
204                         ll_xattr_cache_destroy(inode);
205                         bits &= ~MDS_INODELOCK_XATTR;
206                 }
207
208                 /* For OPEN locks we differentiate between lock modes
209                  * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
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 %lu\n",
261                                inode->i_ino);
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 != NULL &&
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 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
294 {
295 #if 0
296         int i;
297 #endif
298
299         LASSERT(i1 != NULL);
300         LASSERT(suppgids != NULL);
301
302         suppgids[0] = ll_i2suppgid(i1);
303
304         if (i2)
305                 suppgids[1] = ll_i2suppgid(i2);
306                 else
307                         suppgids[1] = -1;
308
309 #if 0
310         for (i = 0; i < current_ngroups; i++) {
311                 if (suppgids[0] == -1) {
312                         if (current_groups[i] != suppgids[1])
313                                 suppgids[0] = current_groups[i];
314                         continue;
315                 }
316                 if (suppgids[1] == -1) {
317                         if (current_groups[i] != suppgids[0])
318                                 suppgids[1] = current_groups[i];
319                         continue;
320                 }
321                 break;
322         }
323 #endif
324 }
325
326 /*
327  * try to reuse three types of dentry:
328  * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
329  *    by concurrent .revalidate).
330  * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
331  *    be cleared by others calling d_lustre_revalidate).
332  * 3. DISCONNECTED alias.
333  */
334 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
335 {
336         struct dentry *alias, *discon_alias, *invalid_alias;
337         struct ll_d_hlist_node *p;
338
339         if (ll_d_hlist_empty(&inode->i_dentry))
340                 return NULL;
341
342         discon_alias = invalid_alias = NULL;
343
344         ll_lock_dcache(inode);
345         ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry, d_u.d_alias) {
346                 LASSERT(alias != dentry);
347
348                 spin_lock(&alias->d_lock);
349                 if (alias->d_flags & DCACHE_DISCONNECTED)
350                         /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
351                         discon_alias = alias;
352                 else if (alias->d_parent == dentry->d_parent         &&
353                          alias->d_name.hash == dentry->d_name.hash       &&
354                          alias->d_name.len == dentry->d_name.len         &&
355                          memcmp(alias->d_name.name, dentry->d_name.name,
356                                 dentry->d_name.len) == 0)
357                         invalid_alias = alias;
358                 spin_unlock(&alias->d_lock);
359
360                 if (invalid_alias)
361                         break;
362         }
363         alias = invalid_alias ?: discon_alias ?: NULL;
364         if (alias) {
365                 spin_lock(&alias->d_lock);
366                 dget_dlock(alias);
367                 spin_unlock(&alias->d_lock);
368         }
369         ll_unlock_dcache(inode);
370
371         return alias;
372 }
373
374 /*
375  * Similar to d_splice_alias(), but lustre treats invalid alias
376  * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
377  */
378 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
379 {
380         struct dentry *new;
381         int rc;
382
383         if (inode) {
384                 new = ll_find_alias(inode, de);
385                 if (new) {
386                         rc = ll_d_init(new);
387                         if (rc < 0) {
388                                 dput(new);
389                                 return ERR_PTR(rc);
390                         }
391                         d_move(new, de);
392                         iput(inode);
393                         CDEBUG(D_DENTRY,
394                                "Reuse dentry %p inode %p refc %d flags %#x\n",
395                               new, d_inode(new), d_count(new), new->d_flags);
396                         return new;
397                 }
398         }
399         rc = ll_d_init(de);
400         if (rc < 0)
401                 return ERR_PTR(rc);
402         d_add(de, inode);
403         CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
404                de, d_inode(de), d_count(de), de->d_flags);
405         return de;
406 }
407
408 static int ll_lookup_it_finish(struct ptlrpc_request *request,
409                                struct lookup_intent *it,
410                                struct inode *parent, struct dentry **de)
411 {
412         struct inode *inode = NULL;
413         __u64 bits = 0;
414         int rc;
415
416         /* NB 1 request reference will be taken away by ll_intent_lock()
417          * when I return */
418         CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
419                it->d.lustre.it_disposition);
420         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
421                 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
422                 if (rc)
423                         return rc;
424
425                 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
426
427                 /* We used to query real size from OSTs here, but actually
428                    this is not needed. For stat() calls size would be updated
429                    from subsequent do_revalidate()->ll_inode_revalidate_it() in
430                    2.4 and
431                    vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
432                    Everybody else who needs correct file size would call
433                    ll_glimpse_size or some equivalent themselves anyway.
434                    Also see bug 7198. */
435         }
436
437         /* Only hash *de if it is unhashed (new dentry).
438          * Atoimc_open may passing hashed dentries for open.
439          */
440         if (d_unhashed(*de)) {
441                 struct dentry *alias;
442
443                 alias = ll_splice_alias(inode, *de);
444                 if (IS_ERR(alias))
445                         return PTR_ERR(alias);
446                 *de = alias;
447         } else if (!it_disposition(it, DISP_LOOKUP_NEG)  &&
448                    !it_disposition(it, DISP_OPEN_CREATE)) {
449                 /* With DISP_OPEN_CREATE dentry will
450                    instantiated in ll_create_it. */
451                 LASSERT(d_inode(*de) == NULL);
452                 d_instantiate(*de, inode);
453         }
454
455         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
456                 /* we have lookup look - unhide dentry */
457                 if (bits & MDS_INODELOCK_LOOKUP)
458                         d_lustre_revalidate(*de);
459         } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
460                 /* If file created on server, don't depend on parent UPDATE
461                  * lock to unhide it. It is left hidden and next lookup can
462                  * find it in ll_splice_alias.
463                  */
464                 /* Check that parent has UPDATE lock. */
465                 struct lookup_intent parent_it = {
466                                         .it_op = IT_GETATTR,
467                                         .d.lustre.it_lock_handle = 0 };
468
469                 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it,
470                                        &ll_i2info(parent)->lli_fid, NULL)) {
471                         d_lustre_revalidate(*de);
472                         ll_intent_release(&parent_it);
473                 }
474         }
475
476         return 0;
477 }
478
479 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
480                                    struct lookup_intent *it, int lookup_flags)
481 {
482         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
483         struct dentry *save = dentry, *retval;
484         struct ptlrpc_request *req = NULL;
485         struct inode *inode;
486         struct md_op_data *op_data;
487         __u32 opc;
488         int rc;
489
490         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
491                 return ERR_PTR(-ENAMETOOLONG);
492
493         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),intent=%s\n",
494                dentry, parent->i_ino,
495                parent->i_generation, parent, LL_IT2STR(it));
496
497         if (d_mountpoint(dentry))
498                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
499
500         if (it == NULL || it->it_op == IT_GETXATTR)
501                 it = &lookup_it;
502
503         if (it->it_op == IT_GETATTR) {
504                 rc = ll_statahead_enter(parent, &dentry, 0);
505                 if (rc == 1) {
506                         if (dentry == save)
507                                 retval = NULL;
508                         else
509                                 retval = dentry;
510                         goto out;
511                 }
512         }
513
514         if (it->it_op & IT_CREAT)
515                 opc = LUSTRE_OPC_CREATE;
516         else
517                 opc = LUSTRE_OPC_ANY;
518
519         op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
520                                      dentry->d_name.len, lookup_flags, opc,
521                                      NULL);
522         if (IS_ERR(op_data))
523                 return (void *)op_data;
524
525         /* enforce umask if acl disabled or MDS doesn't support umask */
526         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
527                 it->it_create_mode &= ~current_umask();
528
529         rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
530                             lookup_flags, &req, ll_md_blocking_ast, 0);
531         ll_finish_md_op_data(op_data);
532         if (rc < 0) {
533                 retval = ERR_PTR(rc);
534                 goto out;
535         }
536
537         rc = ll_lookup_it_finish(req, it, parent, &dentry);
538         if (rc != 0) {
539                 ll_intent_release(it);
540                 retval = ERR_PTR(rc);
541                 goto out;
542         }
543
544         inode = d_inode(dentry);
545         if ((it->it_op & IT_OPEN) && inode &&
546             !S_ISREG(inode->i_mode) &&
547             !S_ISDIR(inode->i_mode)) {
548                 ll_release_openhandle(inode, it);
549         }
550         ll_lookup_finish_locks(it, inode);
551
552         if (dentry == save)
553                 retval = NULL;
554         else
555                 retval = dentry;
556         goto out;
557  out:
558         if (req)
559                 ptlrpc_req_finished(req);
560         if (it->it_op == IT_GETATTR && (retval == NULL || retval == dentry))
561                 ll_statahead_mark(parent, dentry);
562         return retval;
563 }
564
565 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
566                                    unsigned int flags)
567 {
568         struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
569         struct dentry *de;
570
571         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),flags=%u\n",
572                dentry, parent->i_ino,
573                parent->i_generation, parent, flags);
574
575         /* Optimize away (CREATE && !OPEN). Let .create handle the race. */
576         if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
577                 return NULL;
578
579         if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
580                 itp = NULL;
581         else
582                 itp = &it;
583         de = ll_lookup_it(parent, dentry, itp, 0);
584
585         if (itp != NULL)
586                 ll_intent_release(itp);
587
588         return de;
589 }
590
591 /*
592  * For cached negative dentry and new dentry, handle lookup/create/open
593  * together.
594  */
595 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
596                           struct file *file, unsigned open_flags,
597                           umode_t mode, int *opened)
598 {
599         struct lookup_intent *it;
600         struct dentry *de;
601         long long lookup_flags = LOOKUP_OPEN;
602         int rc = 0;
603
604         CDEBUG(D_VFSTRACE,
605                "VFS Op:name=%pd,dir=%lu/%u(%p),file %p,open_flags %x,mode %x opened %d\n",
606                dentry, dir->i_ino,
607                dir->i_generation, dir, file, open_flags, mode, *opened);
608
609         it = kzalloc(sizeof(*it), GFP_NOFS);
610         if (!it)
611                 return -ENOMEM;
612
613         it->it_op = IT_OPEN;
614         if (open_flags & O_CREAT) {
615                 it->it_op |= IT_CREAT;
616                 lookup_flags |= LOOKUP_CREATE;
617         }
618         it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
619         it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
620
621         /* Dentry added to dcache tree in ll_lookup_it */
622         de = ll_lookup_it(dir, dentry, it, lookup_flags);
623         if (IS_ERR(de))
624                 rc = PTR_ERR(de);
625         else if (de != NULL)
626                 dentry = de;
627
628         if (!rc) {
629                 if (it_disposition(it, DISP_OPEN_CREATE)) {
630                         /* Dentry instantiated in ll_create_it. */
631                         rc = ll_create_it(dir, dentry, mode, it);
632                         if (rc) {
633                                 /* We dget in ll_splice_alias. */
634                                 if (de != NULL)
635                                         dput(de);
636                                 goto out_release;
637                         }
638
639                         *opened |= FILE_CREATED;
640                 }
641                 if (d_really_is_positive(dentry) && it_disposition(it, DISP_OPEN_OPEN)) {
642                         /* Open dentry. */
643                         if (S_ISFIFO(d_inode(dentry)->i_mode)) {
644                                 /* We cannot call open here as it would
645                                  * deadlock.
646                                  */
647                                 if (it_disposition(it, DISP_ENQ_OPEN_REF))
648                                         ptlrpc_req_finished(
649                                                        (struct ptlrpc_request *)
650                                                           it->d.lustre.it_data);
651                                 rc = finish_no_open(file, de);
652                         } else {
653                                 file->private_data = it;
654                                 rc = finish_open(file, dentry, NULL, opened);
655                                 /* We dget in ll_splice_alias. finish_open takes
656                                  * care of dget for fd open.
657                                  */
658                                 if (de != NULL)
659                                         dput(de);
660                         }
661                 } else {
662                         rc = finish_no_open(file, de);
663                 }
664         }
665
666 out_release:
667         ll_intent_release(it);
668         OBD_FREE(it, sizeof(*it));
669
670         return rc;
671 }
672
673
674 /* We depend on "mode" being set with the proper file type/umask by now */
675 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
676 {
677         struct inode *inode = NULL;
678         struct ptlrpc_request *request = NULL;
679         struct ll_sb_info *sbi = ll_i2sbi(dir);
680         int rc;
681
682         LASSERT(it && it->d.lustre.it_disposition);
683
684         LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
685         request = it->d.lustre.it_data;
686         it_clear_disposition(it, DISP_ENQ_CREATE_REF);
687         rc = ll_prep_inode(&inode, request, dir->i_sb, it);
688         if (rc) {
689                 inode = ERR_PTR(rc);
690                 goto out;
691         }
692
693         LASSERT(ll_d_hlist_empty(&inode->i_dentry));
694
695         /* We asked for a lock on the directory, but were granted a
696          * lock on the inode.  Since we finally have an inode pointer,
697          * stuff it in the lock. */
698         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
699                inode, inode->i_ino, inode->i_generation);
700         ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
701  out:
702         ptlrpc_req_finished(request);
703         return inode;
704 }
705
706 /*
707  * By the time this is called, we already have created the directory cache
708  * entry for the new file, but it is so far negative - it has no inode.
709  *
710  * We defer creating the OBD object(s) until open, to keep the intent and
711  * non-intent code paths similar, and also because we do not have the MDS
712  * inode number before calling ll_create_node() (which is needed for LOV),
713  * so we would need to do yet another RPC to the MDS to store the LOV EA
714  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
715  * lmm_size in datalen (the MDS still has code which will handle that).
716  *
717  * If the create succeeds, we fill in the inode information
718  * with d_instantiate().
719  */
720 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
721                         struct lookup_intent *it)
722 {
723         struct inode *inode;
724         int rc = 0;
725
726         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),intent=%s\n",
727                dentry, dir->i_ino,
728                dir->i_generation, dir, LL_IT2STR(it));
729
730         rc = it_open_error(DISP_OPEN_CREATE, it);
731         if (rc)
732                 return rc;
733
734         inode = ll_create_node(dir, it);
735         if (IS_ERR(inode))
736                 return PTR_ERR(inode);
737
738         d_instantiate(dentry, inode);
739         return 0;
740 }
741
742 static void ll_update_times(struct ptlrpc_request *request,
743                             struct inode *inode)
744 {
745         struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
746                                                        &RMF_MDT_BODY);
747
748         LASSERT(body);
749         if (body->valid & OBD_MD_FLMTIME &&
750             body->mtime > LTIME_S(inode->i_mtime)) {
751                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
752                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
753                 LTIME_S(inode->i_mtime) = body->mtime;
754         }
755         if (body->valid & OBD_MD_FLCTIME &&
756             body->ctime > LTIME_S(inode->i_ctime))
757                 LTIME_S(inode->i_ctime) = body->ctime;
758 }
759
760 static int ll_new_node(struct inode *dir, struct dentry *dentry,
761                        const char *tgt, int mode, int rdev,
762                        __u32 opc)
763 {
764         struct ptlrpc_request *request = NULL;
765         struct md_op_data *op_data;
766         struct inode *inode = NULL;
767         struct ll_sb_info *sbi = ll_i2sbi(dir);
768         int tgt_len = 0;
769         int err;
770
771         if (unlikely(tgt != NULL))
772                 tgt_len = strlen(tgt) + 1;
773
774         op_data = ll_prep_md_op_data(NULL, dir, NULL,
775                                      dentry->d_name.name,
776                                      dentry->d_name.len,
777                                      0, opc, NULL);
778         if (IS_ERR(op_data)) {
779                 err = PTR_ERR(op_data);
780                 goto err_exit;
781         }
782
783         err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
784                         from_kuid(&init_user_ns, current_fsuid()),
785                         from_kgid(&init_user_ns, current_fsgid()),
786                         cfs_curproc_cap_pack(), rdev, &request);
787         ll_finish_md_op_data(op_data);
788         if (err)
789                 goto err_exit;
790
791         ll_update_times(request, dir);
792
793         err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
794         if (err)
795                 goto err_exit;
796
797         d_instantiate(dentry, inode);
798 err_exit:
799         ptlrpc_req_finished(request);
800
801         return err;
802 }
803
804 static int ll_mknod(struct inode *dir, struct dentry *dchild,
805                     umode_t mode, dev_t rdev)
806 {
807         int err;
808
809         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p) mode %o dev %x\n",
810                dchild, dir->i_ino, dir->i_generation, dir,
811                mode, old_encode_dev(rdev));
812
813         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
814                 mode &= ~current_umask();
815
816         switch (mode & S_IFMT) {
817         case 0:
818                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
819         case S_IFREG:
820         case S_IFCHR:
821         case S_IFBLK:
822         case S_IFIFO:
823         case S_IFSOCK:
824                 err = ll_new_node(dir, dchild, NULL, mode,
825                                   old_encode_dev(rdev),
826                                   LUSTRE_OPC_MKNOD);
827                 break;
828         case S_IFDIR:
829                 err = -EPERM;
830                 break;
831         default:
832                 err = -EINVAL;
833         }
834
835         if (!err)
836                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
837
838         return err;
839 }
840
841 /*
842  * Plain create. Intent create is handled in atomic_open.
843  */
844 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
845                         umode_t mode, bool want_excl)
846 {
847         int rc;
848
849         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),flags=%u, excl=%d\n",
850                dentry, dir->i_ino,
851                dir->i_generation, dir, mode, want_excl);
852
853         rc = ll_mknod(dir, dentry, mode, 0);
854
855         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
856
857         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
858                dentry, d_unhashed(dentry));
859
860         return rc;
861 }
862
863 static inline void ll_get_child_fid(struct dentry *child, struct lu_fid *fid)
864 {
865         if (d_really_is_positive(child))
866                 *fid = *ll_inode2fid(d_inode(child));
867 }
868
869 /**
870  * Remove dir entry
871  **/
872 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
873 {
874         struct ptlrpc_request *request = NULL;
875         struct md_op_data *op_data;
876         int rc;
877
878         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
879                namelen, name, dir->i_ino, dir->i_generation, dir);
880
881         op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
882                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
883         if (IS_ERR(op_data))
884                 return PTR_ERR(op_data);
885         op_data->op_cli_flags |= CLI_RM_ENTRY;
886         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
887         ll_finish_md_op_data(op_data);
888         if (rc == 0) {
889                 ll_update_times(request, dir);
890                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
891         }
892
893         ptlrpc_req_finished(request);
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         struct obd_capa *oc = NULL;
905         int rc;
906
907         /* req is swabbed so this is safe */
908         body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
909         if (!(body->valid & OBD_MD_FLEASIZE))
910                 return 0;
911
912         if (body->eadatasize == 0) {
913                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
914                 rc = -EPROTO;
915                 goto out;
916         }
917
918         /* The MDS sent back the EA because we unlinked the last reference
919          * to this file. Use this EA to unlink the objects on the OST.
920          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
921          * check it is complete and sensible. */
922         eadata = req_capsule_server_sized_get(&request->rq_pill, &RMF_MDT_MD,
923                                               body->eadatasize);
924         LASSERT(eadata != NULL);
925
926         rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
927         if (rc < 0) {
928                 CERROR("obd_unpackmd: %d\n", rc);
929                 goto out;
930         }
931         LASSERT(rc >= sizeof(*lsm));
932
933         OBDO_ALLOC(oa);
934         if (oa == NULL) {
935                 rc = -ENOMEM;
936                 goto out_free_memmd;
937         }
938
939         oa->o_oi = lsm->lsm_oi;
940         oa->o_mode = body->mode & S_IFMT;
941         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
942
943         if (body->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 == NULL) {
951                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
952                         body->valid &= ~OBD_MD_FLCOOKIE;
953                 }
954         }
955
956         if (body->valid & OBD_MD_FLOSSCAPA) {
957                 rc = md_unpack_capa(ll_i2mdexp(dir), request, &RMF_CAPA2, &oc);
958                 if (rc)
959                         goto out_free_memmd;
960         }
961
962         rc = obd_destroy(NULL, ll_i2dtexp(dir), oa, lsm, &oti,
963                          ll_i2mdexp(dir), oc);
964         capa_put(oc);
965         if (rc)
966                 CERROR("obd destroy objid "DOSTID" error %d\n",
967                        POSTID(&lsm->lsm_oi), rc);
968 out_free_memmd:
969         obd_free_memmd(ll_i2dtexp(dir), &lsm);
970         OBDO_FREE(oa);
971 out:
972         return rc;
973 }
974
975 /* ll_unlink() doesn't update the inode with the new link count.
976  * Instead, ll_ddelete() and ll_d_iput() will update it based upon if there
977  * is any lock existing. They will recycle dentries and inodes based upon locks
978  * too. b=20433 */
979 static int ll_unlink(struct inode *dir, struct dentry *dentry)
980 {
981         struct ptlrpc_request *request = NULL;
982         struct md_op_data *op_data;
983         int rc;
984
985         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
986                dentry, dir->i_ino, dir->i_generation, dir);
987
988         op_data = ll_prep_md_op_data(NULL, dir, NULL,
989                                      dentry->d_name.name,
990                                      dentry->d_name.len,
991                                      0, LUSTRE_OPC_ANY, NULL);
992         if (IS_ERR(op_data))
993                 return PTR_ERR(op_data);
994
995         ll_get_child_fid(dentry, &op_data->op_fid3);
996         op_data->op_fid2 = op_data->op_fid3;
997         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
998         ll_finish_md_op_data(op_data);
999         if (rc)
1000                 goto out;
1001
1002         ll_update_times(request, dir);
1003         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1004
1005         rc = ll_objects_destroy(request, dir);
1006  out:
1007         ptlrpc_req_finished(request);
1008         return rc;
1009 }
1010
1011 static int ll_mkdir(struct inode *dir, struct dentry *dentry, ll_umode_t mode)
1012 {
1013         int err;
1014
1015         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
1016                dentry, dir->i_ino, dir->i_generation, dir);
1017
1018         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1019                 mode &= ~current_umask();
1020         mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1021         err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1022
1023         if (!err)
1024                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1025
1026         return err;
1027 }
1028
1029 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1030 {
1031         struct ptlrpc_request *request = NULL;
1032         struct md_op_data *op_data;
1033         int rc;
1034
1035         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
1036                dentry, dir->i_ino, dir->i_generation, dir);
1037
1038         op_data = ll_prep_md_op_data(NULL, dir, NULL,
1039                                      dentry->d_name.name,
1040                                      dentry->d_name.len,
1041                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
1042         if (IS_ERR(op_data))
1043                 return PTR_ERR(op_data);
1044
1045         ll_get_child_fid(dentry, &op_data->op_fid3);
1046         op_data->op_fid2 = op_data->op_fid3;
1047         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1048         ll_finish_md_op_data(op_data);
1049         if (rc == 0) {
1050                 ll_update_times(request, dir);
1051                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1052         }
1053
1054         ptlrpc_req_finished(request);
1055         return rc;
1056 }
1057
1058 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1059                       const char *oldname)
1060 {
1061         int err;
1062
1063         CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),target=%.*s\n",
1064                dentry, dir->i_ino, dir->i_generation,
1065                dir, 3000, oldname);
1066
1067         err = ll_new_node(dir, dentry, oldname, S_IFLNK | S_IRWXUGO,
1068                         0, LUSTRE_OPC_SYMLINK);
1069
1070         if (!err)
1071                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1072
1073         return err;
1074 }
1075
1076 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1077                    struct dentry *new_dentry)
1078 {
1079         struct inode *src = d_inode(old_dentry);
1080         struct ll_sb_info *sbi = ll_i2sbi(dir);
1081         struct ptlrpc_request *request = NULL;
1082         struct md_op_data *op_data;
1083         int err;
1084
1085         CDEBUG(D_VFSTRACE,
1086                "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%pd\n",
1087                src->i_ino, src->i_generation, src, dir->i_ino,
1088                dir->i_generation, dir, new_dentry);
1089
1090         op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1091                                      new_dentry->d_name.len,
1092                                      0, LUSTRE_OPC_ANY, NULL);
1093         if (IS_ERR(op_data))
1094                 return PTR_ERR(op_data);
1095
1096         err = md_link(sbi->ll_md_exp, op_data, &request);
1097         ll_finish_md_op_data(op_data);
1098         if (err)
1099                 goto out;
1100
1101         ll_update_times(request, dir);
1102         ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1103 out:
1104         ptlrpc_req_finished(request);
1105         return err;
1106 }
1107
1108 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1109                      struct inode *new_dir, struct dentry *new_dentry)
1110 {
1111         struct ptlrpc_request *request = NULL;
1112         struct ll_sb_info *sbi = ll_i2sbi(old_dir);
1113         struct md_op_data *op_data;
1114         int err;
1115
1116         CDEBUG(D_VFSTRACE,
1117                "VFS Op:oldname=%pd,src_dir=%lu/%u(%p),newname=%pd,tgt_dir=%lu/%u(%p)\n",
1118                old_dentry, old_dir->i_ino, old_dir->i_generation, old_dir,
1119                new_dentry, new_dir->i_ino, new_dir->i_generation, new_dir);
1120
1121         op_data = ll_prep_md_op_data(NULL, old_dir, new_dir, NULL, 0, 0,
1122                                      LUSTRE_OPC_ANY, NULL);
1123         if (IS_ERR(op_data))
1124                 return PTR_ERR(op_data);
1125
1126         ll_get_child_fid(old_dentry, &op_data->op_fid3);
1127         ll_get_child_fid(new_dentry, &op_data->op_fid4);
1128         err = md_rename(sbi->ll_md_exp, op_data,
1129                         old_dentry->d_name.name,
1130                         old_dentry->d_name.len,
1131                         new_dentry->d_name.name,
1132                         new_dentry->d_name.len, &request);
1133         ll_finish_md_op_data(op_data);
1134         if (!err) {
1135                 ll_update_times(request, old_dir);
1136                 ll_update_times(request, new_dir);
1137                 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1138                 err = ll_objects_destroy(request, old_dir);
1139         }
1140
1141         ptlrpc_req_finished(request);
1142         if (!err)
1143                 d_move(old_dentry, new_dentry);
1144         return err;
1145 }
1146
1147 const struct inode_operations ll_dir_inode_operations = {
1148         .mknod        = ll_mknod,
1149         .atomic_open        = ll_atomic_open,
1150         .lookup      = ll_lookup_nd,
1151         .create      = ll_create_nd,
1152         /* We need all these non-raw things for NFSD, to not patch it. */
1153         .unlink      = ll_unlink,
1154         .mkdir        = ll_mkdir,
1155         .rmdir        = ll_rmdir,
1156         .symlink            = ll_symlink,
1157         .link          = ll_link,
1158         .rename      = ll_rename,
1159         .setattr            = ll_setattr,
1160         .getattr            = ll_getattr,
1161         .permission      = ll_inode_permission,
1162         .setxattr          = ll_setxattr,
1163         .getxattr          = ll_getxattr,
1164         .listxattr        = ll_listxattr,
1165         .removexattr    = ll_removexattr,
1166         .get_acl            = ll_get_acl,
1167 };
1168
1169 const struct inode_operations ll_special_inode_operations = {
1170         .setattr        = ll_setattr,
1171         .getattr        = ll_getattr,
1172         .permission     = ll_inode_permission,
1173         .setxattr       = ll_setxattr,
1174         .getxattr       = ll_getxattr,
1175         .listxattr      = ll_listxattr,
1176         .removexattr    = ll_removexattr,
1177         .get_acl            = ll_get_acl,
1178 };