Whamcloud - gitweb
LU-3285 mds: combine DoM bit with other IBITS
[fs/lustre-release.git] / 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, 2016, 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 #include <linux/user_namespace.h>
41 #ifdef HAVE_UIDGID_HEADER
42 # include <linux/uidgid.h>
43 #endif
44
45 #define DEBUG_SUBSYSTEM S_LLITE
46
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <lustre_dlm.h>
50 #include "llite_internal.h"
51
52 static int ll_create_it(struct inode *dir, struct dentry *dentry,
53                         struct lookup_intent *it,
54                         void *secctx, __u32 secctxlen);
55
56 /* called from iget5_locked->find_inode() under inode_lock spinlock */
57 static int ll_test_inode(struct inode *inode, void *opaque)
58 {
59         struct ll_inode_info    *lli = ll_i2info(inode);
60         struct lustre_md        *md = opaque;
61
62         if (unlikely(!(md->body->mbo_valid & OBD_MD_FLID))) {
63                 CERROR("MDS body missing FID\n");
64                 return 0;
65         }
66
67         if (!lu_fid_eq(&lli->lli_fid, &md->body->mbo_fid1))
68                 return 0;
69
70         return 1;
71 }
72
73 static int ll_set_inode(struct inode *inode, void *opaque)
74 {
75         struct ll_inode_info *lli = ll_i2info(inode);
76         struct mdt_body *body = ((struct lustre_md *)opaque)->body;
77
78         if (unlikely(!(body->mbo_valid & OBD_MD_FLID))) {
79                 CERROR("MDS body missing FID\n");
80                 return -EINVAL;
81         }
82
83         lli->lli_fid = body->mbo_fid1;
84         if (unlikely(!(body->mbo_valid & OBD_MD_FLTYPE))) {
85                 CERROR("Can not initialize inode "DFID" without object type: "
86                        "valid = %#llx\n",
87                        PFID(&lli->lli_fid), body->mbo_valid);
88                 return -EINVAL;
89         }
90
91         inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mbo_mode & S_IFMT);
92         if (unlikely(inode->i_mode == 0)) {
93                 CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
94                 return -EINVAL;
95         }
96
97         ll_lli_init(lli);
98
99         return 0;
100 }
101
102
103 /**
104  * Get an inode by inode number(@hash), which is already instantiated by
105  * the intent lookup).
106  */
107 struct inode *ll_iget(struct super_block *sb, ino_t hash,
108                       struct lustre_md *md)
109 {
110         struct inode    *inode;
111         int             rc = 0;
112
113         ENTRY;
114
115         LASSERT(hash != 0);
116         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
117         if (inode == NULL)
118                 RETURN(ERR_PTR(-ENOMEM));
119
120         if (inode->i_state & I_NEW) {
121                 rc = ll_read_inode2(inode, md);
122                 if (rc == 0 && S_ISREG(inode->i_mode) &&
123                     ll_i2info(inode)->lli_clob == NULL)
124                         rc = cl_file_inode_init(inode, md);
125
126                 if (rc != 0) {
127                         /* Let's clear directory lsm here, otherwise
128                          * make_bad_inode() will reset the inode mode
129                          * to regular, then ll_clear_inode will not
130                          * be able to clear lsm_md */
131                         if (S_ISDIR(inode->i_mode))
132                                 ll_dir_clear_lsm_md(inode);
133                         make_bad_inode(inode);
134                         unlock_new_inode(inode);
135                         iput(inode);
136                         inode = ERR_PTR(rc);
137                 } else {
138                         inode_has_no_xattr(inode);
139                         unlock_new_inode(inode);
140                 }
141         } else if (is_bad_inode(inode)) {
142                 iput(inode);
143                 inode = ERR_PTR(-ESTALE);
144         } else if (!(inode->i_state & (I_FREEING | I_CLEAR))) {
145                 rc = ll_update_inode(inode, md);
146                 CDEBUG(D_VFSTRACE, "got inode: "DFID"(%p): rc = %d\n",
147                        PFID(&md->body->mbo_fid1), inode, rc);
148                 if (rc != 0) {
149                         if (S_ISDIR(inode->i_mode))
150                                 ll_dir_clear_lsm_md(inode);
151                         iput(inode);
152                         inode = ERR_PTR(rc);
153                 }
154         }
155
156         RETURN(inode);
157 }
158
159 static void ll_invalidate_negative_children(struct inode *dir)
160 {
161         struct dentry *dentry, *tmp_subdir;
162         DECLARE_LL_D_HLIST_NODE_PTR(p);
163
164         ll_lock_dcache(dir);
165         ll_d_hlist_for_each_entry(dentry, p, &dir->i_dentry) {
166                 spin_lock(&dentry->d_lock);
167                 if (!list_empty(&dentry->d_subdirs)) {
168                         struct dentry *child;
169
170                         list_for_each_entry_safe(child, tmp_subdir,
171                                                  &dentry->d_subdirs,
172                                                  d_child) {
173                                 if (child->d_inode == NULL)
174                                         d_lustre_invalidate(child, 1);
175                         }
176                 }
177                 spin_unlock(&dentry->d_lock);
178         }
179         ll_unlock_dcache(dir);
180 }
181
182 int ll_test_inode_by_fid(struct inode *inode, void *opaque)
183 {
184         return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
185 }
186
187 int ll_dom_lock_cancel(struct inode *inode, struct ldlm_lock *lock)
188 {
189         struct lu_env *env;
190         struct ll_inode_info *lli = ll_i2info(inode);
191         struct cl_layout clt = { .cl_layout_gen = 0, };
192         int rc;
193         __u16 refcheck;
194
195
196         ENTRY;
197
198         if (!lli->lli_clob)
199                 RETURN(0);
200
201         env = cl_env_get(&refcheck);
202         if (IS_ERR(env))
203                 RETURN(PTR_ERR(env));
204
205         rc = cl_object_layout_get(env, lli->lli_clob, &clt);
206         if (rc) {
207                 CDEBUG(D_INODE, "Cannot get layout for "DFID"\n",
208                        PFID(ll_inode2fid(inode)));
209                 rc = -ENODATA;
210         } else if (clt.cl_dom_comp_size == 0) {
211                 CDEBUG(D_INODE, "DOM lock without DOM layout for "DFID"\n",
212                        PFID(ll_inode2fid(inode)));
213                 rc = -EINVAL;
214         } else {
215                 enum cl_fsync_mode mode;
216                 loff_t end = clt.cl_dom_comp_size - 1;
217
218                 mode = ldlm_is_discard_data(lock) ?
219                                         CL_FSYNC_DISCARD : CL_FSYNC_LOCAL;
220                 rc = cl_sync_file_range(inode, 0, end, mode, 1);
221                 truncate_inode_pages_range(inode->i_mapping, 0, end);
222         }
223         cl_env_put(env, &refcheck);
224         RETURN(rc);
225 }
226
227 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
228                        void *data, int flag)
229 {
230         struct lustre_handle lockh;
231         int rc;
232         ENTRY;
233
234         switch (flag) {
235         case LDLM_CB_BLOCKING:
236                 ldlm_lock2handle(lock, &lockh);
237                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
238                 if (rc < 0) {
239                         CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
240                         RETURN(rc);
241                 }
242                 break;
243         case LDLM_CB_CANCELING: {
244                 struct inode *inode = ll_inode_from_resource_lock(lock);
245                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
246
247                 if (inode == NULL)
248                         break;
249
250                 /* Invalidate all dentries associated with this inode */
251                 LASSERT(ldlm_is_canceling(lock));
252
253                 if (!fid_res_name_eq(ll_inode2fid(inode),
254                                      &lock->l_resource->lr_name)) {
255                         LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
256                                    PFID(ll_inode2fid(inode)), inode);
257                         LBUG();
258                 }
259
260                 if (bits & MDS_INODELOCK_XATTR) {
261                         if (S_ISDIR(inode->i_mode))
262                                 ll_i2info(inode)->lli_def_stripe_offset = -1;
263                         ll_xattr_cache_destroy(inode);
264                         bits &= ~MDS_INODELOCK_XATTR;
265                 }
266
267                 /* For OPEN locks we differentiate between lock modes
268                  * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
269                 if (bits & MDS_INODELOCK_OPEN)
270                         ll_have_md_lock(inode, &bits, lock->l_req_mode);
271
272                 if (bits & MDS_INODELOCK_OPEN) {
273                         fmode_t fmode;
274
275                         switch (lock->l_req_mode) {
276                         case LCK_CW:
277                                 fmode = FMODE_WRITE;
278                                 break;
279                         case LCK_PR:
280                                 fmode = FMODE_EXEC;
281                                 break;
282                         case LCK_CR:
283                                 fmode = FMODE_READ;
284                                 break;
285                         default:
286                                 LDLM_ERROR(lock, "bad lock mode for OPEN lock");
287                                 LBUG();
288                         }
289
290                         ll_md_real_close(inode, fmode);
291
292                         bits &= ~MDS_INODELOCK_OPEN;
293                 }
294
295                 if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
296                             MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM |
297                             MDS_INODELOCK_DOM))
298                         ll_have_md_lock(inode, &bits, LCK_MINMODE);
299
300                 if (bits & MDS_INODELOCK_DOM) {
301                         rc =  ll_dom_lock_cancel(inode, lock);
302                         if (rc < 0)
303                                 CDEBUG(D_INODE, "cannot flush DoM data "
304                                        DFID": rc = %d\n",
305                                        PFID(ll_inode2fid(inode)), rc);
306                         lock_res_and_lock(lock);
307                         ldlm_set_kms_ignore(lock);
308                         unlock_res_and_lock(lock);
309                         bits &= ~MDS_INODELOCK_DOM;
310                 }
311
312                 if (bits & MDS_INODELOCK_LAYOUT) {
313                         struct cl_object_conf conf = {
314                                 .coc_opc = OBJECT_CONF_INVALIDATE,
315                                 .coc_inode = inode,
316                         };
317
318                         rc = ll_layout_conf(inode, &conf);
319                         if (rc < 0)
320                                 CDEBUG(D_INODE, "cannot invalidate layout of "
321                                        DFID": rc = %d\n",
322                                        PFID(ll_inode2fid(inode)), rc);
323                 }
324
325                 if (bits & MDS_INODELOCK_UPDATE) {
326                         struct ll_inode_info *lli = ll_i2info(inode);
327                         lli->lli_update_atime = 1;
328                 }
329
330                 if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
331                         struct ll_inode_info *lli = ll_i2info(inode);
332
333                         CDEBUG(D_INODE, "invalidating inode "DFID" lli = %p, "
334                                "pfid  = "DFID"\n", PFID(ll_inode2fid(inode)),
335                                lli, PFID(&lli->lli_pfid));
336                         truncate_inode_pages(inode->i_mapping, 0);
337
338                         if (unlikely(!fid_is_zero(&lli->lli_pfid))) {
339                                 struct inode *master_inode = NULL;
340                                 unsigned long hash;
341
342                                 /* This is slave inode, since all of the child
343                                  * dentry is connected on the master inode, so
344                                  * we have to invalidate the negative children
345                                  * on master inode */
346                                 CDEBUG(D_INODE, "Invalidate s"DFID" m"DFID"\n",
347                                        PFID(ll_inode2fid(inode)),
348                                        PFID(&lli->lli_pfid));
349
350                                 hash = cl_fid_build_ino(&lli->lli_pfid,
351                                         ll_need_32bit_api(ll_i2sbi(inode)));
352
353                                 /* Do not lookup the inode with ilookup5,
354                                  * otherwise it will cause dead lock,
355                                  *
356                                  * 1. Client1 send chmod req to the MDT0, then
357                                  * on MDT0, it enqueues master and all of its
358                                  * slaves lock, (mdt_attr_set() ->
359                                  * mdt_lock_slaves()), after gets master and
360                                  * stripe0 lock, it will send the enqueue req
361                                  * (for stripe1) to MDT1, then MDT1 finds the
362                                  * lock has been granted to client2. Then MDT1
363                                  * sends blocking ast to client2.
364                                  *
365                                  * 2. At the same time, client2 tries to unlink
366                                  * the striped dir (rm -rf striped_dir), and
367                                  * during lookup, it will hold the master inode
368                                  * of the striped directory, whose inode state
369                                  * is NEW, then tries to revalidate all of its
370                                  * slaves, (ll_prep_inode()->ll_iget()->
371                                  * ll_read_inode2()-> ll_update_inode().). And
372                                  * it will be blocked on the server side because
373                                  * of 1.
374                                  *
375                                  * 3. Then the client get the blocking_ast req,
376                                  * cancel the lock, but being blocked if using
377                                  * ->ilookup5()), because master inode state is
378                                  *  NEW. */
379                                 master_inode = ilookup5_nowait(inode->i_sb,
380                                                     hash, ll_test_inode_by_fid,
381                                                         (void *)&lli->lli_pfid);
382                                 if (master_inode) {
383                                         ll_invalidate_negative_children(
384                                                                 master_inode);
385                                         iput(master_inode);
386                                 }
387                         } else {
388                                 ll_invalidate_negative_children(inode);
389                         }
390                 }
391
392                 if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
393                     inode->i_sb->s_root != NULL &&
394                     inode != inode->i_sb->s_root->d_inode)
395                         ll_invalidate_aliases(inode);
396
397                 iput(inode);
398                 break;
399         }
400         default:
401                 LBUG();
402         }
403
404         RETURN(0);
405 }
406
407 __u32 ll_i2suppgid(struct inode *i)
408 {
409         if (in_group_p(i->i_gid))
410                 return (__u32)from_kgid(&init_user_ns, i->i_gid);
411         else
412                 return (__u32) __kgid_val(INVALID_GID);
413 }
414
415 /* Pack the required supplementary groups into the supplied groups array.
416  * If we don't need to use the groups from the target inode(s) then we
417  * instead pack one or more groups from the user's supplementary group
418  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
419 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
420 {
421         LASSERT(i1 != NULL);
422         LASSERT(suppgids != NULL);
423
424         suppgids[0] = ll_i2suppgid(i1);
425
426         if (i2)
427                 suppgids[1] = ll_i2suppgid(i2);
428         else
429                 suppgids[1] = -1;
430 }
431
432 /*
433  * try to reuse three types of dentry:
434  * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
435  *    by concurrent .revalidate).
436  * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
437  *    be cleared by others calling d_lustre_revalidate).
438  * 3. DISCONNECTED alias.
439  */
440 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
441 {
442         struct dentry *alias, *discon_alias, *invalid_alias;
443         DECLARE_LL_D_HLIST_NODE_PTR(p);
444
445         if (ll_d_hlist_empty(&inode->i_dentry))
446                 return NULL;
447
448         discon_alias = invalid_alias = NULL;
449
450         ll_lock_dcache(inode);
451         ll_d_hlist_for_each_entry(alias, p, &inode->i_dentry) {
452                 LASSERT(alias != dentry);
453
454                 spin_lock(&alias->d_lock);
455                 if ((alias->d_flags & DCACHE_DISCONNECTED) &&
456                     S_ISDIR(inode->i_mode))
457                         /* LASSERT(last_discon == NULL); LU-405, bz 20055 */
458                         discon_alias = alias;
459                 else if (alias->d_parent == dentry->d_parent             &&
460                          alias->d_name.hash == dentry->d_name.hash       &&
461                          alias->d_name.len == dentry->d_name.len         &&
462                          memcmp(alias->d_name.name, dentry->d_name.name,
463                                 dentry->d_name.len) == 0)
464                         invalid_alias = alias;
465                 spin_unlock(&alias->d_lock);
466
467                 if (invalid_alias)
468                         break;
469         }
470         alias = invalid_alias ?: discon_alias ?: NULL;
471         if (alias) {
472                 spin_lock(&alias->d_lock);
473                 dget_dlock(alias);
474                 spin_unlock(&alias->d_lock);
475         }
476         ll_unlock_dcache(inode);
477
478         return alias;
479 }
480
481 /*
482  * Similar to d_splice_alias(), but lustre treats invalid alias
483  * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
484  */
485 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
486 {
487         struct dentry *new;
488         int rc;
489
490         if (inode) {
491                 new = ll_find_alias(inode, de);
492                 if (new) {
493                         rc = ll_d_init(new);
494                         if (rc < 0) {
495                                 dput(new);
496                                 return ERR_PTR(rc);
497                         }
498                         d_move(new, de);
499                         iput(inode);
500                         CDEBUG(D_DENTRY,
501                                "Reuse dentry %p inode %p refc %d flags %#x\n",
502                               new, new->d_inode, ll_d_count(new), new->d_flags);
503                         return new;
504                 }
505         }
506         rc = ll_d_init(de);
507         if (rc < 0)
508                 return ERR_PTR(rc);
509         d_add(de, inode);
510         CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
511                de, de->d_inode, ll_d_count(de), de->d_flags);
512         return de;
513 }
514
515 static int ll_lookup_it_finish(struct ptlrpc_request *request,
516                                struct lookup_intent *it,
517                                struct inode *parent, struct dentry **de)
518 {
519         struct inode             *inode = NULL;
520         __u64                     bits = 0;
521         int                       rc;
522         struct dentry *alias;
523         ENTRY;
524
525         /* NB 1 request reference will be taken away by ll_intent_lock()
526          * when I return */
527         CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
528                it->it_disposition);
529         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
530                 rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
531                 if (rc)
532                         RETURN(rc);
533
534                 ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
535
536                 /* We used to query real size from OSTs here, but actually
537                    this is not needed. For stat() calls size would be updated
538                    from subsequent do_revalidate()->ll_inode_revalidate_it() in
539                    2.4 and
540                    vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
541                    Everybody else who needs correct file size would call
542                    ll_glimpse_size or some equivalent themselves anyway.
543                    Also see bug 7198. */
544         }
545
546         /* Only hash *de if it is unhashed (new dentry).
547          * Atoimc_open may passin hashed dentries for open.
548          */
549         alias = ll_splice_alias(inode, *de);
550         if (IS_ERR(alias))
551                 GOTO(out, rc = PTR_ERR(alias));
552
553         *de = alias;
554
555         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
556                 /* we have lookup look - unhide dentry */
557                 if (bits & MDS_INODELOCK_LOOKUP)
558                         d_lustre_revalidate(*de);
559         } else if (!it_disposition(it, DISP_OPEN_CREATE)) {
560                 /* If file created on server, don't depend on parent UPDATE
561                  * lock to unhide it. It is left hidden and next lookup can
562                  * find it in ll_splice_alias.
563                  */
564                 /* Check that parent has UPDATE lock. */
565                 struct lookup_intent parent_it = {
566                                         .it_op = IT_GETATTR,
567                                         .it_lock_handle = 0 };
568                 struct lu_fid   fid = ll_i2info(parent)->lli_fid;
569
570                 /* If it is striped directory, get the real stripe parent */
571                 if (unlikely(ll_i2info(parent)->lli_lsm_md != NULL)) {
572                         rc = md_get_fid_from_lsm(ll_i2mdexp(parent),
573                                                  ll_i2info(parent)->lli_lsm_md,
574                                                  (*de)->d_name.name,
575                                                  (*de)->d_name.len, &fid);
576                         if (rc != 0)
577                                 GOTO(out, rc);
578                 }
579
580                 if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it, &fid,
581                                        NULL)) {
582                         d_lustre_revalidate(*de);
583                         ll_intent_release(&parent_it);
584                 }
585         }
586
587         GOTO(out, rc = 0);
588
589 out:
590         if (rc != 0 && it->it_op & IT_OPEN)
591                 ll_open_cleanup((*de)->d_sb, request);
592
593         return rc;
594 }
595
596 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
597                                    struct lookup_intent *it,
598                                    void **secctx, __u32 *secctxlen)
599 {
600         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
601         struct dentry *save = dentry, *retval;
602         struct ptlrpc_request *req = NULL;
603         struct md_op_data *op_data = NULL;
604         __u32 opc;
605         int rc;
606         ENTRY;
607
608         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
609                 RETURN(ERR_PTR(-ENAMETOOLONG));
610
611         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
612                dentry->d_name.len, dentry->d_name.name,
613                PFID(ll_inode2fid(parent)), parent, LL_IT2STR(it));
614
615         if (d_mountpoint(dentry))
616                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
617
618         if (it == NULL || it->it_op == IT_GETXATTR)
619                 it = &lookup_it;
620
621         if (it->it_op == IT_GETATTR && dentry_may_statahead(parent, dentry)) {
622                 rc = ll_statahead(parent, &dentry, 0);
623                 if (rc == 1)
624                         RETURN(dentry == save ? NULL : dentry);
625         }
626
627         if (it->it_op & IT_OPEN && it->it_flags & FMODE_WRITE &&
628             dentry->d_sb->s_flags & MS_RDONLY)
629                 RETURN(ERR_PTR(-EROFS));
630
631         if (it->it_op & IT_CREAT)
632                 opc = LUSTRE_OPC_CREATE;
633         else
634                 opc = LUSTRE_OPC_ANY;
635
636         op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
637                                      dentry->d_name.len, 0, opc, NULL);
638         if (IS_ERR(op_data))
639                 GOTO(out, retval = ERR_CAST(op_data));
640
641         /* enforce umask if acl disabled or MDS doesn't support umask */
642         if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
643                 it->it_create_mode &= ~current_umask();
644
645         if (it->it_op & IT_CREAT &&
646             ll_i2sbi(parent)->ll_flags & LL_SBI_FILE_SECCTX) {
647                 rc = ll_dentry_init_security(dentry, it->it_create_mode,
648                                              &dentry->d_name,
649                                              &op_data->op_file_secctx_name,
650                                              &op_data->op_file_secctx,
651                                              &op_data->op_file_secctx_size);
652                 if (rc < 0)
653                         GOTO(out, retval = ERR_PTR(rc));
654                 if (secctx != NULL)
655                         *secctx = op_data->op_file_secctx;
656                 if (secctxlen != NULL)
657                         *secctxlen = op_data->op_file_secctx_size;
658         }
659
660         rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
661                             &ll_md_blocking_ast, 0);
662         /* If the MDS allows the client to chgrp (CFS_SETGRP_PERM), but the
663          * client does not know which suppgid should be sent to the MDS, or
664          * some other(s) changed the target file's GID after this RPC sent
665          * to the MDS with the suppgid as the original GID, then we should
666          * try again with right suppgid. */
667         if (rc == -EACCES && it->it_op & IT_OPEN &&
668             it_disposition(it, DISP_OPEN_DENY)) {
669                 struct mdt_body *body;
670
671                 LASSERT(req != NULL);
672
673                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
674                 if (op_data->op_suppgids[0] == body->mbo_gid ||
675                     op_data->op_suppgids[1] == body->mbo_gid ||
676                     !in_group_p(make_kgid(&init_user_ns, body->mbo_gid)))
677                         GOTO(out, retval = ERR_PTR(-EACCES));
678
679                 fid_zero(&op_data->op_fid2);
680                 op_data->op_suppgids[1] = body->mbo_gid;
681                 ptlrpc_req_finished(req);
682                 req = NULL;
683                 ll_intent_release(it);
684                 rc = md_intent_lock(ll_i2mdexp(parent), op_data, it, &req,
685                                     &ll_md_blocking_ast, 0);
686         }
687
688         if (rc < 0)
689                 GOTO(out, retval = ERR_PTR(rc));
690
691         rc = ll_lookup_it_finish(req, it, parent, &dentry);
692         if (rc != 0) {
693                 ll_intent_release(it);
694                 GOTO(out, retval = ERR_PTR(rc));
695         }
696
697         if ((it->it_op & IT_OPEN) && dentry->d_inode &&
698             !S_ISREG(dentry->d_inode->i_mode) &&
699             !S_ISDIR(dentry->d_inode->i_mode)) {
700                 ll_release_openhandle(dentry, it);
701         }
702         ll_lookup_finish_locks(it, dentry);
703
704         GOTO(out, retval = (dentry == save) ? NULL : dentry);
705
706 out:
707         if (op_data != NULL && !IS_ERR(op_data)) {
708                 if (secctx != NULL && secctxlen != NULL) {
709                         /* caller needs sec ctx info, so reset it in op_data to
710                          * prevent it from being freed */
711                         op_data->op_file_secctx = NULL;
712                         op_data->op_file_secctx_size = 0;
713                 }
714                 ll_finish_md_op_data(op_data);
715         }
716
717         ptlrpc_req_finished(req);
718         return retval;
719 }
720
721 #ifdef HAVE_IOP_ATOMIC_OPEN
722 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
723                                    unsigned int flags)
724 {
725         struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
726         struct dentry *de;
727
728         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), flags=%u\n",
729                dentry->d_name.len, dentry->d_name.name,
730                PFID(ll_inode2fid(parent)), parent, flags);
731
732         /*
733          * Optimize away (CREATE && !OPEN). Let .create handle the race.
734          * but only if we have write permissions there, otherwise we need
735          * to proceed with lookup. LU-4185
736          */
737         if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN) &&
738             (inode_permission(parent, MAY_WRITE | MAY_EXEC) == 0))
739                 return NULL;
740
741         if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
742                 itp = NULL;
743         else
744                 itp = &it;
745         de = ll_lookup_it(parent, dentry, itp, NULL, NULL);
746
747         if (itp != NULL)
748                 ll_intent_release(itp);
749
750         return de;
751 }
752
753 /*
754  * For cached negative dentry and new dentry, handle lookup/create/open
755  * together.
756  */
757 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
758                           struct file *file, unsigned open_flags,
759                           umode_t mode, int *opened)
760 {
761         struct lookup_intent *it;
762         struct dentry *de;
763         long long lookup_flags = LOOKUP_OPEN;
764         void *secctx = NULL;
765         __u32 secctxlen = 0;
766         int rc = 0;
767         ENTRY;
768
769         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), file %p,"
770                            "open_flags %x, mode %x opened %d\n",
771                dentry->d_name.len, dentry->d_name.name,
772                PFID(ll_inode2fid(dir)), dir, file, open_flags, mode, *opened);
773
774         /* Only negative dentries enter here */
775         LASSERT(dentry->d_inode == NULL);
776
777         if (!d_unhashed(dentry)) {
778                 /* A valid negative dentry that just passed revalidation,
779                  * there's little point to try and open it server-side,
780                  * even though there's a minuscule chance it might succeed.
781                  * Either way it's a valid race to just return -ENOENT here.
782                  */
783                 if (!(open_flags & O_CREAT))
784                         return -ENOENT;
785
786                 /* Otherwise we just unhash it to be rehashed afresh via
787                  * lookup if necessary
788                  */
789                 d_drop(dentry);
790         }
791
792         OBD_ALLOC(it, sizeof(*it));
793         if (!it)
794                 RETURN(-ENOMEM);
795
796         it->it_op = IT_OPEN;
797         if (open_flags & O_CREAT) {
798                 it->it_op |= IT_CREAT;
799                 lookup_flags |= LOOKUP_CREATE;
800         }
801         it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
802         it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
803         it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
804
805         /* Dentry added to dcache tree in ll_lookup_it */
806         de = ll_lookup_it(dir, dentry, it, &secctx, &secctxlen);
807         if (IS_ERR(de))
808                 rc = PTR_ERR(de);
809         else if (de != NULL)
810                 dentry = de;
811
812         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
813
814         if (!rc) {
815                 if (it_disposition(it, DISP_OPEN_CREATE)) {
816                         /* Dentry instantiated in ll_create_it. */
817                         rc = ll_create_it(dir, dentry, it, secctx, secctxlen);
818                         security_release_secctx(secctx, secctxlen);
819                         if (rc) {
820                                 /* We dget in ll_splice_alias. */
821                                 if (de != NULL)
822                                         dput(de);
823                                 goto out_release;
824                         }
825
826                         *opened |= FILE_CREATED;
827                 }
828                 if (dentry->d_inode && it_disposition(it, DISP_OPEN_OPEN)) {
829                         /* Open dentry. */
830                         if (S_ISFIFO(dentry->d_inode->i_mode)) {
831                                 /* We cannot call open here as it might
832                                  * deadlock. This case is unreachable in
833                                  * practice because of OBD_CONNECT_NODEVOH. */
834                                 rc = finish_no_open(file, de);
835                         } else {
836                                 file->private_data = it;
837                                 rc = finish_open(file, dentry, NULL, opened);
838                                 /* We dget in ll_splice_alias. finish_open takes
839                                  * care of dget for fd open.
840                                  */
841                                 if (de != NULL)
842                                         dput(de);
843                         }
844                 } else {
845                         rc = finish_no_open(file, de);
846                 }
847         }
848
849 out_release:
850         ll_intent_release(it);
851         OBD_FREE(it, sizeof(*it));
852
853         RETURN(rc);
854 }
855
856 #else /* !HAVE_IOP_ATOMIC_OPEN */
857 static struct lookup_intent *
858 ll_convert_intent(struct open_intent *oit, int lookup_flags, bool is_readonly)
859 {
860         struct lookup_intent *it;
861
862         OBD_ALLOC_PTR(it);
863         if (!it)
864                 return ERR_PTR(-ENOMEM);
865
866         if (lookup_flags & LOOKUP_OPEN) {
867                 it->it_op = IT_OPEN;
868                 /* Avoid file creation for ro bind mount point(is_readonly) */
869                 if ((lookup_flags & LOOKUP_CREATE) && !is_readonly)
870                         it->it_op |= IT_CREAT;
871                 it->it_create_mode = (oit->create_mode & S_IALLUGO) | S_IFREG;
872                 it->it_flags = ll_namei_to_lookup_intent_flag(oit->flags &
873                                                 ~(is_readonly ? O_CREAT : 0));
874                 it->it_flags &= ~MDS_OPEN_FL_INTERNAL;
875         } else {
876                 it->it_op = IT_GETATTR;
877         }
878
879         return it;
880 }
881
882 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
883                                    struct nameidata *nd)
884 {
885         struct dentry *de;
886         ENTRY;
887
888         if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
889                 struct lookup_intent *it;
890
891                 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
892                         it = ll_d2d(dentry)->lld_it;
893                         ll_d2d(dentry)->lld_it = NULL;
894                 } else {
895                         /*
896                          * Optimize away (CREATE && !OPEN). Let .create handle
897                          * the race. But only if we have write permissions
898                          * there, otherwise we need to proceed with lookup.
899                          * LU-4185
900                          */
901                         if ((nd->flags & LOOKUP_CREATE) &&
902                             !(nd->flags & LOOKUP_OPEN) &&
903                             (inode_permission(parent,
904                                               MAY_WRITE | MAY_EXEC) == 0))
905                                 RETURN(NULL);
906
907                         it = ll_convert_intent(&nd->intent.open, nd->flags,
908                                 (nd->path.mnt->mnt_flags & MNT_READONLY) ||
909                                 (nd->path.mnt->mnt_sb->s_flags & MS_RDONLY));
910                         if (IS_ERR(it))
911                                 RETURN((struct dentry *)it);
912                 }
913
914                 de = ll_lookup_it(parent, dentry, it, NULL, NULL);
915                 if (de)
916                         dentry = de;
917                 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
918                         if (dentry->d_inode &&
919                             it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
920                                 if (S_ISFIFO(dentry->d_inode->i_mode)) {
921                                         /* We cannot call open here as it might
922                                          * deadlock. This case is unreachable in
923                                          * practice because of
924                                          * OBD_CONNECT_NODEVOH. */
925                                 } else {
926                                         struct file *filp;
927
928                                         nd->intent.open.file->private_data = it;
929                                         filp = lookup_instantiate_filp(nd,
930                                                                        dentry,
931                                                                        NULL);
932                                         if (IS_ERR(filp)) {
933                                                 if (de)
934                                                         dput(de);
935                                                 de = (struct dentry *)filp;
936                                         }
937                                 }
938                         } else if (it_disposition(it, DISP_OPEN_CREATE)) {
939                                 /* XXX This can only reliably work on assumption
940                                  * that there are NO hashed negative dentries.*/
941                                 ll_d2d(dentry)->lld_it = it;
942                                 it = NULL; /* Will be freed in ll_create_nd */
943                                 /* We absolutely depend on ll_create_nd to be
944                                  * called to not leak this intent and possible
945                                  * data attached to it */
946                         }
947                 }
948
949                 if (it) {
950                         ll_intent_release(it);
951                         OBD_FREE(it, sizeof(*it));
952                 }
953         } else {
954                 de = ll_lookup_it(parent, dentry, NULL, NULL, NULL);
955         }
956
957         RETURN(de);
958 }
959 #endif /* HAVE_IOP_ATOMIC_OPEN */
960
961 /* We depend on "mode" being set with the proper file type/umask by now */
962 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
963 {
964         struct inode *inode = NULL;
965         struct ptlrpc_request *request = NULL;
966         struct ll_sb_info *sbi = ll_i2sbi(dir);
967         int rc;
968         ENTRY;
969
970         LASSERT(it && it->it_disposition);
971
972         LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
973         request = it->it_request;
974         it_clear_disposition(it, DISP_ENQ_CREATE_REF);
975         rc = ll_prep_inode(&inode, request, dir->i_sb, it);
976         if (rc)
977                 GOTO(out, inode = ERR_PTR(rc));
978
979         /* Pause to allow for a race with concurrent access by fid */
980         OBD_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_NODE_PAUSE, cfs_fail_val);
981
982         /* We asked for a lock on the directory, but were granted a
983          * lock on the inode.  Since we finally have an inode pointer,
984          * stuff it in the lock. */
985         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode "DFID"(%p)\n",
986                PFID(ll_inode2fid(inode)), inode);
987         ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
988         EXIT;
989  out:
990         ptlrpc_req_finished(request);
991         return inode;
992 }
993
994 /*
995  * By the time this is called, we already have created the directory cache
996  * entry for the new file, but it is so far negative - it has no inode.
997  *
998  * We defer creating the OBD object(s) until open, to keep the intent and
999  * non-intent code paths similar, and also because we do not have the MDS
1000  * inode number before calling ll_create_node() (which is needed for LOV),
1001  * so we would need to do yet another RPC to the MDS to store the LOV EA
1002  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
1003  * lmm_size in datalen (the MDS still has code which will handle that).
1004  *
1005  * If the create succeeds, we fill in the inode information
1006  * with d_instantiate().
1007  */
1008 static int ll_create_it(struct inode *dir, struct dentry *dentry,
1009                         struct lookup_intent *it,
1010                         void *secctx, __u32 secctxlen)
1011 {
1012         struct inode *inode;
1013         int rc = 0;
1014         ENTRY;
1015
1016         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), intent=%s\n",
1017                dentry->d_name.len, dentry->d_name.name,
1018                PFID(ll_inode2fid(dir)), dir, LL_IT2STR(it));
1019
1020         rc = it_open_error(DISP_OPEN_CREATE, it);
1021         if (rc)
1022                 RETURN(rc);
1023
1024         inode = ll_create_node(dir, it);
1025         if (IS_ERR(inode))
1026                 RETURN(PTR_ERR(inode));
1027
1028         if ((ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX) &&
1029             secctx != NULL) {
1030                 inode_lock(inode);
1031                 /* must be done before d_instantiate, because it calls
1032                  * security_d_instantiate, which means a getxattr if security
1033                  * context is not set yet */
1034                 rc = security_inode_notifysecctx(inode, secctx, secctxlen);
1035                 inode_unlock(inode);
1036                 if (rc)
1037                         RETURN(rc);
1038         }
1039
1040         d_instantiate(dentry, inode);
1041
1042         if (!(ll_i2sbi(inode)->ll_flags & LL_SBI_FILE_SECCTX)) {
1043                 rc = ll_inode_init_security(dentry, inode, dir);
1044                 if (rc)
1045                         RETURN(rc);
1046         }
1047
1048         RETURN(0);
1049 }
1050
1051 void ll_update_times(struct ptlrpc_request *request, struct inode *inode)
1052 {
1053         struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
1054                                                        &RMF_MDT_BODY);
1055
1056         LASSERT(body);
1057         if (body->mbo_valid & OBD_MD_FLMTIME &&
1058             body->mbo_mtime > LTIME_S(inode->i_mtime)) {
1059                 CDEBUG(D_INODE, "setting fid "DFID" mtime from %lu to %llu"
1060                        "\n", PFID(ll_inode2fid(inode)),
1061                        LTIME_S(inode->i_mtime), body->mbo_mtime);
1062                 LTIME_S(inode->i_mtime) = body->mbo_mtime;
1063         }
1064
1065         if (body->mbo_valid & OBD_MD_FLCTIME &&
1066             body->mbo_ctime > LTIME_S(inode->i_ctime))
1067                 LTIME_S(inode->i_ctime) = body->mbo_ctime;
1068 }
1069
1070 static int ll_new_node(struct inode *dir, struct dentry *dchild,
1071                        const char *tgt, umode_t mode, int rdev, __u32 opc)
1072 {
1073         struct qstr *name = &dchild->d_name;
1074         struct ptlrpc_request *request = NULL;
1075         struct md_op_data *op_data;
1076         struct inode *inode = NULL;
1077         struct ll_sb_info *sbi = ll_i2sbi(dir);
1078         int tgt_len = 0;
1079         int err;
1080
1081         ENTRY;
1082         if (unlikely(tgt != NULL))
1083                 tgt_len = strlen(tgt) + 1;
1084
1085 again:
1086         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1087                                      name->len, 0, opc, NULL);
1088         if (IS_ERR(op_data))
1089                 GOTO(err_exit, err = PTR_ERR(op_data));
1090
1091         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1092                 err = ll_dentry_init_security(dchild, mode, &dchild->d_name,
1093                                               &op_data->op_file_secctx_name,
1094                                               &op_data->op_file_secctx,
1095                                               &op_data->op_file_secctx_size);
1096                 if (err < 0)
1097                         GOTO(err_exit, err);
1098         }
1099
1100         err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
1101                         from_kuid(&init_user_ns, current_fsuid()),
1102                         from_kgid(&init_user_ns, current_fsgid()),
1103                         cfs_curproc_cap_pack(), rdev, &request);
1104         if (err < 0 && err != -EREMOTE)
1105                 GOTO(err_exit, err);
1106
1107         /* If the client doesn't know where to create a subdirectory (or
1108          * in case of a race that sends the RPC to the wrong MDS), the
1109          * MDS will return -EREMOTE and the client will fetch the layout
1110          * of the directory, then create the directory on the right MDT. */
1111         if (unlikely(err == -EREMOTE)) {
1112                 struct ll_inode_info    *lli = ll_i2info(dir);
1113                 struct lmv_user_md      *lum;
1114                 int                     lumsize;
1115                 int                     err2;
1116
1117                 ptlrpc_req_finished(request);
1118                 request = NULL;
1119
1120                 err2 = ll_dir_getstripe(dir, (void **)&lum, &lumsize, &request,
1121                                         OBD_MD_DEFAULT_MEA);
1122                 if (err2 == 0) {
1123                         /* Update stripe_offset and retry */
1124                         lli->lli_def_stripe_offset = lum->lum_stripe_offset;
1125                 } else if (err2 == -ENODATA &&
1126                            lli->lli_def_stripe_offset != -1) {
1127                         /* If there are no default stripe EA on the MDT, but the
1128                          * client has default stripe, then it probably means
1129                          * default stripe EA has just been deleted. */
1130                         lli->lli_def_stripe_offset = -1;
1131                 } else {
1132                         GOTO(err_exit, err);
1133                 }
1134
1135                 ptlrpc_req_finished(request);
1136                 request = NULL;
1137                 ll_finish_md_op_data(op_data);
1138                 goto again;
1139         }
1140
1141         ll_update_times(request, dir);
1142
1143         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_NEWNODE_PAUSE, cfs_fail_val);
1144
1145         err = ll_prep_inode(&inode, request, dchild->d_sb, NULL);
1146         if (err)
1147                 GOTO(err_exit, err);
1148
1149         if (sbi->ll_flags & LL_SBI_FILE_SECCTX) {
1150                 inode_lock(inode);
1151                 /* must be done before d_instantiate, because it calls
1152                  * security_d_instantiate, which means a getxattr if security
1153                  * context is not set yet */
1154                 err = security_inode_notifysecctx(inode,
1155                                                   op_data->op_file_secctx,
1156                                                   op_data->op_file_secctx_size);
1157                 inode_unlock(inode);
1158                 if (err)
1159                         GOTO(err_exit, err);
1160         }
1161
1162         d_instantiate(dchild, inode);
1163
1164         if (!(sbi->ll_flags & LL_SBI_FILE_SECCTX)) {
1165                 err = ll_inode_init_security(dchild, inode, dir);
1166                 if (err)
1167                         GOTO(err_exit, err);
1168         }
1169
1170         EXIT;
1171 err_exit:
1172         if (request != NULL)
1173                 ptlrpc_req_finished(request);
1174
1175         if (!IS_ERR_OR_NULL(op_data))
1176                 ll_finish_md_op_data(op_data);
1177
1178         return err;
1179 }
1180
1181 static int ll_mknod(struct inode *dir, struct dentry *dchild, ll_umode_t mode,
1182                     dev_t rdev)
1183 {
1184         struct qstr *name = &dchild->d_name;
1185         int err;
1186         ENTRY;
1187
1188         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p) mode %o dev %x\n",
1189                name->len, name->name, PFID(ll_inode2fid(dir)), dir,
1190                mode, rdev);
1191
1192         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1193                 mode &= ~current_umask();
1194
1195         switch (mode & S_IFMT) {
1196         case 0:
1197                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
1198         case S_IFREG:
1199         case S_IFCHR:
1200         case S_IFBLK:
1201         case S_IFIFO:
1202         case S_IFSOCK:
1203                 err = ll_new_node(dir, dchild, NULL, mode, old_encode_dev(rdev),
1204                                   LUSTRE_OPC_MKNOD);
1205                 break;
1206         case S_IFDIR:
1207                 err = -EPERM;
1208                 break;
1209         default:
1210                 err = -EINVAL;
1211         }
1212
1213         if (!err)
1214                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
1215
1216         RETURN(err);
1217 }
1218
1219 #ifdef HAVE_IOP_ATOMIC_OPEN
1220 /*
1221  * Plain create. Intent create is handled in atomic_open.
1222  */
1223 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1224                         umode_t mode, bool want_excl)
1225 {
1226         int rc;
1227
1228         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1229
1230         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), "
1231                            "flags=%u, excl=%d\n", dentry->d_name.len,
1232                dentry->d_name.name, PFID(ll_inode2fid(dir)),
1233                dir, mode, want_excl);
1234
1235         /* Using mknod(2) to create a regular file is designed to not recognize
1236          * volatile file name, so we use ll_mknod() here. */
1237         rc = ll_mknod(dir, dentry, mode, 0);
1238
1239         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1240
1241         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, unhashed %d\n",
1242                dentry->d_name.len, dentry->d_name.name, d_unhashed(dentry));
1243
1244         return rc;
1245 }
1246 #else /* !HAVE_IOP_ATOMIC_OPEN */
1247 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
1248                         ll_umode_t mode, struct nameidata *nd)
1249 {
1250         struct ll_dentry_data *lld = ll_d2d(dentry);
1251         struct lookup_intent *it = NULL;
1252         int rc;
1253
1254         CFS_FAIL_TIMEOUT(OBD_FAIL_LLITE_CREATE_FILE_PAUSE, cfs_fail_val);
1255
1256         if (lld != NULL)
1257                 it = lld->lld_it;
1258
1259         if (!it) {
1260                 /* LU-8559: use LUSTRE_OPC_CREATE for non atomic open case
1261                  * so that volatile file name is recoginized.
1262                  * Mknod(2), however, is designed to not recognize volatile
1263                  * file name to avoid inode leak under orphan directory until
1264                  * MDT reboot */
1265                 return ll_new_node(dir, dentry, NULL, mode, 0,
1266                                    LUSTRE_OPC_CREATE);
1267         }
1268
1269         lld->lld_it = NULL;
1270
1271         /* Was there an error? Propagate it! */
1272         if (it->it_status) {
1273                 rc = it->it_status;
1274                 goto out;
1275         }
1276
1277         rc = ll_create_it(dir, dentry, it, NULL, 0);
1278         if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
1279                 struct file *filp;
1280
1281                 nd->intent.open.file->private_data = it;
1282                 filp = lookup_instantiate_filp(nd, dentry, NULL);
1283                 if (IS_ERR(filp))
1284                         rc = PTR_ERR(filp);
1285         }
1286
1287 out:
1288         ll_intent_release(it);
1289         OBD_FREE(it, sizeof(*it));
1290
1291         if (!rc)
1292                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
1293
1294         return rc;
1295 }
1296 #endif /* HAVE_IOP_ATOMIC_OPEN */
1297
1298 static int ll_symlink(struct inode *dir, struct dentry *dchild,
1299                       const char *oldpath)
1300 {
1301         struct qstr *name = &dchild->d_name;
1302         int err;
1303         ENTRY;
1304
1305         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p), target=%.*s\n",
1306                name->len, name->name, PFID(ll_inode2fid(dir)),
1307                dir, 3000, oldpath);
1308
1309         err = ll_new_node(dir, dchild, oldpath, S_IFLNK | S_IRWXUGO, 0,
1310                           LUSTRE_OPC_SYMLINK);
1311
1312         if (!err)
1313                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1314
1315         RETURN(err);
1316 }
1317
1318 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1319                    struct dentry *new_dentry)
1320 {
1321         struct inode *src = old_dentry->d_inode;
1322         struct qstr *name = &new_dentry->d_name;
1323         struct ll_sb_info *sbi = ll_i2sbi(dir);
1324         struct ptlrpc_request *request = NULL;
1325         struct md_op_data *op_data;
1326         int err;
1327
1328         ENTRY;
1329         CDEBUG(D_VFSTRACE, "VFS Op: inode="DFID"(%p), dir="DFID"(%p), "
1330                "target=%.*s\n", PFID(ll_inode2fid(src)), src,
1331                PFID(ll_inode2fid(dir)), dir, name->len, name->name);
1332
1333         op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
1334                                      0, LUSTRE_OPC_ANY, NULL);
1335         if (IS_ERR(op_data))
1336                 RETURN(PTR_ERR(op_data));
1337
1338         err = md_link(sbi->ll_md_exp, op_data, &request);
1339         ll_finish_md_op_data(op_data);
1340         if (err)
1341                 GOTO(out, err);
1342
1343         ll_update_times(request, dir);
1344         ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1345         EXIT;
1346 out:
1347         ptlrpc_req_finished(request);
1348         RETURN(err);
1349 }
1350
1351 static int ll_mkdir(struct inode *dir, struct dentry *dchild, ll_umode_t mode)
1352 {
1353         struct qstr *name = &dchild->d_name;
1354         int err;
1355         ENTRY;
1356
1357         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1358                name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1359
1360         if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
1361                 mode &= ~current_umask();
1362
1363         mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
1364
1365         err = ll_new_node(dir, dchild, NULL, mode, 0, LUSTRE_OPC_MKDIR);
1366         if (err == 0)
1367                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
1368
1369         RETURN(err);
1370 }
1371
1372 static int ll_rmdir(struct inode *dir, struct dentry *dchild)
1373 {
1374         struct qstr *name = &dchild->d_name;
1375         struct ptlrpc_request *request = NULL;
1376         struct md_op_data *op_data;
1377         int rc;
1378         ENTRY;
1379
1380         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1381                name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1382
1383         if (unlikely(d_mountpoint(dchild)))
1384                 RETURN(-EBUSY);
1385
1386         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
1387                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
1388         if (IS_ERR(op_data))
1389                 RETURN(PTR_ERR(op_data));
1390
1391         if (dchild->d_inode != NULL)
1392                 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1393
1394         op_data->op_fid2 = op_data->op_fid3;
1395         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1396         ll_finish_md_op_data(op_data);
1397         if (rc == 0) {
1398                 ll_update_times(request, dir);
1399                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1400         }
1401
1402         ptlrpc_req_finished(request);
1403         RETURN(rc);
1404 }
1405
1406 /**
1407  * Remove dir entry
1408  **/
1409 int ll_rmdir_entry(struct inode *dir, char *name, int namelen)
1410 {
1411         struct ptlrpc_request *request = NULL;
1412         struct md_op_data *op_data;
1413         int rc;
1414         ENTRY;
1415
1416         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1417                namelen, name, PFID(ll_inode2fid(dir)), dir);
1418
1419         op_data = ll_prep_md_op_data(NULL, dir, NULL, name, strlen(name),
1420                                      S_IFDIR, LUSTRE_OPC_ANY, NULL);
1421         if (IS_ERR(op_data))
1422                 RETURN(PTR_ERR(op_data));
1423         op_data->op_cli_flags |= CLI_RM_ENTRY;
1424         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1425         ll_finish_md_op_data(op_data);
1426         if (rc == 0) {
1427                 ll_update_times(request, dir);
1428                 ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1429         }
1430
1431         ptlrpc_req_finished(request);
1432         RETURN(rc);
1433 }
1434
1435 static int ll_unlink(struct inode *dir, struct dentry *dchild)
1436 {
1437         struct qstr *name = &dchild->d_name;
1438         struct ptlrpc_request *request = NULL;
1439         struct md_op_data *op_data;
1440         int rc;
1441         ENTRY;
1442         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s, dir="DFID"(%p)\n",
1443                name->len, name->name, PFID(ll_inode2fid(dir)), dir);
1444
1445         /*
1446          * XXX: unlink bind mountpoint maybe call to here,
1447          * just check it as vfs_unlink does.
1448          */
1449         if (unlikely(d_mountpoint(dchild)))
1450                 RETURN(-EBUSY);
1451
1452         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len, 0,
1453                                      LUSTRE_OPC_ANY, NULL);
1454         if (IS_ERR(op_data))
1455                 RETURN(PTR_ERR(op_data));
1456
1457         if (dchild->d_inode != NULL)
1458                 op_data->op_fid3 = *ll_inode2fid(dchild->d_inode);
1459
1460         op_data->op_fid2 = op_data->op_fid3;
1461         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1462         ll_finish_md_op_data(op_data);
1463         if (rc)
1464                 GOTO(out, rc);
1465
1466         ll_update_times(request, dir);
1467         ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
1468
1469  out:
1470         ptlrpc_req_finished(request);
1471         RETURN(rc);
1472 }
1473
1474 static int ll_rename(struct inode *src, struct dentry *src_dchild,
1475                      struct inode *tgt, struct dentry *tgt_dchild
1476 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1477                      , unsigned int flags
1478 #endif
1479                      )
1480 {
1481         struct qstr *src_name = &src_dchild->d_name;
1482         struct qstr *tgt_name = &tgt_dchild->d_name;
1483         struct ptlrpc_request *request = NULL;
1484         struct ll_sb_info *sbi = ll_i2sbi(src);
1485         struct md_op_data *op_data;
1486         int err;
1487         ENTRY;
1488
1489 #ifdef HAVE_IOPS_RENAME_WITH_FLAGS
1490         if (flags)
1491                 return -EINVAL;
1492 #endif
1493
1494         CDEBUG(D_VFSTRACE, "VFS Op:oldname=%.*s, src_dir="DFID
1495                "(%p), newname=%.*s, tgt_dir="DFID"(%p)\n",
1496                src_name->len, src_name->name,
1497                PFID(ll_inode2fid(src)), src, tgt_name->len,
1498                tgt_name->name, PFID(ll_inode2fid(tgt)), tgt);
1499
1500         if (unlikely(d_mountpoint(src_dchild) || d_mountpoint(tgt_dchild)))
1501                 RETURN(-EBUSY);
1502
1503         op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1504                                      LUSTRE_OPC_ANY, NULL);
1505         if (IS_ERR(op_data))
1506                 RETURN(PTR_ERR(op_data));
1507
1508         if (src_dchild->d_inode != NULL)
1509                 op_data->op_fid3 = *ll_inode2fid(src_dchild->d_inode);
1510
1511         if (tgt_dchild->d_inode != NULL)
1512                 op_data->op_fid4 = *ll_inode2fid(tgt_dchild->d_inode);
1513
1514         err = md_rename(sbi->ll_md_exp, op_data,
1515                         src_name->name, src_name->len,
1516                         tgt_name->name, tgt_name->len, &request);
1517         ll_finish_md_op_data(op_data);
1518         if (!err) {
1519                 ll_update_times(request, src);
1520                 ll_update_times(request, tgt);
1521                 ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1522         }
1523
1524         ptlrpc_req_finished(request);
1525
1526         if (err == 0)
1527                 d_move(src_dchild, tgt_dchild);
1528
1529         RETURN(err);
1530 }
1531
1532 const struct inode_operations ll_dir_inode_operations = {
1533         .mknod          = ll_mknod,
1534 #ifdef HAVE_IOP_ATOMIC_OPEN
1535         .atomic_open    = ll_atomic_open,
1536 #endif
1537         .lookup         = ll_lookup_nd,
1538         .create         = ll_create_nd,
1539         /* We need all these non-raw things for NFSD, to not patch it. */
1540         .unlink         = ll_unlink,
1541         .mkdir          = ll_mkdir,
1542         .rmdir          = ll_rmdir,
1543         .symlink        = ll_symlink,
1544         .link           = ll_link,
1545         .rename         = ll_rename,
1546         .setattr        = ll_setattr,
1547         .getattr        = ll_getattr,
1548         .permission     = ll_inode_permission,
1549 #ifdef HAVE_IOP_XATTR
1550         .setxattr       = ll_setxattr,
1551         .getxattr       = ll_getxattr,
1552         .removexattr    = ll_removexattr,
1553 #endif
1554         .listxattr      = ll_listxattr,
1555 #ifdef HAVE_IOP_GET_ACL
1556         .get_acl        = ll_get_acl,
1557 #endif
1558 #ifdef HAVE_IOP_SET_ACL
1559         .set_acl        = ll_set_acl,
1560 #endif
1561 };
1562
1563 const struct inode_operations ll_special_inode_operations = {
1564         .setattr        = ll_setattr,
1565         .getattr        = ll_getattr,
1566         .permission     = ll_inode_permission,
1567 #ifdef HAVE_IOP_XATTR
1568         .setxattr       = ll_setxattr,
1569         .getxattr       = ll_getxattr,
1570         .removexattr    = ll_removexattr,
1571 #endif
1572         .listxattr      = ll_listxattr,
1573 #ifdef HAVE_IOP_GET_ACL
1574         .get_acl        = ll_get_acl,
1575 #endif
1576 #ifdef HAVE_IOP_SET_ACL
1577         .set_acl        = ll_set_acl,
1578 #endif
1579 };