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