Whamcloud - gitweb
- nobody needs these warnings any more
[fs/lustre-release.git] / lustre / llite / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  from
10  *
11  *  linux/fs/minix/dir.c
12  *  linux/fs/ext2/dir.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  ext2 directory handling functions
17  *
18  *  Big-endian to little-endian byte-swapping/bitmaps by
19  *        David S. Miller (davem@caip.rutgers.edu), 1995
20  *
21  *  All code that works with directory layout had been switched to pagecache
22  *  and moved here. AV
23  *
24  *  Adapted for Lustre Light
25  *  Copyright (C) 2002-2003, Cluster File Systems, Inc.
26  *
27  */
28
29 #include <linux/fs.h>
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.h>
32 #include <linux/mm.h>
33 #include <linux/version.h>
34 #include <linux/smp_lock.h>
35 #include <asm/uaccess.h>
36 #include <linux/file.h>
37 #include <linux/kmod.h>
38 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
39 # include <linux/locks.h>   // for wait_on_buffer
40 #else
41 # include <linux/buffer_head.h>   // for wait_on_buffer
42 #endif
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45
46 #include <linux/obd_support.h>
47 #include <linux/obd_class.h>
48 #include <linux/lustre_lib.h>
49 #include <linux/lustre_idl.h>
50 #include <linux/lustre_mds.h>
51 #include <linux/lustre_lite.h>
52 #include <linux/lustre_dlm.h>
53 #include <linux/lustre_sec.h>
54 #include <linux/lustre_audit.h>
55 #include <linux/lustre_acl.h>
56 #include "llite_internal.h"
57
58 typedef struct ext2_dir_entry_2 ext2_dirent;
59
60 #define PageChecked(page)        test_bit(PG_checked, &(page)->flags)
61 #define SetPageChecked(page)     set_bit(PG_checked, &(page)->flags)
62
63 /* returns the page unlocked, but with a reference */
64 static int ll_dir_readpage(struct file *file, struct page *page)
65 {
66         struct inode *inode = page->mapping->host;
67         struct ptlrpc_request *request;
68         struct mds_body *body;
69         struct lustre_id id;
70         __u64 offset;
71         int rc = 0;
72         ENTRY;
73
74         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
75                inode->i_generation, inode);
76
77         mdc_pack_id(&id, inode->i_ino, inode->i_generation, 
78                     S_IFDIR, id_group(&ll_i2info(inode)->lli_id),
79                     id_fid(&ll_i2info(inode)->lli_id));
80
81         offset = page->index << PAGE_SHIFT;
82         rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, &id, offset,
83                          page, &request);
84         if (!rc) {
85                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
86                 LASSERT (body != NULL);          /* checked by md_readpage() */
87                 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
88
89                 //inode->i_size = body->size;
90                 SetPageUptodate(page);
91         }
92         ptlrpc_req_finished(request);
93
94         unlock_page(page);
95         RETURN(rc);
96 }
97
98 struct address_space_operations ll_dir_aops = {
99         .readpage  = ll_dir_readpage,
100 };
101
102 /*
103  * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
104  * more robust, but we have what we have
105  */
106 static inline unsigned ext2_chunk_size(struct inode *inode)
107 {
108         return inode->i_sb->s_blocksize;
109 }
110
111 static inline void ext2_put_page(struct page *page)
112 {
113         kunmap(page);
114         page_cache_release(page);
115 }
116
117 static inline unsigned long dir_pages(struct inode *inode)
118 {
119         return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
120 }
121
122 static void ext2_check_page(struct page *page)
123 {
124         struct inode *dir = page->mapping->host;
125         unsigned chunk_size = ext2_chunk_size(dir);
126         char *kaddr = page_address(page);
127         //      u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
128         unsigned offs, rec_len;
129         unsigned limit = PAGE_CACHE_SIZE;
130         ext2_dirent *p;
131         char *error;
132
133         if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
134                 limit = dir->i_size & ~PAGE_CACHE_MASK;
135                 if (limit & (chunk_size - 1)) {
136                         CERROR("limit %d dir size %lld index %ld\n",
137                                limit, dir->i_size, page->index);
138                         goto Ebadsize;
139                 }
140                 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
141                         ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
142                         p->rec_len = cpu_to_le16(chunk_size);
143                         p->name_len = 0;
144                         p->inode = 0;
145                 }
146                 if (!limit)
147                         goto out;
148         }
149         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
150                 p = (ext2_dirent *)(kaddr + offs);
151                 rec_len = le16_to_cpu(p->rec_len);
152
153                 if (rec_len < EXT2_DIR_REC_LEN(1))
154                         goto Eshort;
155                 if (rec_len & 3)
156                         goto Ealign;
157                 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
158                         goto Enamelen;
159                 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
160                         goto Espan;
161                 //              if (le32_to_cpu(p->inode) > max_inumber)
162                 //goto Einumber;
163         }
164         if (offs != limit)
165                 goto Eend;
166 out:
167         SetPageChecked(page);
168         return;
169
170         /* Too bad, we had an error */
171
172 Ebadsize:
173         CERROR("ext2_check_page"
174                 "size of directory #%lu is not a multiple of chunk size\n",
175                 dir->i_ino
176         );
177         goto fail;
178 Eshort:
179         error = "rec_len is smaller than minimal";
180         goto bad_entry;
181 Ealign:
182         error = "unaligned directory entry";
183         goto bad_entry;
184 Enamelen:
185         error = "rec_len is too small for name_len";
186         goto bad_entry;
187 Espan:
188         error = "directory entry across blocks";
189         goto bad_entry;
190         //Einumber:
191         // error = "inode out of bounds";
192 bad_entry:
193         CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
194                 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
195                 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
196                 (unsigned long) le32_to_cpu(p->inode),
197                 rec_len, p->name_len);
198         goto fail;
199 Eend:
200         p = (ext2_dirent *)(kaddr + offs);
201         CERROR("ext2_check_page"
202                 "entry in directory #%lu spans the page boundary"
203                 "offset=%lu, inode=%lu",
204                 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
205                 (unsigned long) le32_to_cpu(p->inode));
206 fail:
207         SetPageChecked(page);
208         SetPageError(page);
209 }
210
211 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
212 {
213         struct ll_inode_info *li = ll_i2info(dir);
214         struct ldlm_res_id res_id =
215                 { .name = { id_fid(&li->lli_id), id_group(&li->lli_id)} };
216         struct lustre_handle lockh;
217         struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_md_exp);
218         struct address_space *mapping = dir->i_mapping;
219         struct page *page;
220         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
221         int rc;
222
223         obddev = md_get_real_obd(ll_i2sbi(dir)->ll_md_exp, &li->lli_id);
224         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
225                              &res_id, LDLM_IBITS, &policy, LCK_PR, &lockh);
226         if (!rc) {
227                 struct lookup_intent it = { .it_op = IT_READDIR };
228                 struct ptlrpc_request *request;
229                 struct mdc_op_data *op_data;
230
231                 OBD_ALLOC(op_data, sizeof(*op_data));
232                 if (op_data == NULL)
233                         return ERR_PTR(-ENOMEM);
234
235                 ll_inode2mdc_data(op_data, dir, (OBD_MD_FLID | OBD_MD_MEA));
236
237                 rc = ll_intent_alloc(&it);
238                 if (rc)
239                         return ERR_PTR(rc);
240
241                 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, LDLM_IBITS, &it,
242                                 LCK_PR, op_data, &lockh, NULL, 0,
243                                 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
244                 OBD_FREE(op_data, sizeof(*op_data));
245
246                 request = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
247                 ll_intent_free(&it);
248
249                 if (request)
250                         ptlrpc_req_finished(request);
251                 if (rc < 0) {
252                         CERROR("lock enqueue: rc: %d\n", rc);
253                         return ERR_PTR(rc);
254                 }
255         } else {
256                 if (ptlrpcs_check_cred(obddev->u.cli.cl_import)) {
257                         /* return immediately if no credential held */
258                         ldlm_lock_decref(&lockh, LCK_PR);
259                         return ERR_PTR(-EACCES);
260                 }
261         }
262         ldlm_lock_dump_handle(D_OTHER, &lockh);
263
264         page = read_cache_page(mapping, n,
265                                (filler_t*)mapping->a_ops->readpage, NULL);
266         if (!IS_ERR(page)) {
267                 wait_on_page(page);
268                 (void)kmap(page);
269                 if (!PageUptodate(page))
270                         goto fail;
271                 if (!PageChecked(page))
272                         ext2_check_page(page);
273                 if (PageError(page))
274                         goto fail;
275         }
276
277 out_unlock:
278         ldlm_lock_decref(&lockh, LCK_PR);
279         return page;
280
281 fail:
282         ext2_put_page(page);
283         page = ERR_PTR(-EIO);
284         goto out_unlock;
285 }
286
287 /*
288  * p is at least 6 bytes before the end of page
289  */
290 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
291 {
292         return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
293 }
294
295 static inline unsigned
296 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
297 {
298         ext2_dirent *de = (ext2_dirent*)(base + offset);
299         ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
300         while ((char*)p < (char*)de)
301                 p = ext2_next_entry(p);
302         return (char *)p - base;
303 }
304
305 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
306         [EXT2_FT_UNKNOWN]       DT_UNKNOWN,
307         [EXT2_FT_REG_FILE]      DT_REG,
308         [EXT2_FT_DIR]           DT_DIR,
309         [EXT2_FT_CHRDEV]        DT_CHR,
310         [EXT2_FT_BLKDEV]        DT_BLK,
311         [EXT2_FT_FIFO]          DT_FIFO,
312         [EXT2_FT_SOCK]          DT_SOCK,
313         [EXT2_FT_SYMLINK]       DT_LNK,
314 };
315
316
317 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
318 {
319         struct inode *inode = filp->f_dentry->d_inode;
320         loff_t pos = filp->f_pos;
321         // XXX struct super_block *sb = inode->i_sb;
322         unsigned offset = pos & ~PAGE_CACHE_MASK;
323         unsigned long n = pos >> PAGE_CACHE_SHIFT;
324         unsigned long npages = dir_pages(inode);
325         unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
326         unsigned char *types = ext2_filetype_table;
327         int need_revalidate = (filp->f_version != inode->i_version);
328         int rc = 0;
329         ENTRY;
330
331         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
332                inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
333
334         if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
335                 RETURN(0);
336
337         for ( ; n < npages; n++, offset = 0) {
338                 char *kaddr, *limit;
339                 ext2_dirent *de;
340                 struct page *page;
341
342                 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
343                        PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
344                        n, npages, inode->i_size);
345                 page = ll_get_dir_page(inode, n);
346
347                 /* size might have been updated by md_readpage() */
348                 npages = dir_pages(inode);
349
350                 if (IS_ERR(page)) {
351                         rc = PTR_ERR(page);
352                         CERROR("error reading dir %lu/%u page %lu: rc %d\n",
353                                inode->i_ino, inode->i_generation, n, rc);
354                         continue;
355                 }
356
357                 kaddr = page_address(page);
358                 if (need_revalidate) {
359                         offset = ext2_validate_entry(kaddr, offset, chunk_mask);
360                         need_revalidate = 0;
361                 }
362                 de = (ext2_dirent *)(kaddr+offset);
363                 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
364                 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
365                         if (de->inode) {
366                                 int over;
367                                 
368                                 rc = 0; /* no error if we return something */
369                                 
370                                 offset = (char *)de - kaddr;
371                                 over = filldir(dirent, de->name, de->name_len,
372                                                (n<<PAGE_CACHE_SHIFT) | offset,
373                                                le32_to_cpu(de->inode),
374                                                types[de->file_type &
375                                                      (EXT2_FT_MAX - 1)]);
376
377                                 if (over) {
378                                         ext2_put_page(page);
379                                         GOTO(done, rc);
380                                 }
381                         }
382                 }
383                 ext2_put_page(page);
384         }
385
386         EXIT;
387 done:
388         filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
389         filp->f_version = inode->i_version;
390         update_atime(inode);
391         return rc;
392 }
393
394 static int ll_mkdir_stripe(struct inode *inode, unsigned long arg)
395 {
396         struct ptlrpc_request *request = NULL;
397         struct ll_sb_info *sbi = ll_i2sbi(inode);
398         struct ll_user_mkdir_stripe lums;
399         struct mdc_op_data *op_data;
400         u16 nstripes;
401         mode_t mode;
402         char *name;
403         int err = 0;
404         ENTRY;
405
406         if (copy_from_user(&lums, (void *)arg, sizeof(lums)))
407                 RETURN(-EFAULT);
408
409         if (lums.lums_namelen <= 0)
410                 RETURN(-EINVAL);
411         OBD_ALLOC(name, lums.lums_namelen);
412         if (!name)
413                 RETURN(-ENOMEM);
414
415         if (copy_from_user(name, lums.lums_name, lums.lums_namelen))
416                 GOTO(out, err = -EFAULT);
417
418         CDEBUG(D_VFSTRACE, "ioctl Op:name=%s,dir=%lu/%u(%p)\n",
419                name, inode->i_ino, inode->i_generation, inode);
420         nstripes = lums.lums_nstripes;
421
422         mode = lums.lums_mode;
423         mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
424
425         OBD_ALLOC(op_data, sizeof(*op_data));
426         if (op_data == NULL)
427                 GOTO(out, err = -ENOMEM);
428         ll_prepare_mdc_data(op_data, inode, NULL, name, lums.lums_namelen, 0);
429         err = md_create(sbi->ll_md_exp, op_data, &nstripes, sizeof(nstripes),
430                         mode, current->fsuid, current->fsgid, 0, &request);
431         OBD_FREE(op_data, sizeof(*op_data));
432         ptlrpc_req_finished(request);
433         EXIT;
434 out:
435         OBD_FREE(name, lums.lums_namelen);
436         return err;
437 }
438
439 /*
440  * we don't call getxattr_internal/setxattr_internal because we
441  * need more precisely control.
442  */
443 static int ll_ioctl_getfacl(struct inode *inode,
444                             struct file *file,
445                             struct ll_acl_ioctl_data *ioc)
446 {
447         struct ptlrpc_request *req = NULL;
448         struct mds_body *body;
449         char *cmd, *res;
450         struct lustre_id id;
451         int rc;
452         ENTRY;
453
454         if (!ioc->cmd || !ioc->cmd_len ||
455             !ioc->res || !ioc->res_len) {
456                 CERROR("error: cmd %p, len %lu, res %p, len %lu\n",
457                        ioc->cmd, ioc->cmd_len, ioc->res, ioc->res_len);
458                 RETURN(-EINVAL);
459         }
460
461         OBD_ALLOC(cmd, ioc->cmd_len);
462         if (!cmd)
463                 RETURN(-ENOMEM);
464         if (copy_from_user(cmd, ioc->cmd, ioc->cmd_len))
465                 GOTO(out, rc = -EFAULT);
466
467         /* we didn't call ll_getxattr_internal() because we'd like to
468          * copy from reply buffer to user space directly.
469          */
470         ll_inode2id(&id, inode);
471         rc = md_getattr(ll_i2sbi(inode)->ll_md_exp, &id, OBD_MD_FLXATTR,
472                         XATTR_NAME_LUSTRE_ACL,
473                         cmd, ioc->cmd_len, ioc->res_len, NULL, &req);
474         if (rc < 0) {
475                 CERROR("rc: %d\n", rc);
476                 GOTO(out, rc);
477         }
478
479         res = lustre_msg_buf(req->rq_repmsg, 1, ioc->res_len);
480         LASSERT(res);
481         if (copy_to_user(ioc->res, res, ioc->res_len))
482                 rc = -EFAULT;
483
484         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
485         LASSERT(body);
486         ioc->status = (__s32) body->flags;
487
488         EXIT;
489 out:
490         if (req)
491                 ptlrpc_req_finished(req);
492         OBD_FREE(cmd, ioc->cmd_len);
493
494         return rc;
495 }
496
497 static int ll_ioctl_setfacl(struct inode *inode,
498                             struct file *file,
499                             struct ll_acl_ioctl_data *ioc)
500 {
501         struct ptlrpc_request *req = NULL;
502         struct mdc_op_data *op_data;
503         struct mds_body *body;
504         struct iattr attr;
505         char *cmd;
506         int replen, rc;
507         ENTRY;
508
509         if (!ioc->cmd || !ioc->cmd_len) {
510                 CERROR("error: cmd %p, len %lu\n", ioc->cmd, ioc->cmd_len);
511                 RETURN(-EINVAL);
512         }
513
514         OBD_ALLOC(cmd, ioc->cmd_len);
515         if (!cmd)
516                 RETURN(-ENOMEM);
517         if (copy_from_user(cmd, ioc->cmd, ioc->cmd_len))
518                 GOTO(out, rc = -EFAULT);
519
520         memset(&attr, 0x0, sizeof(attr));
521         attr.ia_valid |= ATTR_EA;
522         attr.ia_attr_flags = 0;
523
524         OBD_ALLOC(op_data, sizeof(*op_data));
525         if (!op_data)
526                 GOTO(out, rc = -ENOMEM);
527         
528         ll_inode2mdc_data(op_data, inode, (OBD_MD_FLID | OBD_MD_MEA));
529
530         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data, &attr,
531                         (void *)XATTR_NAME_LUSTRE_ACL,
532                         sizeof(XATTR_NAME_LUSTRE_ACL),
533                         (void *)cmd, ioc->cmd_len, NULL, 0, &req);
534         OBD_FREE(op_data, sizeof(*op_data));
535         
536         if (rc) {
537                 CERROR("md_setattr fails: rc = %d\n", rc);
538                 GOTO(out, rc);
539         }
540
541         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
542         LASSERT(body);
543         ioc->status = (__s32) body->flags;
544
545         LASSERT(req->rq_repmsg->bufcount == 2);
546         replen = req->rq_repmsg->buflens[1];
547         LASSERT(replen <= LUSTRE_ACL_SIZE_MAX);
548         if (replen) {
549                 if (replen > ioc->res_len)
550                         replen = ioc->res_len;
551                 if (copy_to_user(ioc->res,
552                                  lustre_msg_buf(req->rq_repmsg, 1, replen),
553                                  replen))
554                         rc = -EFAULT;
555         }
556         EXIT;
557 out:
558         if (req)
559                 ptlrpc_req_finished(req);
560         OBD_FREE(cmd, ioc->cmd_len);
561
562         return rc;
563 }
564
565 static int ll_dir_ioctl(struct inode *inode, struct file *file,
566                         unsigned int cmd, unsigned long arg)
567 {
568         struct ll_sb_info *sbi = ll_i2sbi(inode);
569         struct obd_ioctl_data *data;
570         ENTRY;
571
572         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
573                inode->i_ino, inode->i_generation, inode, cmd);
574
575         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
576                 return -ENOTTY;
577
578         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
579         switch(cmd) {
580         case EXT3_IOC_GETFLAGS:
581         case EXT3_IOC_SETFLAGS:
582                 RETURN(ll_iocontrol(inode, file, cmd, arg));
583         case IOC_MDC_LOOKUP: {
584                 struct ptlrpc_request *request = NULL;
585                 struct lustre_id id;
586                 char *buf = NULL;
587                 char *filename;
588                 int namelen, rc, len = 0;
589                 __u64 valid;
590
591                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
592                 if (rc)
593                         RETURN(rc);
594                 data = (void *)buf;
595
596                 filename = data->ioc_inlbuf1;
597                 namelen = data->ioc_inllen1;
598
599                 if (namelen < 1) {
600                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
601                         GOTO(out, rc = -EINVAL);
602                 }
603
604                 valid = OBD_MD_FLID;
605                 ll_inode2id(&id, inode);
606                 rc = md_getattr_lock(sbi->ll_md_exp, &id,
607                                      filename, namelen, valid, 0, &request);
608                 if (rc < 0) {
609                         CDEBUG(D_INFO, "md_getattr_lock: %d\n", rc);
610                         GOTO(out, rc);
611                 }
612
613                 ptlrpc_req_finished(request);
614
615                 EXIT;
616         out:
617                 obd_ioctl_freedata(buf, len);
618                 return rc;
619         }
620         case LL_IOC_KEY_TYPE: {
621                 char *buf = NULL;
622                 char *type;
623                 int typelen, rc, len = 0;
624
625                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
626                 if (rc)
627                         RETURN(rc);
628                 data = (void *)buf;
629
630                 type = data->ioc_inlbuf1;
631                 typelen = data->ioc_inllen1;
632
633                 if (typelen < 1) {
634                         CDEBUG(D_INFO, "LL_IOC_KEY_TYPE missing filename\n");
635                         GOTO(out_free, rc = -EINVAL);
636                 }
637                 ll_set_sb_gksinfo(inode->i_sb, type);
638                 EXIT;
639         out_free:
640                 obd_ioctl_freedata(buf, len);
641                 RETURN(rc);
642         }
643         case LL_IOC_MDC_MKDIRSTRIPE:
644                 RETURN(ll_mkdir_stripe(inode, arg));
645         case LL_IOC_LOV_SETSTRIPE: {
646                 struct ptlrpc_request *request = NULL;
647                 struct mdc_op_data *op_data;
648                 struct iattr attr = { 0 };
649                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
650                 int rc = 0;
651
652                 LASSERT(sizeof(lum) == sizeof(*lump));
653                 LASSERT(sizeof(lum.lmm_objects[0]) ==
654                         sizeof(lump->lmm_objects[0]));
655                 rc = copy_from_user(&lum, lump, sizeof(lum));
656                 if (rc)
657                         RETURN(-EFAULT);
658
659                 if (lum.lmm_magic != LOV_USER_MAGIC)
660                         RETURN(-EINVAL);
661
662                 OBD_ALLOC(op_data, sizeof(*op_data));
663                 if (op_data == NULL)
664                         RETURN(-ENOMEM);
665                 
666                 ll_inode2mdc_data(op_data, inode, (OBD_MD_FLID | OBD_MD_MEA));
667
668                 rc = md_setattr(sbi->ll_md_exp, op_data, &attr, &lum,
669                                 sizeof(lum), NULL, 0, NULL, 0, &request);
670                 OBD_FREE(op_data, sizeof(*op_data));
671                 ptlrpc_req_finished(request);
672
673                 if (rc) {
674                         if (rc != -EPERM && rc != -EACCES)
675                                 CERROR("md_setattr fails: rc = %d\n", rc);
676                 }
677                 RETURN(rc);
678         }
679         case LL_IOC_LOV_GETSTRIPE: {
680                 struct ptlrpc_request *request = NULL;
681                 struct lov_user_md *lump = (struct lov_user_md *)arg;
682                 struct lov_mds_md *lmm;
683                 struct lustre_id id;
684                 struct mds_body *body;
685                 __u64 valid = 0;
686                 int rc, lmmsize;
687
688                 valid |= OBD_MD_FLDIREA;
689
690                 ll_inode2id(&id, inode);
691                 rc = md_getattr(sbi->ll_md_exp, &id, valid, NULL, NULL, 0,
692                                 obd_size_diskmd(sbi->ll_dt_exp, NULL),
693                                 NULL, &request);
694                 if (rc < 0) {
695                         CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
696                         RETURN(rc);
697                 }
698
699                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
700                 LASSERT(body != NULL);         /* checked by md_getattr_lock */
701                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
702
703                 lmmsize = body->eadatasize;
704                 if (lmmsize == 0)
705                         GOTO(out_get, rc = -ENODATA);
706
707                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
708                 LASSERT(lmm != NULL);
709                 LASSERT_REPSWABBED(request, 1);
710                 rc = copy_to_user(lump, lmm, lmmsize);
711                 if (rc)
712                         GOTO(out_get, rc = -EFAULT);
713
714                 EXIT;
715         out_get:
716                 ptlrpc_req_finished(request);
717                 RETURN(rc);
718         }
719         case IOC_MDC_SHOWFID: {
720                 struct lustre_id *idp = (struct lustre_id *)arg;
721                 struct lustre_id id;
722                 char *filename;
723                 int rc;
724
725                 filename = getname((const char *)arg);
726                 if (IS_ERR(filename))
727                         RETURN(PTR_ERR(filename));
728
729                 ll_inode2id(&id, inode);
730
731                 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
732                 if (rc < 0)
733                         GOTO(out_filename, rc);
734
735                 rc = copy_to_user(idp, &id, sizeof(*idp));
736                 if (rc)
737                         GOTO(out_filename, rc = -EFAULT);
738
739                 EXIT;
740         out_filename:
741                 putname(filename);
742                 return rc;
743         }
744         case IOC_MDC_GETSTRIPE: {
745                 struct lov_user_md *lump = (struct lov_user_md *)arg;
746                 struct ptlrpc_request *request = NULL;
747                 struct lov_mds_md *lmm;
748                 struct mds_body *body;
749                 struct lustre_id id;
750                 char *filename;
751                 int rc, lmmsize;
752
753                 filename = getname((const char *)arg);
754                 if (IS_ERR(filename))
755                         RETURN(PTR_ERR(filename));
756
757                 ll_inode2id(&id, inode);
758                 rc = md_getattr_lock(sbi->ll_md_exp, &id, filename,
759                                      strlen(filename) + 1, OBD_MD_FLEASIZE,
760                                      obd_size_diskmd(sbi->ll_dt_exp, NULL),
761                                      &request);
762                 if (rc < 0) {
763                         CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
764                                filename, rc);
765                         GOTO(out_name, rc);
766                 }
767
768                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
769                 LASSERT(body != NULL);         /* checked by md_getattr_lock */
770                 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
771
772                 lmmsize = body->eadatasize;
773
774                 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
775                         GOTO(out_req, rc = -ENODATA);
776
777                 if (lmmsize > 4096)
778                         GOTO(out_req, rc = -EFBIG);
779
780                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
781                 LASSERT(lmm != NULL);
782                 LASSERT_REPSWABBED(request, 1);
783
784                 rc = copy_to_user(lump, lmm, lmmsize);
785                 if (rc)
786                         GOTO(out_req, rc = -EFAULT);
787
788                 EXIT;
789         out_req:
790                 ptlrpc_req_finished(request);
791         out_name:
792                 putname(filename);
793                 return rc;
794         }
795         case OBD_IOC_PING: {
796                 struct ptlrpc_request *req = NULL;
797                 char *buf = NULL;
798                 int rc, len=0;
799                 struct client_obd *cli;
800                 struct obd_device *obd;
801
802                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
803                 if (rc)
804                         RETURN(rc);
805                 data = (void *)buf;
806
807                 obd = class_name2obd(data->ioc_inlbuf1);
808
809                 if (!obd )
810                         GOTO(out_ping, rc = -ENODEV);
811
812                 if (!obd->obd_attached) {
813                         CERROR("Device %d not attached\n", obd->obd_minor);
814                         GOTO(out_ping, rc = -ENODEV);
815                 }
816                 if (!obd->obd_set_up) {
817                         CERROR("Device %d still not setup\n", obd->obd_minor);
818                         GOTO(out_ping, rc = -ENODEV);
819                 }
820                 cli = &obd->u.cli;
821                 req = ptlrpc_prep_req(cli->cl_import, LUSTRE_OBD_VERSION,
822                                       OBD_PING, 0, NULL, NULL);
823                 if (!req)
824                         GOTO(out_ping, rc = -ENOMEM);
825
826                 req->rq_replen = lustre_msg_size(0, NULL);
827                 req->rq_send_state = LUSTRE_IMP_FULL;
828
829                 rc = ptlrpc_queue_wait(req);
830
831                 ptlrpc_req_finished(req);
832         out_ping:
833                 obd_ioctl_freedata(buf, len);
834                 return rc;
835         }
836         case OBD_IOC_LLOG_CATINFO: {
837                 struct ptlrpc_request *req = NULL;
838                 char *buf = NULL;
839                 int rc, len = 0;
840                 char *bufs[2], *str;
841                 int lens[2], size;
842
843                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
844                 if (rc)
845                         RETURN(rc);
846                 data = (void *)buf;
847
848                 if (!data->ioc_inlbuf1) {
849                         obd_ioctl_freedata(buf, len);
850                         RETURN(-EINVAL);
851                 }
852
853                 lens[0] = data->ioc_inllen1;
854                 bufs[0] = data->ioc_inlbuf1;
855                 if (data->ioc_inllen2) {
856                         lens[1] = data->ioc_inllen2;
857                         bufs[1] = data->ioc_inlbuf2;
858                 } else {
859                         lens[1] = 0;
860                         bufs[1] = NULL;
861                 }
862                 size = data->ioc_plen1;
863                 req = ptlrpc_prep_req(sbi2md(sbi)->cl_import,
864                                       LUSTRE_LOG_VERSION, LLOG_CATINFO,
865                                       2, lens, bufs);
866                 if (!req)
867                         GOTO(out_catinfo, rc = -ENOMEM);
868
869                 req->rq_replen = lustre_msg_size(1, &size);
870
871                 rc = ptlrpc_queue_wait(req);
872                 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
873                 if (!rc)
874                         rc = copy_to_user(data->ioc_pbuf1, str,
875                                           data->ioc_plen1);
876                 ptlrpc_req_finished(req);
877         out_catinfo:
878                 obd_ioctl_freedata(buf, len);
879                 RETURN(rc);
880         }
881         case LL_IOC_GETFACL: {
882                 struct ll_acl_ioctl_data ioc, *uioc;
883                 int rc;
884
885                 if (copy_from_user(&ioc, (void *) arg, sizeof(ioc)))
886                         RETURN(-EFAULT);
887
888                 rc = ll_ioctl_getfacl(inode, file, &ioc);
889                 if (!rc) {
890                         uioc = (struct ll_acl_ioctl_data *) arg;
891                         if (copy_to_user(&uioc->status, &ioc.status,
892                                          sizeof(ioc.status)))
893                                 rc = -EFAULT;
894                 }
895                 RETURN(rc);
896         }
897         case LL_IOC_SETFACL: {
898                 struct ll_acl_ioctl_data ioc, *uioc;
899                 int rc;
900
901                 if (copy_from_user(&ioc, (void *) arg, sizeof(ioc)))
902                         RETURN(-EFAULT);
903
904                 rc = ll_ioctl_setfacl(inode, file, &ioc);
905                 if (!rc) {
906                         uioc = (struct ll_acl_ioctl_data *) arg;
907                         if (copy_to_user(&uioc->status, &ioc.status,
908                                          sizeof(ioc.status)))
909                                 rc = -EFAULT;
910                 }
911                 RETURN(rc);
912         }
913         case LL_IOC_FLUSH_CRED:
914                 RETURN(ll_flush_cred(inode));
915         case LL_IOC_AUDIT:
916                 RETURN(ll_set_audit(inode, SET_AUDIT_OP(arg, AUDIT_DIR)));
917         default:
918                 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0,
919                                      NULL, (void *)arg);
920         }
921 }
922
923 int ll_dir_open(struct inode *inode, struct file *file)
924 {
925         return ll_file_open(inode, file);
926 }
927
928 int ll_dir_release(struct inode *inode, struct file *file)
929 {
930         return ll_file_release(inode, file);
931 }
932
933 struct file_operations ll_dir_operations = {
934         .open     = ll_dir_open,
935         .release  = ll_dir_release,
936         .read     = generic_read_dir,
937         .readdir  = ll_readdir,
938         .ioctl    = ll_dir_ioctl
939 };
940