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