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 "llite_internal.h"
55 typedef struct ext2_dir_entry_2 ext2_dirent;
57 #define PageChecked(page) test_bit(PG_checked, &(page)->flags)
58 #define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
60 /* returns the page unlocked, but with a reference */
61 static int ll_dir_readpage(struct file *file, struct page *page)
63 struct inode *inode = page->mapping->host;
64 struct ptlrpc_request *request;
65 struct mds_body *body;
71 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
72 inode->i_generation, inode);
74 mdc_pack_id(&id, inode->i_ino, inode->i_generation,
75 S_IFDIR, id_group(&ll_i2info(inode)->lli_id),
76 id_fid(&ll_i2info(inode)->lli_id));
78 offset = page->index << PAGE_SHIFT;
79 rc = md_readpage(ll_i2sbi(inode)->ll_lmv_exp, &id, offset,
82 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
83 LASSERT (body != NULL); /* checked by md_readpage() */
84 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
86 #warning "FIXME ASAP!"
87 //inode->i_size = body->size;
88 SetPageUptodate(page);
90 ptlrpc_req_finished(request);
96 struct address_space_operations ll_dir_aops = {
97 .readpage = ll_dir_readpage,
101 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
102 * more robust, but we have what we have
104 static inline unsigned ext2_chunk_size(struct inode *inode)
106 return inode->i_sb->s_blocksize;
109 static inline void ext2_put_page(struct page *page)
112 page_cache_release(page);
115 static inline unsigned long dir_pages(struct inode *inode)
117 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
120 static void ext2_check_page(struct page *page)
122 struct inode *dir = page->mapping->host;
123 unsigned chunk_size = ext2_chunk_size(dir);
124 char *kaddr = page_address(page);
125 // u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
126 unsigned offs, rec_len;
127 unsigned limit = PAGE_CACHE_SIZE;
131 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
132 limit = dir->i_size & ~PAGE_CACHE_MASK;
133 if (limit & (chunk_size - 1)) {
134 CERROR("limit %d dir size %lld index %ld\n",
135 limit, dir->i_size, page->index);
138 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
139 ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
140 p->rec_len = cpu_to_le16(chunk_size);
147 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
148 p = (ext2_dirent *)(kaddr + offs);
149 rec_len = le16_to_cpu(p->rec_len);
151 if (rec_len < EXT2_DIR_REC_LEN(1))
155 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
157 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
159 // if (le32_to_cpu(p->inode) > max_inumber)
165 SetPageChecked(page);
168 /* Too bad, we had an error */
171 CERROR("ext2_check_page"
172 "size of directory #%lu is not a multiple of chunk size\n",
177 error = "rec_len is smaller than minimal";
180 error = "unaligned directory entry";
183 error = "rec_len is too small for name_len";
186 error = "directory entry across blocks";
189 // error = "inode out of bounds";
191 CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
192 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
193 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
194 (unsigned long) le32_to_cpu(p->inode),
195 rec_len, p->name_len);
198 p = (ext2_dirent *)(kaddr + offs);
199 CERROR("ext2_check_page"
200 "entry in directory #%lu spans the page boundary"
201 "offset=%lu, inode=%lu",
202 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
203 (unsigned long) le32_to_cpu(p->inode));
205 SetPageChecked(page);
209 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
211 struct ll_inode_info *li = ll_i2info(dir);
212 struct ldlm_res_id res_id =
213 { .name = { id_fid(&li->lli_id), id_group(&li->lli_id)} };
214 struct lustre_handle lockh;
215 struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_lmv_exp);
216 struct address_space *mapping = dir->i_mapping;
218 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
221 obddev = md_get_real_obd(ll_i2sbi(dir)->ll_lmv_exp, NULL, 0);
222 rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
223 &res_id, LDLM_IBITS, &policy, LCK_PR, &lockh);
225 struct lookup_intent it = { .it_op = IT_READDIR };
226 struct ptlrpc_request *request;
227 struct mdc_op_data *op_data;
229 OBD_ALLOC(op_data, sizeof(*op_data));
231 RETURN(ERR_PTR(-ENOMEM));
233 ll_prepare_mdc_data(op_data, dir, NULL, NULL, 0, 0);
235 rc = md_enqueue(ll_i2sbi(dir)->ll_lmv_exp, LDLM_IBITS, &it,
236 LCK_PR, op_data, &lockh, NULL, 0,
237 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
238 OBD_FREE(op_data, sizeof(*op_data));
240 request = (struct ptlrpc_request *)it.d.lustre.it_data;
242 ptlrpc_req_finished(request);
244 CERROR("lock enqueue: rc: %d\n", rc);
248 ldlm_lock_dump_handle(D_OTHER, &lockh);
250 page = read_cache_page(mapping, n,
251 (filler_t*)mapping->a_ops->readpage, NULL);
255 if (!PageUptodate(page))
257 if (!PageChecked(page))
258 ext2_check_page(page);
264 ldlm_lock_decref(&lockh, LCK_PR);
269 page = ERR_PTR(-EIO);
274 * p is at least 6 bytes before the end of page
276 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
278 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
281 static inline unsigned
282 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
284 ext2_dirent *de = (ext2_dirent*)(base + offset);
285 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
286 while ((char*)p < (char*)de)
287 p = ext2_next_entry(p);
288 return (char *)p - base;
291 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
292 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
293 [EXT2_FT_REG_FILE] DT_REG,
294 [EXT2_FT_DIR] DT_DIR,
295 [EXT2_FT_CHRDEV] DT_CHR,
296 [EXT2_FT_BLKDEV] DT_BLK,
297 [EXT2_FT_FIFO] DT_FIFO,
298 [EXT2_FT_SOCK] DT_SOCK,
299 [EXT2_FT_SYMLINK] DT_LNK,
303 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
305 struct inode *inode = filp->f_dentry->d_inode;
306 loff_t pos = filp->f_pos;
307 // XXX struct super_block *sb = inode->i_sb;
308 unsigned offset = pos & ~PAGE_CACHE_MASK;
309 unsigned long n = pos >> PAGE_CACHE_SHIFT;
310 unsigned long npages = dir_pages(inode);
311 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
312 unsigned char *types = ext2_filetype_table;
313 int need_revalidate = (filp->f_version != inode->i_version);
317 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
318 inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
320 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
323 for ( ; n < npages; n++, offset = 0) {
328 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
329 PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
330 n, npages, inode->i_size);
331 page = ll_get_dir_page(inode, n);
333 /* size might have been updated by md_readpage() */
334 npages = dir_pages(inode);
338 CERROR("error reading dir %lu/%u page %lu: rc %d\n",
339 inode->i_ino, inode->i_generation, n, rc);
343 kaddr = page_address(page);
344 if (need_revalidate) {
345 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
348 de = (ext2_dirent *)(kaddr+offset);
349 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
350 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
354 rc = 0; /* no error if we return something */
356 offset = (char *)de - kaddr;
357 over = filldir(dirent, de->name, de->name_len,
358 (n<<PAGE_CACHE_SHIFT) | offset,
359 le32_to_cpu(de->inode),
360 types[de->file_type &
374 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
375 filp->f_version = inode->i_version;
380 static int ll_mkdir_stripe(struct inode *inode, unsigned long arg)
382 struct ptlrpc_request *request = NULL;
383 struct ll_sb_info *sbi = ll_i2sbi(inode);
384 struct ll_user_mkdir_stripe lums;
385 struct mdc_op_data *op_data;
392 if (copy_from_user(&lums, (void *)arg, sizeof(lums)))
395 if (lums.lums_namelen <= 0)
397 OBD_ALLOC(name, lums.lums_namelen);
401 if (copy_from_user(name, lums.lums_name, lums.lums_namelen))
402 GOTO(out, err = -EFAULT);
404 CDEBUG(D_VFSTRACE, "ioctl Op:name=%s,dir=%lu/%u(%p)\n",
405 name, inode->i_ino, inode->i_generation, inode);
406 nstripes = lums.lums_nstripes;
408 mode = lums.lums_mode;
409 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
411 OBD_ALLOC(op_data, sizeof(*op_data));
413 GOTO(out, err = -ENOMEM);
414 ll_prepare_mdc_data(op_data, inode, NULL, name,lums.lums_namelen,0);
415 err = md_create(sbi->ll_lmv_exp, op_data, &nstripes, sizeof(nstripes),
416 mode, current->fsuid, current->fsgid, 0, &request);
417 OBD_FREE(op_data, sizeof(*op_data));
418 ptlrpc_req_finished(request);
421 OBD_FREE(name, lums.lums_namelen);
425 static int ll_dir_ioctl(struct inode *inode, struct file *file,
426 unsigned int cmd, unsigned long arg)
428 struct ll_sb_info *sbi = ll_i2sbi(inode);
429 struct obd_ioctl_data *data;
432 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
433 inode->i_ino, inode->i_generation, inode, cmd);
435 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
438 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
440 case EXT3_IOC_GETFLAGS:
441 case EXT3_IOC_SETFLAGS:
442 RETURN(ll_iocontrol(inode, file, cmd, arg));
443 case IOC_MDC_LOOKUP: {
444 struct ptlrpc_request *request = NULL;
448 int namelen, rc, len = 0;
451 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
456 filename = data->ioc_inlbuf1;
457 namelen = data->ioc_inllen1;
460 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
461 GOTO(out, rc = -EINVAL);
465 ll_inode2id(&id, inode);
466 rc = md_getattr_lock(sbi->ll_lmv_exp, &id,
467 filename, namelen, valid, 0, &request);
469 CDEBUG(D_INFO, "md_getattr_lock: %d\n", rc);
473 ptlrpc_req_finished(request);
477 obd_ioctl_freedata(buf, len);
480 case LL_IOC_MDC_MKDIRSTRIPE:
481 RETURN(ll_mkdir_stripe(inode, arg));
482 case IOC_MDC_FINISH_GNS:
483 RETURN(ll_finish_gns(sbi));
484 case LL_IOC_LOV_SETSTRIPE: {
485 struct ptlrpc_request *request = NULL;
486 struct mdc_op_data *op_data;
487 struct iattr attr = { 0 };
488 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
491 LASSERT(sizeof(lum) == sizeof(*lump));
492 LASSERT(sizeof(lum.lmm_objects[0]) ==
493 sizeof(lump->lmm_objects[0]));
494 rc = copy_from_user(&lum, lump, sizeof(lum));
498 if (lum.lmm_magic != LOV_USER_MAGIC)
501 OBD_ALLOC(op_data, sizeof(*op_data));
505 ll_prepare_mdc_data(op_data, inode, NULL, NULL, 0, 0);
507 rc = md_setattr(sbi->ll_lmv_exp, op_data, &attr, &lum,
508 sizeof(lum), NULL, 0, &request);
509 OBD_FREE(op_data, sizeof(*op_data));
510 ptlrpc_req_finished(request);
513 if (rc != -EPERM && rc != -EACCES)
514 CERROR("md_setattr fails: rc = %d\n", rc);
518 case LL_IOC_LOV_GETSTRIPE: {
519 struct ptlrpc_request *request = NULL;
520 struct lov_user_md *lump = (struct lov_user_md *)arg;
521 struct lov_mds_md *lmm;
523 struct mds_body *body;
527 valid |= OBD_MD_FLDIREA;
529 ll_inode2id(&id, inode);
530 rc = md_getattr(sbi->ll_lmv_exp, &id, valid,
531 obd_size_diskmd(sbi->ll_lov_exp, NULL),
534 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
538 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
539 LASSERT(body != NULL); /* checked by md_getattr_lock */
540 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
542 lmmsize = body->eadatasize;
544 GOTO(out_get, rc = -ENODATA);
546 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
547 LASSERT(lmm != NULL);
548 LASSERT_REPSWABBED(request, 1);
549 rc = copy_to_user(lump, lmm, lmmsize);
551 GOTO(out_get, rc = -EFAULT);
555 ptlrpc_req_finished(request);
558 case IOC_MDC_SHOWFID: {
559 struct lustre_id *idp = (struct lustre_id *)arg;
564 filename = getname((const char *)arg);
565 if (IS_ERR(filename))
566 RETURN(PTR_ERR(filename));
568 ll_inode2id(&id, inode);
570 rc = ll_get_fid(sbi->ll_lmv_exp, &id, filename, &id);
572 GOTO(out_filename, rc);
574 rc = copy_to_user(idp, &id, sizeof(*idp));
576 GOTO(out_filename, rc = -EFAULT);
583 case IOC_MDC_GETSTRIPE: {
584 struct lov_user_md *lump = (struct lov_user_md *)arg;
585 struct ptlrpc_request *request = NULL;
586 struct lov_mds_md *lmm;
587 struct mds_body *body;
592 filename = getname((const char *)arg);
593 if (IS_ERR(filename))
594 RETURN(PTR_ERR(filename));
596 ll_inode2id(&id, inode);
597 rc = md_getattr_lock(sbi->ll_lmv_exp, &id, filename,
598 strlen(filename) + 1, OBD_MD_FLEASIZE,
599 obd_size_diskmd(sbi->ll_lov_exp, NULL),
602 CDEBUG(D_INFO, "md_getattr_lock failed on %s: rc %d\n",
607 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
608 LASSERT(body != NULL); /* checked by md_getattr_lock */
609 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_lock */
611 lmmsize = body->eadatasize;
613 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
614 GOTO(out_req, rc = -ENODATA);
617 GOTO(out_req, rc = -EFBIG);
619 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
620 LASSERT(lmm != NULL);
621 LASSERT_REPSWABBED(request, 1);
623 rc = copy_to_user(lump, lmm, lmmsize);
625 GOTO(out_req, rc = -EFAULT);
629 ptlrpc_req_finished(request);
635 struct ptlrpc_request *req = NULL;
638 struct client_obd *cli;
639 struct obd_device *obd;
641 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
646 obd = class_name2obd(data->ioc_inlbuf1);
649 GOTO(out_ping, rc = -ENODEV);
651 if (!obd->obd_attached) {
652 CERROR("Device %d not attached\n", obd->obd_minor);
653 GOTO(out_ping, rc = -ENODEV);
655 if (!obd->obd_set_up) {
656 CERROR("Device %d still not setup\n", obd->obd_minor);
657 GOTO(out_ping, rc = -ENODEV);
660 req = ptlrpc_prep_req(cli->cl_import, LUSTRE_OBD_VERSION,
661 OBD_PING, 0, NULL, NULL);
663 GOTO(out_ping, rc = -ENOMEM);
665 req->rq_replen = lustre_msg_size(0, NULL);
666 req->rq_send_state = LUSTRE_IMP_FULL;
668 rc = ptlrpc_queue_wait(req);
670 ptlrpc_req_finished(req);
672 obd_ioctl_freedata(buf, len);
675 case OBD_IOC_LLOG_CATINFO: {
676 struct ptlrpc_request *req = NULL;
682 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
687 if (!data->ioc_inlbuf1) {
688 obd_ioctl_freedata(buf, len);
692 lens[0] = data->ioc_inllen1;
693 bufs[0] = data->ioc_inlbuf1;
694 if (data->ioc_inllen2) {
695 lens[1] = data->ioc_inllen2;
696 bufs[1] = data->ioc_inlbuf2;
701 size = data->ioc_plen1;
702 req = ptlrpc_prep_req(sbi2lmv(sbi)->cl_import,
703 LUSTRE_LOG_VERSION, LLOG_CATINFO,
706 GOTO(out_catinfo, rc = -ENOMEM);
708 req->rq_replen = lustre_msg_size(1, &size);
710 rc = ptlrpc_queue_wait(req);
711 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
713 rc = copy_to_user(data->ioc_pbuf1, str,
715 ptlrpc_req_finished(req);
717 obd_ioctl_freedata(buf, len);
721 return obd_iocontrol(cmd, sbi->ll_lov_exp, 0,
726 int ll_dir_open(struct inode *inode, struct file *file)
729 RETURN(ll_file_open(inode, file));
732 int ll_dir_release(struct inode *inode, struct file *file)
735 RETURN(ll_file_release(inode, file));
738 struct file_operations ll_dir_operations = {
740 .release = ll_dir_release,
741 .read = generic_read_dir,
742 .readdir = ll_readdir,
743 .ioctl = ll_dir_ioctl