Whamcloud - gitweb
a16814bbb8565f1000ebbd99d3d6469eb7b88b5e
[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  *  derived in small part from linux/fs/ext2/namei.c
22  *
23  *  Copyright (C) 1991, 1992  Linus Torvalds
24  *
25  *  Big-endian to little-endian byte-swapping/bitmaps by
26  *        David S. Miller (davem@caip.rutgers.edu), 1995
27  *  Directory entry file type support and forward compatibility hooks
28  *      for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
29  */
30
31 #include <linux/fs.h>
32 #include <linux/sched.h>
33 #include <linux/mm.h>
34 #include <linux/smp_lock.h>
35 #include <linux/quotaops.h>
36 #include <linux/highmem.h>
37 #include <linux/pagemap.h>
38
39 #define DEBUG_SUBSYSTEM S_LLITE
40
41 #include <linux/obd_support.h>
42 #include <linux/lustre_lite.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_version.h>
45 #include "llite_internal.h"
46
47 /* methods */
48
49 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
50 static int ll_test_inode(struct inode *inode, unsigned long ino, void *opaque)
51 #else
52 static int ll_test_inode(struct inode *inode, void *opaque)
53 #endif
54 {
55         static int last_ino, last_gen, last_count;
56         struct lustre_md *md = opaque;
57
58         if (!(md->body->valid & (OBD_MD_FLGENER | OBD_MD_FLID))) {
59                 CERROR("MDS body missing inum or generation\n");
60                 return 0;
61         }
62
63         if (last_ino == md->body->ino && last_gen == md->body->generation &&
64             last_count < 500) {
65                 last_count++;
66         } else {
67                 if (last_count > 1)
68                         CDEBUG(D_VFSTRACE, "compared %u/%u %u times\n",
69                                last_ino, last_gen, last_count);
70                 last_count = 0;
71                 last_ino = md->body->ino;
72                 last_gen = md->body->generation;
73                 CDEBUG(D_VFSTRACE,
74                        "comparing inode %p ino %lu/%u/%u to body %u/%u/%u\n",
75                        inode, inode->i_ino, inode->i_generation,
76                        ll_i2info(inode)->lli_mds,
77                        md->body->ino, md->body->generation,
78                        md->body->mds);
79         }
80
81 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
82         if (inode->i_ino != md->body->ino)
83                 return 0;
84 #endif
85         if (inode->i_generation != md->body->generation)
86                 return 0;
87
88         if (ll_i2info(inode)->lli_mds != md->body->mds)
89                 return 0;
90 #ifdef CONFIG_SNAPFS
91         if (ll_i2info(inode)->lli_snap_index != md->body->fid1.snap_index)
92                 return 0;
93 #endif
94         /* Apply the attributes in 'opaque' to this inode */
95         ll_update_inode(inode, md);
96         return 1;
97 }
98
99 extern struct dentry_operations ll_d_ops;
100
101 int ll_unlock(__u32 mode, struct lustre_handle *lockh)
102 {
103         ENTRY;
104
105         ldlm_lock_decref(lockh, mode);
106
107         RETURN(0);
108 }
109
110 /* Get an inode by inode number (already instantiated by the intent lookup).
111  * Returns inode or NULL
112  */
113 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
114 int ll_set_inode(struct inode *inode, void *opaque)
115 {
116         ll_read_inode2(inode, opaque);
117         return 0;
118 }
119
120 struct inode *ll_iget(struct super_block *sb, ino_t hash,
121                       struct lustre_md *md)
122 {
123         struct inode *inode;
124
125         LASSERT(hash != 0);
126         inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
127
128         if (inode) {
129                 if (inode->i_state & I_NEW)
130                         unlock_new_inode(inode);
131                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
132                        inode->i_generation, inode);
133         }
134
135         return inode;
136 }
137 #else
138 struct inode *ll_iget(struct super_block *sb, ino_t hash,
139                       struct lustre_md *md)
140 {
141         struct inode *inode;
142         LASSERT(hash != 0);
143         inode = iget4(sb, hash, ll_test_inode, md);
144         if (inode)
145                 CDEBUG(D_VFSTRACE, "inode: %lu/%u(%p)\n", inode->i_ino,
146                        inode->i_generation, inode);
147         return inode;
148 }
149 #endif
150
151 int ll_mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
152                         void *data, int flag)
153 {
154         int rc;
155         struct lustre_handle lockh;
156         ENTRY;
157
158         switch (flag) {
159         case LDLM_CB_BLOCKING:
160                 ldlm_lock2handle(lock, &lockh);
161                 rc = ldlm_cli_cancel(&lockh);
162                 if (rc < 0) {
163                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
164                         RETURN(rc);
165                 }
166                 break;
167         case LDLM_CB_CANCELING: {
168                 struct inode *inode = ll_inode_from_lock(lock);
169                 __u64 bits = lock->l_policy_data.l_inodebits.bits;
170
171                 /* For lookup locks: Invalidate all dentries associated with
172                    this inode, for UPDATE locks - invalidate directory pages */
173                 if (inode == NULL)
174                         break;
175
176                 if (bits & MDS_INODELOCK_UPDATE)
177                         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK,
178                                   &(ll_i2info(inode)->lli_flags));
179
180
181                 if (lock->l_resource->lr_name.name[0] != inode->i_ino ||
182                     lock->l_resource->lr_name.name[1] != inode->i_generation) {
183                         LDLM_ERROR(lock, "data mismatch with ino %lu/%u",
184                                    inode->i_ino, inode->i_generation);
185                 }
186
187                 /* If lookup lock is cancelled, we just drop the dentry and
188                    this will cause us to reget data from MDS when we'd want to
189                    access this dentry/inode again. If this is lock on
190                    other parts of inode that is cancelled, we do not need to do
191                    much (but need to discard data from readdir, if any), since
192                    abscence of lock will cause ll_revalidate_it (called from
193                    stat() and similar functions) to renew the data anyway */
194                 if (S_ISDIR(inode->i_mode) &&
195                     (bits & MDS_INODELOCK_UPDATE)) {
196                         CDEBUG(D_INODE, "invalidating inode %lu\n",
197                                inode->i_ino);
198
199                         truncate_inode_pages(inode->i_mapping, 0);
200                 }
201
202                 if (inode->i_sb->s_root &&
203                     inode != inode->i_sb->s_root->d_inode &&
204                     (bits & MDS_INODELOCK_LOOKUP))
205                         ll_unhash_aliases(inode);
206                 iput(inode);
207                 break;
208         }
209         default:
210                 LBUG();
211         }
212
213         RETURN(0);
214 }
215
216 int ll_mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
217                          int flags, void *opaque)
218 {
219         struct ldlm_res_id res_id =
220                 { .name = {inode->i_ino, inode->i_generation} };
221         struct obd_device *obddev = class_conn2obd(conn);
222         ENTRY;
223         
224         RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, &res_id, flags,
225                                       opaque));
226 }
227
228 /* Search "inode"'s alias list for a dentry that has the same name and parent as
229  * de.  If found, return it.  If not found, return de. */
230 struct dentry *ll_find_alias(struct inode *inode, struct dentry *de)
231 {
232         struct list_head *tmp;
233
234         spin_lock(&dcache_lock);
235         list_for_each(tmp, &inode->i_dentry) {
236                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
237
238                 /* We are called here with 'de' already on the aliases list. */
239                 if (dentry == de) {
240                         CERROR("whoops\n");
241                         continue;
242                 }
243
244                 if (dentry->d_parent != de->d_parent)
245                         continue;
246
247                 if (dentry->d_name.len != de->d_name.len)
248                         continue;
249
250                 if (memcmp(dentry->d_name.name, de->d_name.name,
251                            de->d_name.len) != 0)
252                         continue;
253
254                 if (!list_empty(&dentry->d_lru))
255                         list_del_init(&dentry->d_lru);
256
257                 hlist_del_init(&dentry->d_hash);
258                 __d_rehash(dentry, 0); /* avoid taking dcache_lock inside */
259                 spin_unlock(&dcache_lock);
260                 atomic_inc(&dentry->d_count);
261                 iput(inode);
262                 dentry->d_flags &= ~DCACHE_LUSTRE_INVALID;
263                 CDEBUG(D_DENTRY, "alias dentry %*s (%p) parent %p inode %p "
264                        "refc %d\n", de->d_name.len, de->d_name.name, de,
265                        de->d_parent, de->d_inode, atomic_read(&de->d_count));
266                 return dentry;
267         }
268
269         spin_unlock(&dcache_lock);
270
271         return de;
272 }
273
274 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
275                             struct lookup_intent *it, void *data)
276 {
277         struct it_cb_data *icbd = data;
278         struct dentry **de = icbd->icbd_childp;
279         struct inode *parent = icbd->icbd_parent;
280         struct ll_sb_info *sbi = ll_i2sbi(parent);
281         struct dentry *dentry = *de, *saved = *de;
282         struct inode *inode = NULL;
283         int rc;
284
285         /* NB 1 request reference will be taken away by ll_intent_lock()
286          * when I return */
287         if (!it_disposition(it, DISP_LOOKUP_NEG)) {
288                 ENTRY;
289
290                 rc = ll_prep_inode(sbi->ll_osc_exp, sbi->ll_mdc_exp,
291                                    &inode, request, offset, dentry->d_sb);
292                 if (rc)
293                         RETURN(rc);
294
295                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
296                        inode, inode->i_ino, inode->i_generation);
297                 mdc_set_lock_data(NULL, &it->d.lustre.it_lock_handle, inode);
298
299                 /* If this is a stat, get the authoritative file size */
300                 if (it->it_op == IT_GETATTR && S_ISREG(inode->i_mode) &&
301                     ll_i2info(inode)->lli_smd != NULL) {
302                         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
303                         struct ost_lvb lvb;
304                         ldlm_error_t rc;
305
306                         LASSERT(lsm->lsm_object_id != 0);
307
308                         /* bug 2334: drop MDS lock before acquiring OST lock */
309                         ll_intent_drop_lock(it);
310
311                         rc = ll_glimpse_size(inode, &lvb);
312                         if (rc) {
313                                 iput(inode);
314                                 RETURN(rc);
315                         }
316                         inode->i_size = lvb.lvb_size;
317                 }
318
319                 dentry = *de = ll_find_alias(inode, dentry);
320         } else {
321                 ENTRY;
322         }
323
324         dentry->d_op = &ll_d_ops;
325         ll_set_dd(dentry);
326
327         if (dentry == saved) {
328                 d_add(dentry, inode);
329         }
330
331         RETURN(0);
332 }
333
334
335 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
336                                    struct nameidata *nd,
337                                    struct lookup_intent *it, int flags)
338 {
339         struct dentry *save = dentry, *retval;
340         struct ll_fid pfid;
341         struct ll_uctxt ctxt;
342         struct it_cb_data icbd;
343         struct ptlrpc_request *req = NULL;
344         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
345         int rc;
346         ENTRY;
347
348         if (dentry->d_name.len > EXT3_NAME_LEN)
349                 RETURN(ERR_PTR(-ENAMETOOLONG));
350
351         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p),intent=%s\n",
352                dentry->d_name.name, parent->i_ino, parent->i_generation,
353                parent, LL_IT2STR(it));
354
355         if (d_mountpoint(dentry))
356                 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
357
358         if (nd != NULL)
359                 nd->mnt->mnt_last_used = jiffies;
360
361         ll_frob_intent(&it, &lookup_it);
362
363         icbd.icbd_childp = &dentry;
364         icbd.icbd_parent = parent;
365         ll_inode2fid(&pfid, parent);
366         ll_i2uctxt(&ctxt, parent, NULL);
367
368         rc = md_intent_lock(ll_i2mdcexp(parent), &ctxt, &pfid,
369                             dentry->d_name.name, dentry->d_name.len, NULL, 0,
370                             NULL, it, flags, &req, ll_mdc_blocking_ast);
371         if (rc < 0)
372                 GOTO(out, retval = ERR_PTR(rc));
373
374         rc = lookup_it_finish(req, 1, it, &icbd);
375         if (rc != 0) {
376                 ll_intent_release(it);
377                 GOTO(out, retval = ERR_PTR(rc));
378         }
379
380         ll_lookup_finish_locks(it, dentry);
381
382         if (nd &&
383             dentry->d_inode != NULL && dentry->d_inode->i_mode & S_ISUID &&
384             S_ISDIR(dentry->d_inode->i_mode) &&
385             (flags & LOOKUP_CONTINUE || (it->it_op & (IT_CHDIR | IT_OPEN))))
386                 ll_dir_process_mount_object(dentry, nd->mnt);
387
388         if (dentry == save)
389                 GOTO(out, retval = NULL);
390         else
391                 GOTO(out, retval = dentry);
392  out:
393         if (req)
394                 ptlrpc_req_finished(req);
395         if (dentry->d_inode)
396                 CDEBUG(D_INODE, "lookup 0x%p in %lu/%lu: %*s -> %lu/%lu\n",
397                                 dentry,
398                                 (unsigned long) parent->i_ino,
399                                 (unsigned long) parent->i_generation,
400                                 dentry->d_name.len, dentry->d_name.name,
401                                 (unsigned long) dentry->d_inode->i_ino,
402                                 (unsigned long) dentry->d_inode->i_generation);
403         else
404                 CDEBUG(D_INODE, "lookup 0x%p in %lu/%lu: %*s -> ??\n",
405                                 dentry,
406                                 (unsigned long) parent->i_ino,
407                                 (unsigned long) parent->i_generation,
408                                 dentry->d_name.len, dentry->d_name.name);
409         return retval;
410 }
411
412 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
413 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
414                                    struct nameidata *nd)
415 {
416         struct dentry *de;
417         ENTRY;
418
419         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
420                 de = ll_lookup_it(parent, dentry, nd, &nd->intent, nd->flags);
421         else
422                 de = ll_lookup_it(parent, dentry, nd, NULL, 0);
423
424         RETURN(de);
425 }
426 #endif
427
428 /* We depend on "mode" being set with the proper file type/umask by now */
429 static struct inode *ll_create_node(struct inode *dir, const char *name,
430                                     int namelen, const void *data, int datalen,
431                                     int mode, __u64 extra,
432                                     struct lookup_intent *it)
433 {
434         struct inode *inode = NULL;
435         struct ptlrpc_request *request = NULL;
436         struct ll_sb_info *sbi = ll_i2sbi(dir);
437         int rc;
438         ENTRY;
439
440         LASSERT(it && it->d.lustre.it_disposition);
441
442         request = it->d.lustre.it_data;
443         rc = ll_prep_inode(sbi->ll_osc_exp, sbi->ll_mdc_exp,
444                            &inode, request, 1, dir->i_sb);
445         if (rc)
446                 GOTO(out, inode = ERR_PTR(rc));
447
448         LASSERT(list_empty(&inode->i_dentry));
449
450         /* We asked for a lock on the directory, but were granted a
451          * lock on the inode.  Since we finally have an inode pointer,
452          * stuff it in the lock. */
453         CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
454                inode, inode->i_ino, inode->i_generation);
455         mdc_set_lock_data(NULL, &it->d.lustre.it_lock_handle, inode);
456         EXIT;
457  out:
458         ptlrpc_req_finished(request);
459         return inode;
460 }
461
462 /*
463  * By the time this is called, we already have created the directory cache
464  * entry for the new file, but it is so far negative - it has no inode.
465  *
466  * We defer creating the OBD object(s) until open, to keep the intent and
467  * non-intent code paths similar, and also because we do not have the MDS
468  * inode number before calling ll_create_node() (which is needed for LOV),
469  * so we would need to do yet another RPC to the MDS to store the LOV EA
470  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
471  * lmm_size in datalen (the MDS still has code which will handle that).
472  *
473  * If the create succeeds, we fill in the inode information
474  * with d_instantiate().
475  */
476 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
477                         struct lookup_intent *it)
478 {
479         struct inode *inode;
480         struct ptlrpc_request *request = it->d.lustre.it_data;
481         struct obd_export *mdc_exp = ll_i2mdcexp(dir); 
482         int rc = 0;
483         ENTRY;
484
485         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p),intent=%s\n",
486                dentry->d_name.name, dir->i_ino, dir->i_generation, dir,
487                LL_IT2STR(it));
488
489         rc = it_open_error(DISP_OPEN_CREATE, it);
490         if (rc)
491                 RETURN(rc);
492
493         mdc_store_inode_generation(mdc_exp, request, 2, 1);
494         inode = ll_create_node(dir, dentry->d_name.name, dentry->d_name.len,
495                                NULL, 0, mode, 0, it);
496         if (IS_ERR(inode)) {
497                 RETURN(PTR_ERR(inode));
498         }
499
500         d_instantiate(dentry, inode);
501         RETURN(0);
502 }
503
504 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
505 static int ll_create_nd(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
506 {
507         return ll_create_it(dir, dentry, mode, &nd->intent);
508 }
509 #endif
510
511 static int ll_mknod_raw(struct nameidata *nd, int mode, dev_t rdev)
512 {
513         struct ptlrpc_request *request = NULL;
514         struct inode *dir = nd->dentry->d_inode;
515         const char *name = nd->last.name;
516         int len = nd->last.len;
517         struct ll_sb_info *sbi = ll_i2sbi(dir);
518         struct mdc_op_data op_data;
519         int err = -EMLINK;
520         ENTRY;
521
522         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p)\n",
523                name, dir->i_ino, dir->i_generation, dir);
524
525         if (dir->i_nlink >= EXT3_LINK_MAX)
526                 RETURN(err);
527
528         mode &= ~current->fs->umask;
529
530         switch (mode & S_IFMT) {
531         case 0:
532         case S_IFREG:
533                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
534         case S_IFCHR:
535         case S_IFBLK:
536         case S_IFIFO:
537         case S_IFSOCK:
538                 ll_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
539                 err = md_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
540                                 current->fsuid, current->fsgid, rdev, &request);
541                 ptlrpc_req_finished(request);
542                 break;
543         case S_IFDIR:
544                 err = -EPERM;
545                 break;
546         default:
547                 err = -EINVAL;
548         }
549         RETURN(err);
550 }
551
552 static int ll_mknod(struct inode *dir, struct dentry *child, int mode,
553                     ll_dev_t rdev)
554 {
555         struct ptlrpc_request *request = NULL;
556         struct inode *inode = NULL;
557         const char *name = child->d_name.name;
558         int len = child->d_name.len;
559         struct ll_sb_info *sbi = ll_i2sbi(dir);
560         struct mdc_op_data op_data;
561         int err = -EMLINK;
562         ENTRY;
563
564         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p)\n",
565                name, dir->i_ino, dir->i_generation, dir);
566
567         if (dir->i_nlink >= EXT3_LINK_MAX)
568                 RETURN(err);
569
570         mode &= ~current->fs->umask;
571
572         switch (mode & S_IFMT) {
573         case 0:
574         case S_IFREG:
575                 mode |= S_IFREG; /* for mode = 0 case, fallthrough */
576         case S_IFCHR:
577         case S_IFBLK:
578         case S_IFIFO:
579         case S_IFSOCK:
580                 ll_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
581                 err = md_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
582                                 current->fsuid, current->fsgid, rdev, &request);
583                 err = ll_prep_inode(sbi->ll_osc_exp, sbi->ll_mdc_exp,
584                                     &inode, request, 0, child->d_sb);
585                 if (err)
586                         GOTO(out_err, err);
587                 break;
588         case S_IFDIR:
589                 RETURN(-EPERM);
590                 break;
591         default:
592                 RETURN(-EINVAL);
593         }
594
595         d_instantiate(child, inode);
596  out_err:
597         ptlrpc_req_finished(request);
598         RETURN(err);
599 }
600
601 static int ll_symlink_raw(struct nameidata *nd, const char *tgt)
602 {
603         struct inode *dir = nd->dentry->d_inode;
604         const char *name = nd->last.name;
605         int len = nd->last.len;
606         struct ptlrpc_request *request = NULL;
607         struct ll_sb_info *sbi = ll_i2sbi(dir);
608         struct mdc_op_data op_data;
609         int err = -EMLINK;
610         ENTRY;
611
612         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p),target=%s\n",
613                name, dir->i_ino, dir->i_generation, dir, tgt);
614
615         if (dir->i_nlink >= EXT3_LINK_MAX)
616                 RETURN(err);
617
618         ll_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
619         err = md_create(sbi->ll_mdc_exp, &op_data,
620                         tgt, strlen(tgt) + 1, S_IFLNK | S_IRWXUGO,
621                         current->fsuid, current->fsgid, 0, &request);
622         ptlrpc_req_finished(request);
623         RETURN(err);
624 }
625
626 static int ll_link_raw(struct nameidata *srcnd, struct nameidata *tgtnd)
627 {
628         struct inode *src = srcnd->dentry->d_inode;
629         struct inode *dir = tgtnd->dentry->d_inode;
630         const char *name = tgtnd->last.name;
631         int len = tgtnd->last.len;
632         struct ptlrpc_request *request = NULL;
633         struct mdc_op_data op_data;
634         int err;
635         struct ll_sb_info *sbi = ll_i2sbi(dir);
636
637         ENTRY;
638         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),dir=%lu/%u(%p),target=%s\n",
639                src->i_ino, src->i_generation, src,
640                dir->i_ino, dir->i_generation, dir, name);
641
642         ll_prepare_mdc_op_data(&op_data, src, dir, name, len, 0);
643         err = md_link(sbi->ll_mdc_exp, &op_data, &request);
644         ptlrpc_req_finished(request);
645
646         RETURN(err);
647 }
648
649
650 static int ll_mkdir_raw(struct nameidata *nd, int mode)
651 {
652         struct inode *dir = nd->dentry->d_inode;
653         const char *name = nd->last.name;
654         int len = nd->last.len;
655         struct ptlrpc_request *request = NULL;
656         struct ll_sb_info *sbi = ll_i2sbi(dir);
657         struct mdc_op_data op_data;
658         int err = -EMLINK;
659         ENTRY;
660         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p)\n",
661                name, dir->i_ino, dir->i_generation, dir);
662
663         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
664         ll_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
665         err = md_create(sbi->ll_mdc_exp, &op_data, NULL, 0, mode,
666                         current->fsuid, current->fsgid, 0, &request);
667         ptlrpc_req_finished(request);
668         RETURN(err);
669 }
670
671 static int ll_rmdir_raw(struct nameidata *nd)
672 {
673         struct inode *dir = nd->dentry->d_inode;
674         const char *name = nd->last.name;
675         int len = nd->last.len;
676         struct ptlrpc_request *request = NULL;
677         struct mdc_op_data op_data;
678         int rc;
679         ENTRY;
680         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p)\n",
681                name, dir->i_ino, dir->i_generation, dir);
682
683         ll_prepare_mdc_op_data(&op_data, dir, NULL, name, len, S_IFDIR);
684         rc = md_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
685         ptlrpc_req_finished(request);
686         RETURN(rc);
687 }
688
689 int ll_objects_destroy(struct ptlrpc_request *request,
690                        struct inode *dir, int offset)
691 {
692         struct mds_body *body;
693         struct lov_mds_md *eadata;
694         struct lov_stripe_md *lsm = NULL;
695         struct obd_trans_info oti = { 0 };
696         struct obdo *oa;
697         int rc;
698         ENTRY;
699
700         /* req is swabbed so this is safe */
701         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
702
703         if (!(body->valid & OBD_MD_FLEASIZE))
704                 RETURN(0);
705
706         if (body->eadatasize == 0) {
707                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
708                 GOTO(out, rc = -EPROTO);
709         }
710
711         /* The MDS sent back the EA because we unlinked the last reference
712          * to this file. Use this EA to unlink the objects on the OST.
713          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
714          * check it is complete and sensible. */
715         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
716         LASSERT(eadata != NULL);
717         if (eadata == NULL) {
718                 CERROR("Can't unpack MDS EA data\n");
719                 GOTO(out, rc = -EPROTO);
720         }
721
722         rc = obd_unpackmd(ll_i2obdexp(dir), &lsm, eadata, body->eadatasize);
723         if (rc < 0) {
724                 CERROR("obd_unpackmd: %d\n", rc);
725                 GOTO(out, rc);
726         }
727         LASSERT(rc >= sizeof(*lsm));
728
729         oa = obdo_alloc();
730         if (oa == NULL)
731                 GOTO(out_free_memmd, rc = -ENOMEM);
732
733         oa->o_id = lsm->lsm_object_id;
734         oa->o_gr = lsm->lsm_object_gr;
735         oa->o_mode = body->mode & S_IFMT;
736         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
737
738         if (body->valid & OBD_MD_FLCOOKIE) {
739                 int length = sizeof(struct llog_cookie) *
740                                 lsm->lsm_stripe_count;
741                 oa->o_valid |= OBD_MD_FLCOOKIE;
742                 oti.oti_logcookies =
743                         lustre_msg_buf(request->rq_repmsg, 2, length);
744                 if (oti.oti_logcookies == NULL) {
745                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
746                         body->valid &= ~OBD_MD_FLCOOKIE;
747                 } else {
748                         /* copy llog cookies to request to replay unlink
749                          * so that the same llog file and records as those created
750                          * during fail can be re-created while doing replay 
751                          */
752                         if (offset >= 0)
753                                 memcpy(lustre_msg_buf(request->rq_reqmsg, offset, 0),
754                                        oti.oti_logcookies, length);
755                 }
756         }
757
758         rc = obd_destroy(ll_i2obdexp(dir), oa, lsm, &oti);
759         obdo_free(oa);
760         if (rc)
761                 CERROR("obd destroy objid "LPX64" error %d\n",
762                        lsm->lsm_object_id, rc);
763  out_free_memmd:
764         obd_free_memmd(ll_i2obdexp(dir), &lsm);
765  out:
766         return rc;
767 }
768
769 static int ll_unlink_raw(struct nameidata *nd)
770 {
771         struct inode *dir = nd->dentry->d_inode;
772         const char *name = nd->last.name;
773         int len = nd->last.len;
774         struct ptlrpc_request *request = NULL;
775         struct mdc_op_data op_data;
776         int rc;
777         ENTRY;
778         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p)\n",
779                name, dir->i_ino, dir->i_generation, dir);
780
781         ll_prepare_mdc_op_data(&op_data, dir, NULL, name, len, 0);
782         rc = md_unlink(ll_i2sbi(dir)->ll_mdc_exp, &op_data, &request);
783         if (rc)
784                 GOTO(out, rc);
785
786         rc = ll_objects_destroy(request, dir, 2);
787  out:
788         ptlrpc_req_finished(request);
789         RETURN(rc);
790 }
791
792 static int ll_rename_raw(struct nameidata *oldnd, struct nameidata *newnd)
793 {
794         struct inode *src = oldnd->dentry->d_inode;
795         struct inode *tgt = newnd->dentry->d_inode;
796         const char *oldname = oldnd->last.name;
797         int oldlen  = oldnd->last.len;
798         const char *newname = newnd->last.name;
799         int newlen  = newnd->last.len;
800         struct ptlrpc_request *request = NULL;
801         struct ll_sb_info *sbi = ll_i2sbi(src);
802         struct mdc_op_data op_data;
803         int err;
804         ENTRY;
805         CDEBUG(D_VFSTRACE, "VFS Op:oldname=%s,src_dir=%lu/%u(%p),newname=%s,"
806                "tgt_dir=%lu/%u(%p)\n", oldname, src->i_ino, src->i_generation,
807                src, newname, tgt->i_ino, tgt->i_generation, tgt);
808
809         ll_prepare_mdc_op_data(&op_data, src, tgt, NULL, 0, 0);
810         err = md_rename(sbi->ll_mdc_exp, &op_data,
811                         oldname, oldlen, newname, newlen, &request);
812         if (!err) {
813                 err = ll_objects_destroy(request, src, 3);
814         }
815
816         ptlrpc_req_finished(request);
817
818         RETURN(err);
819 }
820
821 struct inode_operations ll_dir_inode_operations = {
822         .link_raw           = ll_link_raw,
823         .unlink_raw         = ll_unlink_raw,
824         .symlink_raw        = ll_symlink_raw,
825         .mkdir_raw          = ll_mkdir_raw,
826         .rmdir_raw          = ll_rmdir_raw,
827         .mknod_raw          = ll_mknod_raw,
828         .mknod              = ll_mknod,
829         .rename_raw         = ll_rename_raw,
830         .setattr            = ll_setattr,
831         .setattr_raw        = ll_setattr_raw,
832 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
833         .create_it          = ll_create_it,
834         .lookup_it          = ll_lookup_it,
835         .revalidate_it      = ll_inode_revalidate_it,
836 #else
837         .lookup             = ll_lookup_nd,
838         .create             = ll_create_nd,
839         .getattr_it         = ll_getattr,
840 #endif
841 };