1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
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)
11 * linux/fs/minix/dir.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * ext2 directory handling functions
18 * Big-endian to little-endian byte-swapping/bitmaps by
19 * David S. Miller (davem@caip.rutgers.edu), 1995
21 * All code that works with directory layout had been switched to pagecache
24 * Adapted for Lustre Light
25 * Copyright (C) 2002-2003, Cluster File Systems, Inc.
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.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
41 # include <linux/buffer_head.h> // for wait_on_buffer
44 #define DEBUG_SUBSYSTEM S_LLITE
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 "llite_internal.h"
56 typedef struct ext2_dir_entry_2 ext2_dirent;
58 #define PageChecked(page) test_bit(PG_checked, &(page)->flags)
59 #define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
61 /* returns the page unlocked, but with a reference */
62 static int ll_dir_readpage(struct file *file, struct page *page)
64 struct inode *inode = page->mapping->host;
65 struct ptlrpc_request *request;
66 struct mds_body *body;
72 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
73 inode->i_generation, inode);
75 mdc_pack_id(&id, inode->i_ino, inode->i_generation,
76 S_IFDIR, id_group(&ll_i2info(inode)->lli_id),
77 id_fid(&ll_i2info(inode)->lli_id));
79 offset = page->index << PAGE_SHIFT;
80 rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, &id, offset,
83 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
84 LASSERT (body != NULL); /* checked by md_readpage() */
85 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
87 #warning "FIXME ASAP!"
88 //inode->i_size = body->size;
89 SetPageUptodate(page);
91 ptlrpc_req_finished(request);
97 struct address_space_operations ll_dir_aops = {
98 .readpage = ll_dir_readpage,
102 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
103 * more robust, but we have what we have
105 static inline unsigned ext2_chunk_size(struct inode *inode)
107 return inode->i_sb->s_blocksize;
110 static inline void ext2_put_page(struct page *page)
113 page_cache_release(page);
116 static inline unsigned long dir_pages(struct inode *inode)
118 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
121 static void ext2_check_page(struct page *page)
123 struct inode *dir = page->mapping->host;
124 unsigned chunk_size = ext2_chunk_size(dir);
125 char *kaddr = page_address(page);
126 // u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
127 unsigned offs, rec_len;
128 unsigned limit = PAGE_CACHE_SIZE;
132 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
133 limit = dir->i_size & ~PAGE_CACHE_MASK;
134 if (limit & (chunk_size - 1)) {
135 CERROR("limit %d dir size %lld index %ld\n",
136 limit, dir->i_size, page->index);
139 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
140 ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
141 p->rec_len = cpu_to_le16(chunk_size);
148 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
149 p = (ext2_dirent *)(kaddr + offs);
150 rec_len = le16_to_cpu(p->rec_len);
152 if (rec_len < EXT2_DIR_REC_LEN(1))
156 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
158 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
160 // if (le32_to_cpu(p->inode) > max_inumber)
166 SetPageChecked(page);
169 /* Too bad, we had an error */
172 CERROR("ext2_check_page"
173 "size of directory #%lu is not a multiple of chunk size\n",
178 error = "rec_len is smaller than minimal";
181 error = "unaligned directory entry";
184 error = "rec_len is too small for name_len";
187 error = "directory entry across blocks";
190 // error = "inode out of bounds";
192 CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
193 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
194 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
195 (unsigned long) le32_to_cpu(p->inode),
196 rec_len, p->name_len);
199 p = (ext2_dirent *)(kaddr + offs);
200 CERROR("ext2_check_page"
201 "entry in directory #%lu spans the page boundary"
202 "offset=%lu, inode=%lu",
203 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
204 (unsigned long) le32_to_cpu(p->inode));
206 SetPageChecked(page);
210 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
212 struct ll_inode_info *li = ll_i2info(dir);
213 struct ldlm_res_id res_id =
214 { .name = { id_fid(&li->lli_id), id_group(&li->lli_id)} };
215 struct lustre_handle lockh;
216 struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_md_exp);
217 struct address_space *mapping = dir->i_mapping;
219 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
222 obddev = md_get_real_obd(ll_i2sbi(dir)->ll_md_exp, &li->lli_id);
223 rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
224 &res_id, LDLM_IBITS, &policy, LCK_PR, &lockh);
226 struct lookup_intent it = { .it_op = IT_READDIR };
227 struct ptlrpc_request *request;
228 struct mdc_op_data *op_data;
230 OBD_ALLOC(op_data, sizeof(*op_data));
232 return ERR_PTR(-ENOMEM);
234 ll_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
236 rc = ll_intent_alloc(&it);
240 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, LDLM_IBITS, &it,
241 LCK_PR, op_data, &lockh, NULL, 0,
242 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
243 OBD_FREE(op_data, sizeof(*op_data));
245 request = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
249 ptlrpc_req_finished(request);
251 CERROR("lock enqueue: rc: %d\n", rc);
255 if (ptlrpcs_check_cred(obddev->u.cli.cl_import)) {
256 /* return immediately if no credential held */
257 ldlm_lock_decref(&lockh, LCK_PR);
258 return ERR_PTR(-EACCES);
261 ldlm_lock_dump_handle(D_OTHER, &lockh);
263 page = read_cache_page(mapping, n,
264 (filler_t*)mapping->a_ops->readpage, NULL);
268 if (!PageUptodate(page))
270 if (!PageChecked(page))
271 ext2_check_page(page);
277 ldlm_lock_decref(&lockh, LCK_PR);
282 page = ERR_PTR(-EIO);
287 * p is at least 6 bytes before the end of page
289 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
291 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
294 static inline unsigned
295 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
297 ext2_dirent *de = (ext2_dirent*)(base + offset);
298 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
299 while ((char*)p < (char*)de)
300 p = ext2_next_entry(p);
301 return (char *)p - base;
304 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
305 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
306 [EXT2_FT_REG_FILE] DT_REG,
307 [EXT2_FT_DIR] DT_DIR,
308 [EXT2_FT_CHRDEV] DT_CHR,
309 [EXT2_FT_BLKDEV] DT_BLK,
310 [EXT2_FT_FIFO] DT_FIFO,
311 [EXT2_FT_SOCK] DT_SOCK,
312 [EXT2_FT_SYMLINK] DT_LNK,
316 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
318 struct inode *inode = filp->f_dentry->d_inode;
319 loff_t pos = filp->f_pos;
320 // XXX struct super_block *sb = inode->i_sb;
321 unsigned offset = pos & ~PAGE_CACHE_MASK;
322 unsigned long n = pos >> PAGE_CACHE_SHIFT;
323 unsigned long npages = dir_pages(inode);
324 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
325 unsigned char *types = ext2_filetype_table;
326 int need_revalidate = (filp->f_version != inode->i_version);
330 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
331 inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
333 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
336 for ( ; n < npages; n++, offset = 0) {
341 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
342 PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
343 n, npages, inode->i_size);
344 page = ll_get_dir_page(inode, n);
346 /* size might have been updated by md_readpage() */
347 npages = dir_pages(inode);
351 CERROR("error reading dir %lu/%u page %lu: rc %d\n",
352 inode->i_ino, inode->i_generation, n, rc);
356 kaddr = page_address(page);
357 if (need_revalidate) {
358 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
361 de = (ext2_dirent *)(kaddr+offset);
362 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
363 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
367 rc = 0; /* no error if we return something */
369 offset = (char *)de - kaddr;
370 over = filldir(dirent, de->name, de->name_len,
371 (n<<PAGE_CACHE_SHIFT) | offset,
372 le32_to_cpu(de->inode),
373 types[de->file_type &
387 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
388 filp->f_version = inode->i_version;
393 static int ll_mkdir_stripe(struct inode *inode, unsigned long arg)
395 struct ptlrpc_request *request = NULL;
396 struct ll_sb_info *sbi = ll_i2sbi(inode);
397 struct ll_user_mkdir_stripe lums;
398 struct mdc_op_data *op_data;
405 if (copy_from_user(&lums, (void *)arg, sizeof(lums)))
408 if (lums.lums_namelen <= 0)
410 OBD_ALLOC(name, lums.lums_namelen);
414 if (copy_from_user(name, lums.lums_name, lums.lums_namelen))
415 GOTO(out, err = -EFAULT);
417 CDEBUG(D_VFSTRACE, "ioctl Op:name=%s,dir=%lu/%u(%p)\n",
418 name, inode->i_ino, inode->i_generation, inode);
419 nstripes = lums.lums_nstripes;
421 mode = lums.lums_mode;
422 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
424 OBD_ALLOC(op_data, sizeof(*op_data));
426 GOTO(out, err = -ENOMEM);
427 ll_prepare_mdc_data(op_data, inode, NULL, name,lums.lums_namelen,0);
428 err = md_create(sbi->ll_md_exp, op_data, &nstripes, sizeof(nstripes),
429 mode, current->fsuid, current->fsgid, 0, &request);
430 OBD_FREE(op_data, sizeof(*op_data));
431 ptlrpc_req_finished(request);
434 OBD_FREE(name, lums.lums_namelen);
438 static int ll_dir_ioctl(struct inode *inode, struct file *file,
439 unsigned int cmd, unsigned long arg)
441 struct ll_sb_info *sbi = ll_i2sbi(inode);
442 struct obd_ioctl_data *data;
445 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
446 inode->i_ino, inode->i_generation, inode, cmd);
448 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
451 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
453 case EXT3_IOC_GETFLAGS:
454 case EXT3_IOC_SETFLAGS:
455 RETURN(ll_iocontrol(inode, file, cmd, arg));
456 case IOC_MDC_LOOKUP: {
457 struct ptlrpc_request *request = NULL;
461 int namelen, rc, len = 0;
464 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
469 filename = data->ioc_inlbuf1;
470 namelen = data->ioc_inllen1;
473 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
474 GOTO(out, rc = -EINVAL);
478 ll_inode2id(&id, inode);
479 rc = md_getattr_lock(sbi->ll_md_exp, &id,
480 filename, namelen, valid, 0, &request);
482 CDEBUG(D_INFO, "md_getattr_lock: %d\n", rc);
486 ptlrpc_req_finished(request);
490 obd_ioctl_freedata(buf, len);
493 case LL_IOC_MDC_MKDIRSTRIPE:
494 RETURN(ll_mkdir_stripe(inode, arg));
495 case LL_IOC_LOV_SETSTRIPE: {
496 struct ptlrpc_request *request = NULL;
497 struct mdc_op_data *op_data;
498 struct iattr attr = { 0 };
499 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
502 LASSERT(sizeof(lum) == sizeof(*lump));
503 LASSERT(sizeof(lum.lmm_objects[0]) ==
504 sizeof(lump->lmm_objects[0]));
505 rc = copy_from_user(&lum, lump, sizeof(lum));
509 if (lum.lmm_magic != LOV_USER_MAGIC)
512 OBD_ALLOC(op_data, sizeof(*op_data));
516 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
518 rc = md_setattr(sbi->ll_md_exp, op_data, &attr, &lum,
519 sizeof(lum), NULL, 0, &request);
520 OBD_FREE(op_data, sizeof(*op_data));
521 ptlrpc_req_finished(request);
524 if (rc != -EPERM && rc != -EACCES)
525 CERROR("md_setattr fails: rc = %d\n", rc);
529 case LL_IOC_LOV_GETSTRIPE: {
530 struct ptlrpc_request *request = NULL;
531 struct lov_user_md *lump = (struct lov_user_md *)arg;
532 struct lov_mds_md *lmm;
534 struct mds_body *body;
538 valid |= OBD_MD_FLDIREA;
540 ll_inode2id(&id, inode);
541 rc = md_getattr(sbi->ll_md_exp, &id, valid, NULL, 0,
542 obd_size_diskmd(sbi->ll_dt_exp, NULL),
545 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
549 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
550 LASSERT(body != NULL); /* checked by md_getattr_lock */
551 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
553 lmmsize = body->eadatasize;
555 GOTO(out_get, rc = -ENODATA);
557 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
558 LASSERT(lmm != NULL);
559 LASSERT_REPSWABBED(request, 1);
560 rc = copy_to_user(lump, lmm, lmmsize);
562 GOTO(out_get, rc = -EFAULT);
566 ptlrpc_req_finished(request);
569 case IOC_MDC_SHOWFID: {
570 struct lustre_id *idp = (struct lustre_id *)arg;
575 filename = getname((const char *)arg);
576 if (IS_ERR(filename))
577 RETURN(PTR_ERR(filename));
579 ll_inode2id(&id, inode);
581 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
583 GOTO(out_filename, rc);
585 rc = copy_to_user(idp, &id, sizeof(*idp));
587 GOTO(out_filename, rc = -EFAULT);
594 case IOC_MDC_GETSTRIPE: {
595 struct lov_user_md *lump = (struct lov_user_md *)arg;
596 struct ptlrpc_request *request = NULL;
597 struct lov_mds_md *lmm;
598 struct mds_body *body;
603 filename = getname((const char *)arg);
604 if (IS_ERR(filename))
605 RETURN(PTR_ERR(filename));
607 ll_inode2id(&id, inode);
608 rc = md_getattr_lock(sbi->ll_md_exp, &id, filename,
609 strlen(filename) + 1, OBD_MD_FLEASIZE,
610 obd_size_diskmd(sbi->ll_dt_exp, NULL),
613 CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
618 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
619 LASSERT(body != NULL); /* checked by md_getattr_lock */
620 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
622 lmmsize = body->eadatasize;
624 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
625 GOTO(out_req, rc = -ENODATA);
628 GOTO(out_req, rc = -EFBIG);
630 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
631 LASSERT(lmm != NULL);
632 LASSERT_REPSWABBED(request, 1);
634 rc = copy_to_user(lump, lmm, lmmsize);
636 GOTO(out_req, rc = -EFAULT);
640 ptlrpc_req_finished(request);
646 struct ptlrpc_request *req = NULL;
649 struct client_obd *cli;
650 struct obd_device *obd;
652 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
657 obd = class_name2obd(data->ioc_inlbuf1);
660 GOTO(out_ping, rc = -ENODEV);
662 if (!obd->obd_attached) {
663 CERROR("Device %d not attached\n", obd->obd_minor);
664 GOTO(out_ping, rc = -ENODEV);
666 if (!obd->obd_set_up) {
667 CERROR("Device %d still not setup\n", obd->obd_minor);
668 GOTO(out_ping, rc = -ENODEV);
671 req = ptlrpc_prep_req(cli->cl_import, LUSTRE_OBD_VERSION,
672 OBD_PING, 0, NULL, NULL);
674 GOTO(out_ping, rc = -ENOMEM);
676 req->rq_replen = lustre_msg_size(0, NULL);
677 req->rq_send_state = LUSTRE_IMP_FULL;
679 rc = ptlrpc_queue_wait(req);
681 ptlrpc_req_finished(req);
683 obd_ioctl_freedata(buf, len);
686 case OBD_IOC_LLOG_CATINFO: {
687 struct ptlrpc_request *req = NULL;
693 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
698 if (!data->ioc_inlbuf1) {
699 obd_ioctl_freedata(buf, len);
703 lens[0] = data->ioc_inllen1;
704 bufs[0] = data->ioc_inlbuf1;
705 if (data->ioc_inllen2) {
706 lens[1] = data->ioc_inllen2;
707 bufs[1] = data->ioc_inlbuf2;
712 size = data->ioc_plen1;
713 req = ptlrpc_prep_req(sbi2md(sbi)->cl_import,
714 LUSTRE_LOG_VERSION, LLOG_CATINFO,
717 GOTO(out_catinfo, rc = -ENOMEM);
719 req->rq_replen = lustre_msg_size(1, &size);
721 rc = ptlrpc_queue_wait(req);
722 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
724 rc = copy_to_user(data->ioc_pbuf1, str,
726 ptlrpc_req_finished(req);
728 obd_ioctl_freedata(buf, len);
731 case LL_IOC_FLUSH_CRED:
732 RETURN(ll_flush_cred(inode));
734 return obd_iocontrol(cmd, sbi->ll_dt_exp, 0,
739 int ll_dir_open(struct inode *inode, struct file *file)
741 return ll_file_open(inode, file);
744 int ll_dir_release(struct inode *inode, struct file *file)
746 return ll_file_release(inode, file);
749 struct file_operations ll_dir_operations = {
751 .release = ll_dir_release,
752 .read = generic_read_dir,
753 .readdir = ll_readdir,
754 .ioctl = ll_dir_ioctl