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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
37 # include <linux/locks.h> // for wait_on_buffer
39 # include <linux/buffer_head.h> // for wait_on_buffer
42 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <linux/obd_support.h>
45 #include <linux/obd_class.h>
46 #include <linux/lustre_lib.h>
47 #include <linux/lustre_idl.h>
48 #include <linux/lustre_mds.h>
49 #include <linux/lustre_lite.h>
50 #include <linux/lustre_dlm.h>
51 #include "llite_internal.h"
53 typedef struct ext2_dir_entry_2 ext2_dirent;
55 #define PageChecked(page) test_bit(PG_checked, &(page)->flags)
56 #define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
58 /* returns the page unlocked, but with a reference */
59 static int ll_dir_readpage(struct file *file, struct page *page)
61 struct inode *inode = page->mapping->host;
62 struct ll_fid mdc_fid;
64 struct ptlrpc_request *request;
65 struct mds_body *body;
69 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
70 inode->i_generation, inode);
72 mdc_pack_fid(&mdc_fid, inode->i_ino, inode->i_generation, S_IFDIR);
73 mdc_fid.mds = ll_i2info(inode)->lli_mds;
74 offset = page->index << PAGE_SHIFT;
75 rc = md_readpage(ll_i2sbi(inode)->ll_mdc_exp, &mdc_fid,
76 offset, page, &request);
78 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
79 LASSERT (body != NULL); /* checked by md_readpage() */
80 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
82 #warning "FIXME ASAP!"
83 //inode->i_size = body->size;
84 SetPageUptodate(page);
86 ptlrpc_req_finished(request);
93 struct address_space_operations ll_dir_aops = {
94 .readpage = ll_dir_readpage,
98 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
99 * more robust, but we have what we have
101 static inline unsigned ext2_chunk_size(struct inode *inode)
103 return inode->i_sb->s_blocksize;
106 static inline void ext2_put_page(struct page *page)
109 page_cache_release(page);
112 static inline unsigned long dir_pages(struct inode *inode)
114 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
118 static void ext2_check_page(struct page *page)
120 struct inode *dir = page->mapping->host;
121 unsigned chunk_size = ext2_chunk_size(dir);
122 char *kaddr = page_address(page);
123 // u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
124 unsigned offs, rec_len;
125 unsigned limit = PAGE_CACHE_SIZE;
129 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
130 limit = dir->i_size & ~PAGE_CACHE_MASK;
131 if (limit & (chunk_size - 1)) {
132 CERROR("limit %d dir size %lld index %ld\n",
133 limit, dir->i_size, page->index);
136 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
137 ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
138 p->rec_len = cpu_to_le16(chunk_size);
145 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
146 p = (ext2_dirent *)(kaddr + offs);
147 rec_len = le16_to_cpu(p->rec_len);
149 if (rec_len < EXT2_DIR_REC_LEN(1))
153 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
155 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
157 // if (le32_to_cpu(p->inode) > max_inumber)
163 SetPageChecked(page);
166 /* Too bad, we had an error */
169 CERROR("ext2_check_page"
170 "size of directory #%lu is not a multiple of chunk size\n",
175 error = "rec_len is smaller than minimal";
178 error = "unaligned directory entry";
181 error = "rec_len is too small for name_len";
184 error = "directory entry across blocks";
187 // error = "inode out of bounds";
189 CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
190 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
191 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
192 (unsigned long) le32_to_cpu(p->inode),
193 rec_len, p->name_len);
196 p = (ext2_dirent *)(kaddr + offs);
197 CERROR("ext2_check_page"
198 "entry in directory #%lu spans the page boundary"
199 "offset=%lu, inode=%lu",
200 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
201 (unsigned long) le32_to_cpu(p->inode));
203 SetPageChecked(page);
207 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
209 struct ldlm_res_id res_id =
210 { .name = { dir->i_ino, (__u64)dir->i_generation} };
211 struct lustre_handle lockh;
212 struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_mdc_exp);
213 struct address_space *mapping = dir->i_mapping;
215 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
218 obddev = md_get_real_obd(ll_i2sbi(dir)->ll_mdc_exp, NULL, 0);
219 rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
220 &res_id, LDLM_IBITS, &policy, LCK_PR, &lockh);
222 struct lookup_intent it = { .it_op = IT_READDIR };
223 struct ptlrpc_request *request;
224 struct mdc_op_data data;
226 ll_prepare_mdc_op_data(&data, dir, NULL, NULL, 0, 0);
228 rc = md_enqueue(ll_i2sbi(dir)->ll_mdc_exp, LDLM_IBITS, &it,
229 LCK_PR, &data, &lockh, NULL, 0,
230 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
232 request = (struct ptlrpc_request *)it.d.lustre.it_data;
234 ptlrpc_req_finished(request);
236 CERROR("lock enqueue: rc: %d\n", rc);
240 ldlm_lock_dump_handle(D_OTHER, &lockh);
242 page = read_cache_page(mapping, n,
243 (filler_t*)mapping->a_ops->readpage, NULL);
247 if (!PageUptodate(page))
249 if (!PageChecked(page))
250 ext2_check_page(page);
256 ldlm_lock_decref(&lockh, LCK_PR);
261 page = ERR_PTR(-EIO);
266 * p is at least 6 bytes before the end of page
268 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
270 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
273 static inline unsigned
274 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
276 ext2_dirent *de = (ext2_dirent*)(base + offset);
277 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
278 while ((char*)p < (char*)de)
279 p = ext2_next_entry(p);
280 return (char *)p - base;
283 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
284 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
285 [EXT2_FT_REG_FILE] DT_REG,
286 [EXT2_FT_DIR] DT_DIR,
287 [EXT2_FT_CHRDEV] DT_CHR,
288 [EXT2_FT_BLKDEV] DT_BLK,
289 [EXT2_FT_FIFO] DT_FIFO,
290 [EXT2_FT_SOCK] DT_SOCK,
291 [EXT2_FT_SYMLINK] DT_LNK,
295 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
297 struct inode *inode = filp->f_dentry->d_inode;
298 loff_t pos = filp->f_pos;
299 // XXX struct super_block *sb = inode->i_sb;
300 unsigned offset = pos & ~PAGE_CACHE_MASK;
301 unsigned long n = pos >> PAGE_CACHE_SHIFT;
302 unsigned long npages = dir_pages(inode);
303 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
304 unsigned char *types = NULL;
305 int need_revalidate = (filp->f_version != inode->i_version);
309 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
310 inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
312 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
315 types = ext2_filetype_table;
317 for ( ; n < npages; n++, offset = 0) {
322 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
323 PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
324 n, npages, inode->i_size);
325 page = ll_get_dir_page(inode, n);
327 /* size might have been updated by md_readpage() */
328 npages = dir_pages(inode);
332 CERROR("error reading dir %lu/%u page %lu: rc %d\n",
333 inode->i_ino, inode->i_generation, n, rc);
337 kaddr = page_address(page);
338 if (need_revalidate) {
339 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
342 de = (ext2_dirent *)(kaddr+offset);
343 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
344 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
347 unsigned char d_type = DT_UNKNOWN;
349 rc = 0; /* no error if we return something */
350 if (types && de->file_type < EXT2_FT_MAX)
351 d_type = types[de->file_type];
353 offset = (char *)de - kaddr;
354 over = filldir(dirent, de->name, de->name_len,
355 (n<<PAGE_CACHE_SHIFT) | offset,
356 le32_to_cpu(de->inode), d_type);
367 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
368 filp->f_version = inode->i_version;
373 static int ll_mkdir_stripe(struct inode *inode, unsigned long arg)
376 struct ll_user_mkdir_stripe lums;
377 struct ptlrpc_request *request = NULL;
378 struct ll_sb_info *sbi = ll_i2sbi(inode);
379 struct mdc_op_data op_data;
385 if (copy_from_user(&lums, (void *)arg, sizeof(lums)))
388 if (lums.lums_namelen <= 0)
390 OBD_ALLOC(name, lums.lums_namelen);
394 if (copy_from_user(name, lums.lums_name, lums.lums_namelen))
395 GOTO(out, err=-EFAULT);
397 CDEBUG(D_VFSTRACE, "ioctl Op:name=%s,dir=%lu/%u(%p)\n",
398 name, inode->i_ino, inode->i_generation, inode);
399 nstripes = lums.lums_nstripes;
401 mode = lums.lums_mode;
402 mode = (mode & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
403 ll_prepare_mdc_op_data(&op_data, inode, NULL, name,lums.lums_namelen,0);
404 err = md_create(sbi->ll_mdc_exp, &op_data, &nstripes, sizeof(nstripes),
405 mode, current->fsuid, current->fsgid, 0, &request);
406 ptlrpc_req_finished(request);
409 OBD_FREE(name, lums.lums_namelen);
413 static int ll_dir_ioctl(struct inode *inode, struct file *file,
414 unsigned int cmd, unsigned long arg)
416 struct ll_sb_info *sbi = ll_i2sbi(inode);
417 struct obd_ioctl_data *data;
420 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
421 inode->i_ino, inode->i_generation, inode, cmd);
423 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
426 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
428 case EXT3_IOC_GETFLAGS:
429 case EXT3_IOC_SETFLAGS:
430 RETURN(ll_iocontrol(inode, file, cmd, arg));
431 case IOC_MDC_LOOKUP: {
432 struct ptlrpc_request *request = NULL;
436 int namelen, rc, len = 0;
439 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
444 filename = data->ioc_inlbuf1;
445 namelen = data->ioc_inllen1;
448 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
449 GOTO(out, rc = -EINVAL);
453 ll_inode2fid(&fid, inode);
454 rc = md_getattr_name(sbi->ll_mdc_exp, &fid,
455 filename, namelen, valid, 0, &request);
457 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
461 ptlrpc_req_finished(request);
465 obd_ioctl_freedata(buf, len);
468 case LL_IOC_MDC_MKDIRSTRIPE:
469 RETURN(ll_mkdir_stripe(inode, arg));
470 case LL_IOC_LOV_SETSTRIPE: {
471 struct ptlrpc_request *request = NULL;
472 struct mdc_op_data op_data;
473 struct iattr attr = { 0 };
474 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
477 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
479 LASSERT(sizeof(lum) == sizeof(*lump));
480 LASSERT(sizeof(lum.lmm_objects[0]) ==
481 sizeof(lump->lmm_objects[0]));
482 rc = copy_from_user(&lum, lump, sizeof(lum));
486 if (lum.lmm_magic != LOV_USER_MAGIC)
489 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
490 &attr, &lum, sizeof(lum), NULL, 0, &request);
492 ptlrpc_req_finished(request);
493 if (rc != -EPERM && rc != -EACCES)
494 CERROR("md_setattr fails: rc = %d\n", rc);
497 ptlrpc_req_finished(request);
501 case LL_IOC_LOV_GETSTRIPE: {
502 struct ptlrpc_request *request = NULL;
503 struct lov_user_md *lump = (struct lov_user_md *)arg;
504 struct lov_mds_md *lmm;
506 struct mds_body *body;
507 unsigned long valid = 0;
510 valid |= OBD_MD_FLDIREA;
512 ll_inode2fid(&fid, inode);
513 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid,
514 obd_size_diskmd(sbi->ll_osc_exp, NULL),
517 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
521 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
522 LASSERT(body != NULL); /* checked by md_getattr_name */
523 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_name */
525 lmmsize = body->eadatasize;
527 GOTO(out_get, rc = -ENODATA);
529 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
530 LASSERT(lmm != NULL);
531 LASSERT_REPSWABBED(request, 1);
532 rc = copy_to_user(lump, lmm, lmmsize);
534 GOTO(out_get, rc = -EFAULT);
538 ptlrpc_req_finished(request);
541 case IOC_MDC_GETSTRIPE: {
542 struct ptlrpc_request *request = NULL;
544 struct mds_body *body;
545 struct lov_user_md *lump = (struct lov_user_md *)arg;
546 struct lov_mds_md *lmm;
550 filename = getname((const char *)arg);
551 if (IS_ERR(filename))
552 RETURN(PTR_ERR(filename));
554 ll_inode2fid(&fid, inode);
555 rc = md_getattr_name(sbi->ll_mdc_exp, &fid, filename,
556 strlen(filename) + 1, OBD_MD_FLEASIZE,
557 obd_size_diskmd(sbi->ll_osc_exp, NULL),
560 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
565 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
566 LASSERT(body != NULL); /* checked by md_getattr_name */
567 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_name */
569 lmmsize = body->eadatasize;
571 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
572 GOTO(out_req, rc = -ENODATA);
575 GOTO(out_req, rc = -EFBIG);
577 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
578 LASSERT(lmm != NULL);
579 LASSERT_REPSWABBED(request, 1);
581 rc = copy_to_user(lump, lmm, lmmsize);
583 GOTO(out_req, rc = -EFAULT);
587 ptlrpc_req_finished(request);
593 struct ptlrpc_request *req = NULL;
596 struct client_obd *cli;
597 struct obd_device *obd;
599 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
604 obd = class_name2obd(data->ioc_inlbuf1);
607 GOTO(out_ping, rc = -ENODEV);
609 if (!obd->obd_attached) {
610 CERROR("Device %d not attached\n", obd->obd_minor);
611 GOTO(out_ping, rc = -ENODEV);
613 if (!obd->obd_set_up) {
614 CERROR("Device %d still not setup\n", obd->obd_minor);
615 GOTO(out_ping, rc = -ENODEV);
618 req = ptlrpc_prep_req(cli->cl_import, OBD_PING, 0, NULL, NULL);
620 GOTO(out_ping, rc = -ENOMEM);
622 req->rq_replen = lustre_msg_size(0, NULL);
623 req->rq_send_state = LUSTRE_IMP_FULL;
625 rc = ptlrpc_queue_wait(req);
627 ptlrpc_req_finished(req);
629 obd_ioctl_freedata(buf, len);
632 case OBD_IOC_LLOG_CATINFO: {
633 struct ptlrpc_request *req = NULL;
639 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
644 if (!data->ioc_inlbuf1) {
645 obd_ioctl_freedata(buf, len);
649 lens[0] = data->ioc_inllen1;
650 bufs[0] = data->ioc_inlbuf1;
651 if (data->ioc_inllen2) {
652 lens[1] = data->ioc_inllen2;
653 bufs[1] = data->ioc_inlbuf2;
658 size = data->ioc_plen1;
659 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import, LLOG_CATINFO,
662 GOTO(out_catinfo, rc = -ENOMEM);
663 req->rq_replen = lustre_msg_size(1, &size);
665 rc = ptlrpc_queue_wait(req);
666 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
668 rc = copy_to_user(data->ioc_pbuf1, str,
670 ptlrpc_req_finished(req);
672 obd_ioctl_freedata(buf, len);
676 return obd_iocontrol(cmd, sbi->ll_osc_exp,0,NULL,(void *)arg);
680 int ll_dir_open(struct inode *inode, struct file *file)
683 RETURN(ll_file_open(inode, file));
686 int ll_dir_release(struct inode *inode, struct file *file)
689 RETURN(ll_file_release(inode, file));
692 struct file_operations ll_dir_operations = {
694 .release = ll_dir_release,
695 .read = generic_read_dir,
696 .readdir = ll_readdir,
697 .ioctl = ll_dir_ioctl