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