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