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