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