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