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