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