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