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