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);
74 offset = page->index << PAGE_SHIFT;
75 rc = mdc_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 mdc_readpage() */
80 LASSERT_REPSWABBED (request, 0); /* swabbed by mdc_readpage() */
82 inode->i_size = body->size;
83 SetPageUptodate(page);
85 ptlrpc_req_finished(request);
92 struct address_space_operations ll_dir_aops = {
93 .readpage = ll_dir_readpage,
97 * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
98 * more robust, but we have what we have
100 static inline unsigned ext2_chunk_size(struct inode *inode)
102 return inode->i_sb->s_blocksize;
105 static inline void ext2_put_page(struct page *page)
108 page_cache_release(page);
111 static inline unsigned long dir_pages(struct inode *inode)
113 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
117 static void ext2_check_page(struct page *page)
119 struct inode *dir = page->mapping->host;
120 unsigned chunk_size = ext2_chunk_size(dir);
121 char *kaddr = page_address(page);
122 // u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
123 unsigned offs, rec_len;
124 unsigned limit = PAGE_CACHE_SIZE;
128 if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
129 limit = dir->i_size & ~PAGE_CACHE_MASK;
130 if (limit & (chunk_size - 1)) {
131 CERROR("limit %d dir size %lld index %ld\n",
132 limit, dir->i_size, page->index);
135 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
136 ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
137 p->rec_len = cpu_to_le16(chunk_size);
144 for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
145 p = (ext2_dirent *)(kaddr + offs);
146 rec_len = le16_to_cpu(p->rec_len);
148 if (rec_len < EXT2_DIR_REC_LEN(1))
152 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
154 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
156 // if (le32_to_cpu(p->inode) > max_inumber)
162 SetPageChecked(page);
165 /* Too bad, we had an error */
168 CERROR("ext2_check_page"
169 "size of directory #%lu is not a multiple of chunk size\n",
174 error = "rec_len is smaller than minimal";
177 error = "unaligned directory entry";
180 error = "rec_len is too small for name_len";
183 error = "directory entry across blocks";
186 // error = "inode out of bounds";
188 CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
189 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
190 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
191 (unsigned long) le32_to_cpu(p->inode),
192 rec_len, p->name_len);
195 p = (ext2_dirent *)(kaddr + offs);
196 CERROR("ext2_check_page"
197 "entry in directory #%lu spans the page boundary"
198 "offset=%lu, inode=%lu",
199 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
200 (unsigned long) le32_to_cpu(p->inode));
202 SetPageChecked(page);
206 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
208 struct ldlm_res_id res_id =
209 { .name = { dir->i_ino, (__u64)dir->i_generation} };
210 struct lustre_handle lockh;
211 struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_mdc_exp);
212 struct address_space *mapping = dir->i_mapping;
216 rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
217 &res_id, LDLM_PLAIN, NULL, LCK_PR, &lockh);
219 struct lookup_intent it = { .it_op = IT_READDIR };
220 struct ptlrpc_request *request;
221 struct mdc_op_data data;
223 ll_prepare_mdc_op_data(&data, dir, NULL, NULL, 0, 0);
225 rc = mdc_enqueue(ll_i2sbi(dir)->ll_mdc_exp, LDLM_PLAIN, &it,
226 LCK_PR, &data, &lockh, NULL, 0,
227 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
229 request = (struct ptlrpc_request *)it.d.lustre.it_data;
231 ptlrpc_req_finished(request);
233 CERROR("lock enqueue: rc: %d\n", rc);
237 ldlm_lock_dump_handle(D_OTHER, &lockh);
239 page = read_cache_page(mapping, n,
240 (filler_t*)mapping->a_ops->readpage, NULL);
244 if (!PageUptodate(page))
246 if (!PageChecked(page))
247 ext2_check_page(page);
253 ldlm_lock_decref(&lockh, LCK_PR);
258 page = ERR_PTR(-EIO);
263 * p is at least 6 bytes before the end of page
265 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
267 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
270 static inline unsigned
271 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
273 ext2_dirent *de = (ext2_dirent*)(base + offset);
274 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
275 while ((char*)p < (char*)de)
276 p = ext2_next_entry(p);
277 return (char *)p - base;
280 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
281 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
282 [EXT2_FT_REG_FILE] DT_REG,
283 [EXT2_FT_DIR] DT_DIR,
284 [EXT2_FT_CHRDEV] DT_CHR,
285 [EXT2_FT_BLKDEV] DT_BLK,
286 [EXT2_FT_FIFO] DT_FIFO,
287 [EXT2_FT_SOCK] DT_SOCK,
288 [EXT2_FT_SYMLINK] DT_LNK,
292 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
294 struct inode *inode = filp->f_dentry->d_inode;
295 loff_t pos = filp->f_pos;
296 // XXX struct super_block *sb = inode->i_sb;
297 unsigned offset = pos & ~PAGE_CACHE_MASK;
298 unsigned long n = pos >> PAGE_CACHE_SHIFT;
299 unsigned long npages = dir_pages(inode);
300 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
301 unsigned char *types = NULL;
302 int need_revalidate = (filp->f_version != inode->i_version);
306 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
307 inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
309 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
312 types = ext2_filetype_table;
314 for ( ; n < npages; n++, offset = 0) {
319 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
320 PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
321 n, npages, inode->i_size);
322 page = ll_get_dir_page(inode, n);
324 /* size might have been updated by mdc_readpage */
325 npages = dir_pages(inode);
329 CERROR("error reading dir %lu/%u page %lu: rc %d\n",
330 inode->i_ino, inode->i_generation, n, rc);
334 kaddr = page_address(page);
335 if (need_revalidate) {
336 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
339 de = (ext2_dirent *)(kaddr+offset);
340 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
341 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
344 unsigned char d_type = DT_UNKNOWN;
346 rc = 0; /* no error if we return something */
347 if (types && de->file_type < EXT2_FT_MAX)
348 d_type = types[de->file_type];
350 offset = (char *)de - kaddr;
351 over = filldir(dirent, de->name, de->name_len,
352 (n<<PAGE_CACHE_SHIFT) | offset,
353 le32_to_cpu(de->inode), d_type);
364 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
365 filp->f_version = inode->i_version;
370 static int ll_dir_ioctl(struct inode *inode, struct file *file,
371 unsigned int cmd, unsigned long arg)
373 struct ll_sb_info *sbi = ll_i2sbi(inode);
374 struct obd_ioctl_data *data;
377 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
378 inode->i_ino, inode->i_generation, inode, cmd);
380 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
383 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
385 case EXT3_IOC_GETFLAGS:
386 case EXT3_IOC_SETFLAGS:
387 RETURN(ll_iocontrol(inode, file, cmd, arg));
388 case EXT3_IOC_GETVERSION_OLD:
389 case EXT3_IOC_GETVERSION:
390 RETURN(put_user(inode->i_generation, (int *)arg));
391 /* We need to special case any other ioctls we want to handle,
392 * to send them to the MDS/OST as appropriate and to properly
393 * network encode the arg field.
394 case EXT3_IOC_SETVERSION_OLD:
395 case EXT3_IOC_SETVERSION:
397 case IOC_MDC_LOOKUP: {
398 struct ptlrpc_request *request = NULL;
402 int namelen, rc, len = 0;
405 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
410 filename = data->ioc_inlbuf1;
411 namelen = data->ioc_inllen1;
414 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
415 GOTO(out, rc = -EINVAL);
419 ll_inode2fid(&fid, inode);
420 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid,
421 filename, namelen, valid, 0, &request);
423 CDEBUG(D_INFO, "mdc_getattr_name: %d\n", rc);
427 ptlrpc_req_finished(request);
431 obd_ioctl_freedata(buf, len);
434 case LL_IOC_LOV_SETSTRIPE: {
435 struct ptlrpc_request *request = NULL;
436 struct mdc_op_data op_data;
437 struct iattr attr = { 0 };
438 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
441 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
443 LASSERT(sizeof(lum) == sizeof(*lump));
444 LASSERT(sizeof(lum.lmm_objects[0]) ==
445 sizeof(lump->lmm_objects[0]));
446 rc = copy_from_user(&lum, lump, sizeof(lum));
450 if (lum.lmm_magic != LOV_USER_MAGIC)
453 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
454 &attr, &lum, sizeof(lum), NULL, 0, &request);
456 ptlrpc_req_finished(request);
457 if (rc != -EPERM && rc != -EACCES)
458 CERROR("mdc_setattr fails: rc = %d\n", rc);
461 ptlrpc_req_finished(request);
465 case LL_IOC_LOV_GETSTRIPE: {
466 struct ptlrpc_request *request = NULL;
467 struct lov_user_md *lump = (struct lov_user_md *)arg;
468 struct lov_mds_md *lmm;
470 struct mds_body *body;
471 unsigned long valid = 0;
474 valid |= OBD_MD_FLDIREA;
476 ll_inode2fid(&fid, inode);
477 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid,
478 obd_size_diskmd(sbi->ll_osc_exp, NULL),
481 CDEBUG(D_INFO, "mdc_getattr failed: rc = %d\n", rc);
485 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
486 LASSERT(body != NULL); /* checked by mdc_getattr_name */
487 LASSERT_REPSWABBED(request, 0);/* swabbed by mdc_getattr_name */
489 lmmsize = body->eadatasize;
491 GOTO(out_get, rc = -ENODATA);
493 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
494 LASSERT(lmm != NULL);
495 LASSERT_REPSWABBED(request, 1);
496 rc = copy_to_user(lump, lmm, lmmsize);
498 GOTO(out_get, rc = -EFAULT);
502 ptlrpc_req_finished(request);
505 case IOC_MDC_GETFILEINFO:
506 case IOC_MDC_GETSTRIPE: {
507 struct ptlrpc_request *request = NULL;
509 struct mds_body *body;
510 struct lov_user_md *lump;
511 struct lov_mds_md *lmm;
515 filename = getname((const char *)arg);
516 if (IS_ERR(filename))
517 RETURN(PTR_ERR(filename));
519 ll_inode2fid(&fid, inode);
520 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, filename,
521 strlen(filename)+1, OBD_MD_FLEASIZE,
522 obd_size_diskmd(sbi->ll_osc_exp, NULL),
525 CDEBUG(D_INFO, "mdc_getattr_name failed on %s: rc %d\n",
530 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
531 LASSERT(body != NULL); /* checked by mdc_getattr_name */
532 LASSERT_REPSWABBED(request, 0);/* swabbed by mdc_getattr_name */
534 lmmsize = body->eadatasize;
536 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
537 GOTO(out_req, rc = -ENODATA);
540 GOTO(out_req, rc = -EFBIG);
542 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
543 LASSERT(lmm != NULL);
544 LASSERT_REPSWABBED(request, 1);
546 if (cmd == IOC_MDC_GETFILEINFO) {
547 struct lov_user_mds_data *lmdp;
551 st.st_mode = body->mode;
552 st.st_nlink = body->nlink;
553 st.st_uid = body->uid;
554 st.st_gid = body->gid;
555 st.st_rdev = body->rdev;
556 st.st_size = body->size;
557 st.st_blksize = PAGE_SIZE;
558 st.st_blocks = body->blocks;
559 st.st_atime = body->atime;
560 st.st_mtime = body->mtime;
561 st.st_ctime = body->ctime;
562 st.st_ino = body->ino;
564 lmdp = (struct lov_user_mds_data *)arg;
565 rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
567 GOTO(out_req, rc = -EFAULT);
568 lump = &lmdp->lmd_lmm;
570 lump = (struct lov_user_md *)arg;
573 rc = copy_to_user(lump, lmm, lmmsize);
575 GOTO(out_req, rc = -EFAULT);
579 ptlrpc_req_finished(request);
584 case OBD_IOC_LLOG_CATINFO: {
585 struct ptlrpc_request *req = NULL;
591 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
596 if (!data->ioc_inlbuf1) {
597 obd_ioctl_freedata(buf, len);
601 lens[0] = data->ioc_inllen1;
602 bufs[0] = data->ioc_inlbuf1;
603 if (data->ioc_inllen2) {
604 lens[1] = data->ioc_inllen2;
605 bufs[1] = data->ioc_inlbuf2;
610 size = data->ioc_plen1;
611 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import, LLOG_CATINFO,
614 GOTO(out_catinfo, rc = -ENOMEM);
615 req->rq_replen = lustre_msg_size(1, &size);
617 rc = ptlrpc_queue_wait(req);
618 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
620 rc = copy_to_user(data->ioc_pbuf1, str,
622 ptlrpc_req_finished(req);
624 obd_ioctl_freedata(buf, len);
628 return obd_iocontrol(cmd, sbi->ll_osc_exp,0,NULL,(void *)arg);
632 int ll_dir_open(struct inode *inode, struct file *file)
635 RETURN(ll_file_open(inode, file));
638 int ll_dir_release(struct inode *inode, struct file *file)
641 RETURN(ll_file_release(inode, file));
644 struct file_operations ll_dir_operations = {
646 .release = ll_dir_release,
647 .read = generic_read_dir,
648 .readdir = ll_readdir,
649 .ioctl = ll_dir_ioctl