Whamcloud - gitweb
- make HEAD from b_post_cmd3
[fs/lustre-release.git] / lustre / llite / namei.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp_lock.h>
26 #include <linux/quotaops.h>
27 #include <linux/highmem.h>
28 #include <linux/pagemap.h>
29
30 #define DEBUG_SUBSYSTEM S_LLITE
31
32 #include <obd_support.h>
33 #include <lustre_fid.h>
34 #include <lustre_lite.h>
35 #include <lustre_dlm.h>
36 #include <lustre_ver.h>
37 #include <lustre_mdc.h>
38 #include "llite_internal.h"
39
40 /* methods */
41 extern struct dentry_operations ll_d_ops;
42
43 /*
44  * Check if we have something mounted at the named dchild.
45  * In such a case there would always be dentry present.
46  */
47 static int ll_d_mountpoint(struct dentry *dparent, struct dentry *dchild,
48                            struct qstr *name)
49 {
50         int mounted = 0;
51
52         if (unlikely(dchild)) {
53                 mounted = d_mountpoint(dchild);
54         } else if (dparent) {
55                 dchild = d_lookup(dparent, name);
56                 if (dchild) {
57                         mounted = d_mountpoint(dchild);
58                         dput(dchild);
59                 }
60         }
61         return mounted;
62 }
63
64 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
65 {
66         ENTRY;
67
68         ldlm_lock_decref(lockh, mode);
69
70         RETURN(0);
71 }
72
73 /*
74  * Get an inode by inode number (already instantiated by the intent lookup).
75  * Returns inode or NULL
76  */
77 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
78 struct inode *ll_iget(struct super_block *sb, ino_t hash,
79                       struct lustre_md *md)
80 {
81         struct ll_inode_info *lli;
82         struct inode *inode;
83         LASSERT(hash != 0);
84
85         inode = iget_locked(sb, hash);
86         if (inode) {
87                 if (inode->i_state & I_NEW) {
88                         lli = ll_i2info(inode);
89                         ll_read_inode2(inode, md);
90                         unlock_new_inode(inode);
91                 } else {
92                         if (!(inode->i_state & (I_FREEING | I_CLEAR)))
93                                 ll_update_inode(inode, md);
94                 }
95                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
96                        inode->i_ino, inode->i_generation, inode);
97         }
98
99         return inode;
100 }
101 #else
102 struct inode *ll_iget(struct super_block *sb, ino_t hash,
103                       struct lustre_md *md)
104 {
105         struct inode *inode;
106         LASSERT(hash != 0);
107
108         inode = iget4(sb, hash, NULL, md);
109         if (inode) {
110                 if (!(inode->i_state & (I_FREEING | I_CLEAR)))
111                         ll_update_inode(inode, md);
112
113                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n",
114                        inode->i_ino, inode->i_generation, inode);
115         }
116         return inode;
117 }
118 #endif
119
120 static void ll_drop_negative_dentry(struct inode *dir)
121
122         struct dentry *dentry, *tmp_alias, *tmp_subdir;
123
124         spin_lock(&dcache_lock);
125 restart:
126         list_for_each_entry_safe(dentry, tmp_alias,
127                                  &dir->i_dentry,d_alias) {
128                 if (!list_empty(&dentry->d_subdirs)) {
129                         struct dentry *child;
130                         list_for_each_entry_safe(child, tmp_subdir,
131                                                  &dentry->d_subdirs,
132                                                  d_child) {
133                                 /* XXX Print some debug here? */
134                                 if (!child->d_inode)
135                                 /* Negative dentry. If we were
136                                    dropping dcache lock, go
137                                    throught the list again */
138                                         if (ll_drop_dentry(child))
139                                                 goto restart;
140                         }
141                 }
142         }
143         spin_unlock(&dcache_lock);
144 }
145
146
147 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
148                        void *data, int flag)
149 {
150         int rc;
151         struct lustre_handle lockh;
152         ENTRY;
153
154         switch (flag) {
155         case LDLM_CB_BLOCKING:
156                 ldlm_lock2handle(lock, &lockh);
157                 rc = ldlm_cli_cancel(&lockh);
158                 if (rc < 0) {
159                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
160                         RETURN(rc);
161                 }
162                 break;
163         case LDLM_CB_CANCELING: {
164                 struct inode *inode = ll_inode_from_lock(lock);
165                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
166                 struct lu_fid *fid;
167
168                 /* Invalidate all dentries associated with this inode */
169                 if (inode == NULL)
170                         break;
171
172                 fid = ll_inode2fid(inode);
173                 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
174                     lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
175                     lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
176                         LDLM_ERROR(lock, "data mismatch with object "
177                                    DFID" (%p)", PFID(fid), inode);
178                 }
179
180                 if (bits & MDS_INODELOCK_OPEN) {
181                         int flags = 0;
182                         switch (lock->l_req_mode) {
183                         case LCK_CW:
184                                 flags = FMODE_WRITE;
185                                 break;
186                         case LCK_PR:
187                                 flags = FMODE_EXEC;
188                                 break;
189                         case LCK_CR:
190                                 flags = FMODE_READ;
191                                 break;
192                         default:
193                                 CERROR("Unexpected lock mode for OPEN lock "
194                                        "%d, inode %ld\n", lock->l_req_mode,
195                                        inode->i_ino);
196                         }
197                         ll_md_real_close(inode, flags);
198                 }
199
200                 if (bits & MDS_INODELOCK_UPDATE)
201                         ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
202
203                 if (S_ISDIR(inode->i_mode) &&
204                      (bits & MDS_INODELOCK_UPDATE)) {
205                         CDEBUG(D_INODE, "invalidating inode %lu\n",
206                                inode->i_ino);
207                         truncate_inode_pages(inode->i_mapping, 0);
208                         ll_drop_negative_dentry(inode);
209                 }
210
211                 if (inode->i_sb->s_root &&
212                     inode != inode->i_sb->s_root->d_inode &&
213                     (bits & MDS_INODELOCK_LOOKUP))
214                         ll_unhash_aliases(inode);
215                 iput(inode);
216                 break;
217         }
218         default:
219                 LBUG();
220         }
221
222         RETURN(0);
223 }
224
225 /* Pack the required supplementary groups into the supplied groups array.
226  * If we don't need to use the groups from the target inode(s) then we
227  * instead pack one or more groups from the user's supplementary group
228  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
229 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
230 {
231         int i;
232
233         LASSERT(i1 != NULL);
234         LASSERT(suppgids != NULL);
235
236         if (in_group_p(i1->i_gid))
237                 suppgids[0] = i1->i_gid;
238         else
239                 suppgids[0] = -1;
240
241         if (i2) {
242                 if (in_group_p(i2->i_gid))
243                         suppgids[1] = i2->i_gid;
244                 else
245                         suppgids[1] = -1;
246         } else {
247                 suppgids[1] = -1;
248         }
249
250         for (i = 0; i < current_ngroups; i++) {
251                 if (suppgids[0] == -1) {
252                         if (current_groups[i] != suppgids[1])
253                                 suppgids[0] = current_groups[i];
254                         continue;
255                 }
256                 if (suppgids[1] == -1) {
257                         if (current_groups[i] != suppgids[0])
258                                 suppgids[1] = current_groups[i];
259                         continue;
260                 }
261                 break;
262         }
263 }
264
265 static void ll_d_add(struct dentry *de, struct inode *inode)
266 {
267         CDEBUG(D_DENTRY, "adding inode %p to dentry %p\n", inode, de);
268         /* d_instantiate */
269         if (!list_empty(&de->d_alias)) {
270                 spin_unlock(&dcache_lock);
271                 CERROR("dentry %.*s %p alias next %p, prev %p\n",
272                        de->d_name.len, de->d_name.name, de,
273                        de->d_alias.next, de->d_alias.prev);
274                 LBUG();
275         }
276         if (inode)
277                 list_add(&de->d_alias, &inode->i_dentry);
278         de->d_inode = inode;
279
280         /* d_rehash */
281         if (!d_unhashed(de)) {
282                 spin_unlock(&dcache_lock);
283                 CERROR("dentry %.*s %p hash next %p\n",
284                        de->d_name.len, de->d_name.name, de, de->d_hash.next);
285                 LBUG();
286         }
287         __d_rehash(de, 0);
288 }
289
290 /* Search "inode"'s alias list for a dentry that has the same name and parent
291  * as de.  If found, return it.  If not found, return de.
292  * Lustre can't use d_add_unique because don't unhash aliases for directory
293  * in ll_revalidate_it.  After revaliadate inode will be have hashed aliases
294  * and it triggers BUG_ON in d_instantiate_unique (bug #10954).
295  */
296 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
297 {
298         struct list_head *tmp;
299         struct dentry *dentry;
300         struct dentry *last_discon = NULL;
301  
302         spin_lock(&dcache_lock);
303         list_for_each(tmp, &inode->i_dentry) {
304                 dentry = list_entry(tmp, struct dentry, d_alias);
305
306                 /* We are called here with 'de' already on the aliases list. */
307                 if (unlikely(dentry == de)) {
308                         CERROR("whoops\n");
309                         continue;
310                 }
311
312                 if (dentry->d_flags & DCACHE_DISCONNECTED) {
313                         LASSERT(last_discon == NULL);
314                         last_discon = dentry;
315                         continue;
316                 }
317
318                 if (dentry->d_parent != de->d_parent)
319                         continue;
320
321                 if (dentry->d_name.hash != de->d_name.hash)
322                         continue;
323
324                 if (dentry->d_name.len != de->d_name.len)
325                         continue;
326
327                 if (memcmp(dentry->d_name.name, de->d_name.name,
328                            de->d_name.len) != 0)
329                         continue;
330
331                 dget_locked(dentry);
332                 lock_dentry(dentry);
333                 __d_drop(dentry);
334 #ifdef LUSTRE_KERNEL_VERSION
335                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
336 #endif
337                 unlock_dentry(dentry);
338                 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
339                 spin_unlock(&dcache_lock);
340                 iput(inode);
341                 CDEBUG(D_DENTRY, "alias dentry %.*s (%p) parent %p inode %p "
342                        "refc %d\n", de->d_name.len, de->d_name.name, de,
343                        de->d_parent, de->d_inode, atomic_read(&de->d_count));
344                 return dentry;
345         }
346         
347         if (last_discon) {
348                 CDEBUG(D_DENTRY, "Reuse disconnected dentry %p inode %p "
349                         "refc %d\n", last_discon, last_discon->d_inode,
350                         atomic_read(&last_discon->d_count));
351                 __d_rehash(de, 0);
352                 dget_locked(last_discon);
353                 __d_move(last_discon, de);
354                 spin_unlock(&dcache_lock);
355                 d_rehash(de);
356                 iput(inode);
357                 return last_discon;
358         }
359        
360         ll_d_add(de, inode);
361
362         spin_unlock(&dcache_lock);
363
364         return de;
365 }
366
367 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
368                             struct lookup_intent *it, void *data)
369 {
370         struct it_cb_data *icbd = data;
371         struct dentry **de = icbd->icbd_childp;
372         struct inode *parent = icbd->icbd_parent;
373         struct ll_sb_info *sbi = ll_i2sbi(parent);
374         struct inode *inode = NULL;
375         int rc;
376
377         /* NB 1 request reference will be taken away by ll_intent_lock()
378          * when I return */
379         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
380                 ENTRY;
381
382                 rc = ll_prep_inode(&inode, request, offset,
383                                    (*de)->d_sb);
384                 if (rc)
385                         RETURN(rc);
386
387                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
388                        inode, inode->i_ino, inode->i_generation);
389                 md_set_lock_data(sbi->ll_md_exp,
390                                  &it->d.lustre.it_lock_handle, inode);
391
392                 /* We used to query real size from OSTs here, but actually
393                    this is not needed. For stat() calls size would be updated
394                    from subsequent do_revalidate()->ll_inode_revalidate_it() in
395                    2.4 and
396                    vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
397                    Everybody else who needs correct file size would call
398                    ll_glimpse_size or some equivalent themselves anyway.
399                    Also see bug 7198. */
400
401                 *de = ll_find_alias(inode, *de);
402         } else {
403                 ENTRY;
404                 /* Check that parent has UPDATE lock. If there is none, we
405                    cannot afford to hash this dentry (done by ll_d_add) as it
406                    might get picked up later when UPDATE lock will appear */
407                 if (ll_have_md_lock(parent, MDS_INODELOCK_UPDATE)) {
408                         spin_lock(&dcache_lock);
409                         ll_d_add(*de, inode);
410                         spin_unlock(&dcache_lock);
411                 } else {
412                         (*de)->d_inode = NULL;
413                         /* We do not want to hash the dentry if don`t have a
414                          * lock, but if this dentry is later used in d_move,
415                          * we'd hit uninitialised list head d_hash, so we just
416                          * do this to init d_hash field but leave dentry
417                          * unhashed. (bug 10796). */
418                         d_rehash(*de);
419                         d_drop(*de);
420                 }
421         }
422
423         ll_set_dd(*de);
424         (*de)->d_op = &ll_d_ops;
425
426         RETURN(0);
427 }
428
429 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
430                                    struct lookup_intent *it, int lookup_flags)
431 {
432         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
433         struct dentry *save = dentry, *retval;
434         struct ptlrpc_request *req = NULL;
435         struct md_op_data *op_data;
436         struct it_cb_data icbd;
437         __u32 opc;
438         int rc;
439         ENTRY;
440
441         if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
442                 RETURN(ERR_PTR(-ENAMETOOLONG));
443
444         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
445                dentry->d_name.len, dentry->d_name.name, parent->i_ino,
446                parent->i_generation, parent, LL_IT2STR(it));
447
448         if (d_mountpoint(dentry))
449                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
450
451         ll_frob_intent(&it, &lookup_it);
452
453         /* As do_lookup is called before follow_mount, root dentry may be left
454          * not valid, revalidate it here. */
455         if (parent->i_sb->s_root && (parent->i_sb->s_root->d_inode == parent) &&
456             (it->it_op & (IT_OPEN | IT_CREAT))) {
457                 rc = ll_inode_revalidate_it(parent->i_sb->s_root, it);
458                 if (rc)
459                         RETURN(ERR_PTR(rc));
460         }
461
462         icbd.icbd_childp = &dentry;
463         icbd.icbd_parent = parent;
464
465         if (it->it_op & IT_CREAT ||
466             (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT))
467                 opc = LUSTRE_OPC_CREATE;
468         else
469                 opc = LUSTRE_OPC_ANY;
470
471         op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
472                                      dentry->d_name.len, lookup_flags, opc);
473         if (IS_ERR(op_data))
474                 RETURN((void *)op_data);
475
476         it->it_create_mode &= ~current->fs->umask;
477
478         rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
479                             lookup_flags, &req, ll_md_blocking_ast, 0);
480         ll_finish_md_op_data(op_data);
481         if (rc < 0)
482                 GOTO(out, retval = ERR_PTR(rc));
483
484         rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
485         if (rc != 0) {
486                 ll_intent_release(it);
487                 GOTO(out, retval = ERR_PTR(rc));
488         }
489
490         if ((it->it_op & IT_OPEN) && dentry->d_inode &&
491             !S_ISREG(dentry->d_inode->i_mode) &&
492             !S_ISDIR(dentry->d_inode->i_mode)) {
493                 ll_release_openhandle(dentry, it);
494         }
495         ll_lookup_finish_locks(it, dentry);
496
497         if (dentry == save)
498                 GOTO(out, retval = NULL);
499         else
500                 GOTO(out, retval = dentry);
501  out:
502         if (req)
503                 ptlrpc_req_finished(req);
504         return retval;
505 }
506
507 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
508 #ifdef LUSTRE_KERNEL_VERSION
509 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
510                                    struct nameidata *nd)
511 {
512         struct dentry *de;
513         ENTRY;
514
515         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
516                 de = ll_lookup_it(parent, dentry, &nd->intent, nd->flags);
517         else
518                 de = ll_lookup_it(parent, dentry, NULL, 0);
519
520         RETURN(de);
521 }
522 #else
523 struct lookup_intent *ll_convert_intent(struct open_intent *oit,
524                                         int lookup_flags)
525 {
526         struct lookup_intent *it;
527
528         OBD_ALLOC(it, sizeof(*it));
529         if (!it)
530                 return ERR_PTR(-ENOMEM);
531
532         if (lookup_flags & LOOKUP_OPEN) {
533                 it->it_op = IT_OPEN;
534                 if (lookup_flags & LOOKUP_CREATE)
535                         it->it_op |= IT_CREAT;
536                 it->it_create_mode = oit->create_mode;
537                 it->it_flags = oit->flags;
538         } else {
539                 it->it_op = IT_GETATTR;
540         }
541
542 #ifndef HAVE_FILE_IN_STRUCT_INTENT
543                 /* Since there is no way to pass our intent to ll_file_open,
544                  * just check the file is there. Actual open will be done
545                  * in ll_file_open */
546                 if (it->it_op & IT_OPEN)
547                         it->it_op = IT_LOOKUP;
548 #endif
549
550         return it;
551 }
552
553 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
554                                    struct nameidata *nd)
555 {
556         struct dentry *de;
557         ENTRY;
558
559         if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
560                 struct lookup_intent *it;
561
562 #if defined(HAVE_FILE_IN_STRUCT_INTENT) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
563                 /* Did we came here from failed revalidate just to propagate
564                  * its error? */
565                 if (nd->flags & LOOKUP_OPEN)
566                         if (IS_ERR(nd->intent.open.file))
567                                 RETURN((struct dentry *)nd->intent.open.file);
568 #endif
569
570                 if (ll_d2d(dentry) && ll_d2d(dentry)->lld_it) {
571                         it = ll_d2d(dentry)->lld_it;
572                         ll_d2d(dentry)->lld_it = NULL;
573                 } else {
574                         it = ll_convert_intent(&nd->intent.open, nd->flags);
575                         if (IS_ERR(it))
576                                 RETURN((struct dentry *)it);
577                 }
578
579                 de = ll_lookup_it(parent, dentry, it, nd->flags);
580                 if (de)
581                         dentry = de;
582                 if ((nd->flags & LOOKUP_OPEN) && !IS_ERR(dentry)) { /* Open */
583                         if (dentry->d_inode &&
584                             it_disposition(it, DISP_OPEN_OPEN)) { /* nocreate */
585 #ifdef HAVE_FILE_IN_STRUCT_INTENT
586                                 if (S_ISFIFO(dentry->d_inode->i_mode)) {
587                                         // We cannot call open here as it would
588                                         // deadlock.
589                                         ptlrpc_req_finished(
590                                                        (struct ptlrpc_request *)
591                                                           it->d.lustre.it_data);
592                                 } else {
593                                         struct file *filp;
594                                         nd->intent.open.file->private_data = it;
595                                         filp =lookup_instantiate_filp(nd,dentry,
596                                                                       NULL);
597 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
598 /* 2.6.1[456] have a bug in open_namei() that forgets to check
599  * nd->intent.open.file for error, so we need to return it as lookup's result
600  * instead */
601                                         if (IS_ERR(filp)) {
602                                                 if (de)
603                                                         dput(de);
604                                                 de = (struct dentry *) filp;
605                                         }
606 #endif
607
608                                 }
609 #else /* HAVE_FILE_IN_STRUCT_INTENT */
610                                 /* Release open handle as we have no way to
611                                  * pass it to ll_file_open */
612                                 ll_release_openhandle(dentry, it);
613 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
614                         } else if (it_disposition(it, DISP_OPEN_CREATE)) {
615                                 // XXX This can only reliably work on assumption
616                                 // that there are NO hashed negative dentries.
617                                 ll_d2d(dentry)->lld_it = it;
618                                 it = NULL; /* Will be freed in ll_create_nd */
619                                 /* We absolutely depend on ll_create_nd to be
620                                  * called to not leak this intent and possible
621                                  * data attached to it */
622                         }
623                 }
624
625                 if (it) {
626                         ll_intent_release(it);
627                         OBD_FREE(it, sizeof(*it));
628                 }
629         } else {
630                 de = ll_lookup_it(parent, dentry, NULL, 0);
631         }
632
633         RETURN(de);
634 }
635 #endif
636 #endif
637
638 /* We depend on "mode" being set with the proper file type/umask by now */
639 static struct inode *ll_create_node(struct inode *dir, const char *name,
640                                     int namelen, const void *data, int datalen,
641                                     int mode, __u64 extra,
642                                     struct lookup_intent *it)
643 {
644         struct inode *inode = NULL;
645         struct ptlrpc_request *request = NULL;
646         struct ll_sb_info *sbi = ll_i2sbi(dir);
647         int rc;
648         ENTRY;
649
650         LASSERT(it && it->d.lustre.it_disposition);
651
652         LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
653         request = it->d.lustre.it_data;
654         it_clear_disposition(it, DISP_ENQ_CREATE_REF);
655         rc = ll_prep_inode(&inode, request, DLM_REPLY_REC_OFF, dir->i_sb);
656         if (rc)
657                 GOTO(out, inode = ERR_PTR(rc));
658
659         LASSERT(list_empty(&inode->i_dentry));
660
661         /* We asked for a lock on the directory, but were granted a
662          * lock on the inode.  Since we finally have an inode pointer,
663          * stuff it in the lock. */
664         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
665                inode, inode->i_ino, inode->i_generation);
666         md_set_lock_data(sbi->ll_md_exp,
667                          &it->d.lustre.it_lock_handle, inode);
668         EXIT;
669  out:
670         ptlrpc_req_finished(request);
671         return inode;
672 }
673
674 /*
675  * By the time this is called, we already have created the directory cache
676  * entry for the new file, but it is so far negative - it has no inode.
677  *
678  * We defer creating the OBD object(s) until open, to keep the intent and
679  * non-intent code paths similar, and also because we do not have the MDS
680  * inode number before calling ll_create_node() (which is needed for LOV),
681  * so we would need to do yet another RPC to the MDS to store the LOV EA
682  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
683  * lmm_size in datalen (the MDS still has code which will handle that).
684  *
685  * If the create succeeds, we fill in the inode information
686  * with d_instantiate().
687  */
688 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
689                         struct lookup_intent *it)
690 {
691         struct inode *inode;
692         int rc = 0;
693         ENTRY;
694
695         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),intent=%s\n",
696                dentry->d_name.len, dentry->d_name.name, dir->i_ino,
697                dir->i_generation, dir, LL_IT2STR(it));
698
699         rc = it_open_error(DISP_OPEN_CREATE, it);
700         if (rc)
701                 RETURN(rc);
702
703         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
704                                NULL, 0, mode, 0, it);
705         if (IS_ERR(inode)) {
706                 RETURN(PTR_ERR(inode));
707         }
708
709         d_instantiate(dentry, inode);
710         /* Negative dentry may be unhashed if parent does not have UPDATE lock,
711          * but some callers, e.g. do_coredump, expect dentry to be hashed after
712          * successful create. Hash it here. */
713         spin_lock(&dcache_lock);
714         if (d_unhashed(dentry))
715                 __d_rehash(dentry, 0);
716         spin_unlock(&dcache_lock);
717         RETURN(0);
718 }
719
720 static void ll_update_times(struct ptlrpc_request *request, int offset,
721                             struct inode *inode)
722 {
723         struct mdt_body *body = lustre_msg_buf(request->rq_repmsg, offset,
724                                                sizeof(*body));
725         LASSERT(body);
726
727         /* mtime is always updated with ctime, but can be set in past.
728            As write and utime(2) may happen within 1 second, and utime's
729            mtime has a priority over write's one, so take mtime from mds
730            for the same ctimes. */
731         if (body->valid & OBD_MD_FLCTIME &&
732             body->ctime >= LTIME_S(inode->i_ctime)) {
733                 LTIME_S(inode->i_ctime) = body->ctime;
734
735                 if (body->valid & OBD_MD_FLMTIME) {
736                         CDEBUG(D_INODE, "setting ino %lu mtime from %lu "
737                                "to "LPU64"\n", inode->i_ino,
738                                LTIME_S(inode->i_mtime), body->mtime);
739                         LTIME_S(inode->i_mtime) = body->mtime;
740                 }
741         }
742 }
743
744 static int ll_new_node(struct inode *dir, struct qstr *name,
745                        const char *tgt, int mode, int rdev,
746                        struct dentry *dchild, __u32 opc)
747 {
748         struct ptlrpc_request *request = NULL;
749         struct md_op_data *op_data;
750         struct inode *inode = NULL;
751         struct ll_sb_info *sbi = ll_i2sbi(dir);
752         int tgt_len = 0;
753         int err;
754
755         ENTRY;
756         if (unlikely(tgt != NULL))
757                 tgt_len = strlen(tgt) + 1;
758
759         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
760                                      name->len, 0, opc);
761         if (IS_ERR(op_data))
762                 GOTO(err_exit, err = PTR_ERR(op_data));
763
764         err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
765                         current->fsuid, current->fsgid,
766                         current->cap_effective, rdev, &request);
767         ll_finish_md_op_data(op_data);
768         if (err)
769                 GOTO(err_exit, err);
770
771         ll_update_times(request, REPLY_REC_OFF, dir);
772
773         if (dchild) {
774                 err = ll_prep_inode(&inode, request, REPLY_REC_OFF,
775                                     dchild->d_sb);
776                 if (err)
777                      GOTO(err_exit, err);
778
779                 d_drop(dchild);
780                 d_instantiate(dchild, inode);
781                 EXIT;
782         }
783 err_exit:
784         ptlrpc_req_finished(request);
785
786         return err;
787 }
788
789 static int ll_mknod_generic(struct inode *dir, struct qstr *name, int mode,
790                             unsigned rdev, struct dentry *dchild)
791 {
792         int err;
793         ENTRY;
794
795         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p) mode %o dev %x\n",
796                name->len, name->name, dir->i_ino, dir->i_generation, dir,
797                mode, rdev);
798
799         mode &= ~current->fs->umask;
800
801         switch (mode & S_IFMT) {
802         case 0:
803                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
804         case S_IFREG:
805         case S_IFCHR:
806         case S_IFBLK:
807         case S_IFIFO:
808         case S_IFSOCK:
809                 err = ll_new_node(dir, name, NULL, mode, rdev, dchild,
810                                   LUSTRE_OPC_MKNOD);
811                 break;
812         case S_IFDIR:
813                 err = -EPERM;
814                 break;
815         default:
816                 err = -EINVAL;
817         }
818         RETURN(err);
819 }
820
821 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
822 #ifndef LUSTRE_KERNEL_VERSION
823 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
824                         int mode, struct nameidata *nd)
825 {
826         struct lookup_intent *it = ll_d2d(dentry)->lld_it;
827         int rc;
828
829         if (!it)
830                 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
831
832         ll_d2d(dentry)->lld_it = NULL;
833
834         /* Was there an error? Propagate it! */
835         if (it->d.lustre.it_status) {
836                 rc = it->d.lustre.it_status;
837                 goto out;
838         }
839
840         rc = ll_create_it(dir, dentry, mode, it);
841 #ifdef HAVE_FILE_IN_STRUCT_INTENT
842         if (nd && (nd->flags & LOOKUP_OPEN) && dentry->d_inode) { /* Open */
843                 nd->intent.open.file->private_data = it;
844                 lookup_instantiate_filp(nd, dentry, NULL);
845         }
846 #else
847         ll_release_openhandle(dentry,it);
848 #endif
849
850 out:
851         ll_intent_release(it);
852         OBD_FREE(it, sizeof(*it));
853
854         return rc;
855 }
856 #else
857 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
858                         int mode, struct nameidata *nd)
859 {
860         if (!nd || !nd->intent.d.lustre.it_disposition)
861                 /* No saved request? Just mknod the file */
862                 return ll_mknod_generic(dir, &dentry->d_name, mode, 0, dentry);
863
864         return ll_create_it(dir, dentry, mode, &nd->intent);
865 }
866 #endif
867 #endif
868
869 static int ll_symlink_generic(struct inode *dir, struct qstr *name,
870                               const char *tgt, struct dentry *dchild)
871 {
872         int err;
873         ENTRY;
874
875         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p),target=%.*s\n",
876                name->len, name->name, dir->i_ino, dir->i_generation,
877                dir, 3000, tgt);
878
879         err = ll_new_node(dir, name, (char *)tgt, S_IFLNK | S_IRWXUGO,
880                           0, dchild, LUSTRE_OPC_SYMLINK);
881         RETURN(err);
882 }
883
884 static int ll_link_generic(struct inode *src,  struct inode *dir,
885                            struct qstr *name, struct dentry *dchild)
886 {
887         struct ll_sb_info *sbi = ll_i2sbi(dir);
888         struct ptlrpc_request *request = NULL;
889         struct md_op_data *op_data;
890         int err;
891
892         ENTRY;
893         CDEBUG(D_VFSTRACE,
894                "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%.*s\n",
895                src->i_ino, src->i_generation, src, dir->i_ino,
896                dir->i_generation, dir, name->len, name->name);
897
898         op_data = ll_prep_md_op_data(NULL, src, dir, name->name, name->len,
899                                      0, LUSTRE_OPC_ANY);
900         if (IS_ERR(op_data))
901                 RETURN(PTR_ERR(op_data));
902
903         err = md_link(sbi->ll_md_exp, op_data, &request);
904         ll_finish_md_op_data(op_data);
905         if (err)
906                 GOTO(out, err);
907         if (dchild)
908                 d_drop(dchild);
909
910         ll_update_times(request, REPLY_REC_OFF, dir);
911         EXIT;
912 out:
913         ptlrpc_req_finished(request);
914         RETURN(err);
915 }
916
917 static int ll_mkdir_generic(struct inode *dir, struct qstr *name,
918                             int mode, struct dentry *dchild)
919
920 {
921         int err;
922         ENTRY;
923
924         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
925                name->len, name->name, dir->i_ino, dir->i_generation, dir);
926
927         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
928         err = ll_new_node(dir, name, NULL, mode, 0, dchild, LUSTRE_OPC_MKDIR);
929
930         RETURN(err);
931 }
932
933 static int ll_rmdir_generic(struct inode *dir, struct dentry *dparent,
934                             struct dentry *dchild, struct qstr *name)
935 {
936         struct ptlrpc_request *request = NULL;
937         struct md_op_data *op_data;
938         int rc;
939         ENTRY;
940         
941         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
942                name->len, name->name, dir->i_ino, dir->i_generation, dir);
943
944         if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
945                 RETURN(-EBUSY);
946
947         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name, name->len,
948                                      S_IFDIR, LUSTRE_OPC_ANY);
949         if (IS_ERR(op_data))
950                 RETURN(PTR_ERR(op_data));
951
952         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
953         ll_finish_md_op_data(op_data);
954         if (rc == 0)
955                 ll_update_times(request, REPLY_REC_OFF, dir);
956         ptlrpc_req_finished(request);
957         RETURN(rc);
958 }
959
960 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
961 {
962         struct mdt_body *body;
963         struct lov_mds_md *eadata;
964         struct lov_stripe_md *lsm = NULL;
965         struct obd_trans_info oti = { 0 };
966         struct obdo *oa;
967         int rc;
968         ENTRY;
969
970         /* req is swabbed so this is safe */
971         body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
972
973         if (!(body->valid & OBD_MD_FLEASIZE))
974                 RETURN(0);
975
976         if (body->eadatasize == 0) {
977                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
978                 GOTO(out, rc = -EPROTO);
979         }
980
981         /* The MDS sent back the EA because we unlinked the last reference
982          * to this file. Use this EA to unlink the objects on the OST.
983          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
984          * check it is complete and sensible. */
985         eadata = lustre_swab_repbuf(request, REPLY_REC_OFF + 1,
986                                     body->eadatasize, NULL);
987         LASSERT(eadata != NULL);
988         if (eadata == NULL) {
989                 CERROR("Can't unpack MDS EA data\n");
990                 GOTO(out, rc = -EPROTO);
991         }
992
993         rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
994         if (rc < 0) {
995                 CERROR("obd_unpackmd: %d\n", rc);
996                 GOTO(out, rc);
997         }
998         LASSERT(rc >= sizeof(*lsm));
999
1000         rc = obd_checkmd(ll_i2dtexp(dir), ll_i2mdexp(dir), lsm);
1001         if (rc)
1002                 GOTO(out_free_memmd, rc);
1003
1004         OBDO_ALLOC(oa);
1005         if (oa == NULL)
1006                 GOTO(out_free_memmd, rc = -ENOMEM);
1007
1008         oa->o_id = lsm->lsm_object_id;
1009         oa->o_gr = lsm->lsm_object_gr;
1010         oa->o_mode = body->mode & S_IFMT;
1011         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
1012
1013         if (body->valid & OBD_MD_FLCOOKIE) {
1014                 oa->o_valid |= OBD_MD_FLCOOKIE;
1015                 oti.oti_logcookies =
1016                         lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF + 2,
1017                                        sizeof(struct llog_cookie) *
1018                                        lsm->lsm_stripe_count);
1019                 if (oti.oti_logcookies == NULL) {
1020                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
1021                         body->valid &= ~OBD_MD_FLCOOKIE;
1022                 }
1023         }
1024
1025         rc = obd_destroy(ll_i2dtexp(dir), oa, lsm, &oti, ll_i2mdexp(dir));
1026         OBDO_FREE(oa);
1027         if (rc)
1028                 CERROR("obd destroy objid "LPX64" error %d\n",
1029                        lsm->lsm_object_id, rc);
1030  out_free_memmd:
1031         obd_free_memmd(ll_i2dtexp(dir), &lsm);
1032  out:
1033         return rc;
1034 }
1035
1036 static int ll_unlink_generic(struct inode *dir, struct dentry *dparent,
1037                              struct dentry *dchild, struct qstr *name)
1038 {
1039         struct ptlrpc_request *request = NULL;
1040         struct md_op_data *op_data;
1041         int rc;
1042         ENTRY;
1043         CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,dir=%lu/%u(%p)\n",
1044                name->len, name->name, dir->i_ino, dir->i_generation, dir);
1045
1046         /*
1047          * XXX: unlink bind mountpoint maybe call to here,
1048          * just check it as vfs_unlink does.
1049          */
1050         if (unlikely(ll_d_mountpoint(dparent, dchild, name)))
1051                 RETURN(-EBUSY);
1052
1053         op_data = ll_prep_md_op_data(NULL, dir, NULL, name->name,
1054                                      name->len, 0, LUSTRE_OPC_ANY);
1055         if (IS_ERR(op_data))
1056                 RETURN(PTR_ERR(op_data));
1057
1058         rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1059         ll_finish_md_op_data(op_data);
1060
1061         if (rc)
1062                 GOTO(out, rc);
1063
1064         ll_update_times(request, REPLY_REC_OFF, dir);
1065
1066         rc = ll_objects_destroy(request, dir);
1067  out:
1068         ptlrpc_req_finished(request);
1069         RETURN(rc);
1070 }
1071
1072 static int ll_rename_generic(struct inode *src, struct dentry *src_dparent,
1073                              struct dentry *src_dchild, struct qstr *src_name,
1074                              struct inode *tgt, struct dentry *tgt_dparent,
1075                              struct dentry *tgt_dchild, struct qstr *tgt_name)
1076 {
1077         struct ptlrpc_request *request = NULL;
1078         struct ll_sb_info *sbi = ll_i2sbi(src);
1079         struct md_op_data *op_data;
1080         int err;
1081         ENTRY;
1082         CDEBUG(D_VFSTRACE,"VFS Op:oldname=%.*s,src_dir=%lu/%u(%p),newname=%.*s,"
1083                "tgt_dir=%lu/%u(%p)\n", src_name->len, src_name->name,
1084                src->i_ino, src->i_generation, src, tgt_name->len,
1085                tgt_name->name, tgt->i_ino, tgt->i_generation, tgt);
1086
1087         if (unlikely(ll_d_mountpoint(src_dparent, src_dchild, src_name) ||
1088             ll_d_mountpoint(tgt_dparent, tgt_dchild, tgt_name)))
1089                 RETURN(-EBUSY);
1090
1091         op_data = ll_prep_md_op_data(NULL, src, tgt, NULL, 0, 0,
1092                                      LUSTRE_OPC_ANY);
1093         if (IS_ERR(op_data))
1094                 RETURN(PTR_ERR(op_data));
1095
1096         err = md_rename(sbi->ll_md_exp, op_data,
1097                         src_name->name, src_name->len,
1098                         tgt_name->name, tgt_name->len, &request);
1099         ll_finish_md_op_data(op_data);
1100         if (!err) {
1101                 ll_update_times(request, REPLY_REC_OFF, src);
1102                 ll_update_times(request, REPLY_REC_OFF, tgt);
1103                 err = ll_objects_destroy(request, src);
1104         }
1105
1106         ptlrpc_req_finished(request);
1107
1108         RETURN(err);
1109 }
1110
1111 #ifdef LUSTRE_KERNEL_VERSION
1112 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
1113 {
1114         return ll_mknod_generic(nd->dentry->d_inode, &nd->last, mode,rdev,NULL);
1115 }
1116 static int ll_rename_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1117 {
1118         return ll_rename_generic(srcnd->dentry->d_inode, srcnd->dentry,
1119                                  NULL, &srcnd->last,
1120                                  tgtnd->dentry->d_inode, tgtnd->dentry,
1121                                  NULL, &tgtnd->last);
1122 }
1123 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
1124 {
1125         return ll_link_generic(srcnd->dentry->d_inode, tgtnd->dentry->d_inode,
1126                                &tgtnd->last, NULL);
1127 }
1128 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
1129 {
1130         return ll_symlink_generic(nd->dentry->d_inode, &nd->last, tgt, NULL);
1131 }
1132 static int ll_rmdir_raw(struct nameidata *nd)
1133 {
1134         return ll_rmdir_generic(nd->dentry->d_inode, nd->dentry, NULL,
1135                                 &nd->last);
1136 }
1137 static int ll_mkdir_raw(struct nameidata *nd, int mode)
1138 {
1139         return ll_mkdir_generic(nd->dentry->d_inode, &nd->last, mode, NULL);
1140 }
1141 static int ll_unlink_raw(struct nameidata *nd)
1142 {
1143         return ll_unlink_generic(nd->dentry->d_inode, nd->dentry, NULL,
1144                                  &nd->last);
1145 }
1146 #endif
1147
1148 static int ll_mknod(struct inode *dir, struct dentry *dchild, int mode,
1149                     ll_dev_t rdev)
1150 {
1151         return ll_mknod_generic(dir, &dchild->d_name, mode,
1152                                 old_encode_dev(rdev), dchild);
1153 }
1154
1155 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1156 static int ll_unlink(struct inode * dir, struct dentry *dentry)
1157 {
1158         return ll_unlink_generic(dir, NULL, dentry, &dentry->d_name);
1159 }
1160 static int ll_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1161 {
1162         return ll_mkdir_generic(dir, &dentry->d_name, mode, dentry);
1163 }
1164 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
1165 {
1166         return ll_rmdir_generic(dir, NULL, dentry, &dentry->d_name);
1167 }
1168 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1169                       const char *oldname)
1170 {
1171         return ll_symlink_generic(dir, &dentry->d_name, oldname, dentry);
1172 }
1173 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1174                    struct dentry *new_dentry)
1175 {
1176         return ll_link_generic(old_dentry->d_inode, dir, &new_dentry->d_name,
1177                                new_dentry);
1178 }
1179 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1180                      struct inode *new_dir, struct dentry *new_dentry)
1181 {
1182         return ll_rename_generic(old_dir, NULL,
1183                                  old_dentry, &old_dentry->d_name,
1184                                  new_dir, NULL, new_dentry,
1185                                  &new_dentry->d_name);
1186 }
1187 #endif
1188
1189 struct inode_operations ll_dir_inode_operations = {
1190 #ifdef LUSTRE_KERNEL_VERSION
1191         .link_raw           = ll_link_raw,
1192         .unlink_raw         = ll_unlink_raw,
1193         .symlink_raw        = ll_symlink_raw,
1194         .mkdir_raw          = ll_mkdir_raw,
1195         .rmdir_raw          = ll_rmdir_raw,
1196         .mknod_raw          = ll_mknod_raw,
1197         .rename_raw         = ll_rename_raw,
1198         .setattr            = ll_setattr,
1199         .setattr_raw        = ll_setattr_raw,
1200 #endif
1201         .mknod              = ll_mknod,
1202 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1203         .create_it          = ll_create_it,
1204         .lookup_it          = ll_lookup_it,
1205         .revalidate_it      = ll_inode_revalidate_it,
1206 #else
1207         .lookup             = ll_lookup_nd,
1208         .create             = ll_create_nd,
1209         /* We need all these non-raw things for NFSD, to not patch it. */
1210         .unlink             = ll_unlink,
1211         .mkdir              = ll_mkdir,
1212         .rmdir              = ll_rmdir,
1213         .symlink            = ll_symlink,
1214         .link               = ll_link,
1215         .rename             = ll_rename,
1216         .setattr            = ll_setattr,
1217         .getattr            = ll_getattr,
1218 #endif
1219         .permission         = ll_inode_permission,
1220         .setxattr           = ll_setxattr,
1221         .getxattr           = ll_getxattr,
1222         .listxattr          = ll_listxattr,
1223         .removexattr        = ll_removexattr,
1224 };
1225
1226 struct inode_operations ll_special_inode_operations = {
1227 #ifdef LUSTRE_KERNEL_VERSION
1228         .setattr_raw    = ll_setattr_raw,
1229 #endif
1230         .setattr        = ll_setattr,
1231 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1232         .getattr        = ll_getattr,
1233 #else
1234         .revalidate_it  = ll_inode_revalidate_it,
1235 #endif
1236         .permission     = ll_inode_permission,
1237         .setxattr       = ll_setxattr,
1238         .getxattr       = ll_getxattr,
1239         .listxattr      = ll_listxattr,
1240         .removexattr    = ll_removexattr,
1241 };