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);
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;
217 rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
218 &res_id, LDLM_PLAIN, NULL, LCK_PR, &lockh);
220 struct lookup_intent it = { .it_op = IT_READDIR };
221 struct ptlrpc_request *request;
222 struct mdc_op_data data;
224 ll_prepare_mdc_op_data(&data, dir, NULL, NULL, 0, 0);
226 rc = mdc_enqueue(ll_i2sbi(dir)->ll_mdc_exp, LDLM_PLAIN, &it,
227 LCK_PR, &data, &lockh, NULL, 0,
228 ldlm_completion_ast, ll_mdc_blocking_ast, dir);
230 request = (struct ptlrpc_request *)it.d.lustre.it_data;
232 ptlrpc_req_finished(request);
234 CERROR("lock enqueue: rc: %d\n", rc);
238 ldlm_lock_dump_handle(D_OTHER, &lockh);
240 page = read_cache_page(mapping, n,
241 (filler_t*)mapping->a_ops->readpage, NULL);
245 if (!PageUptodate(page))
247 if (!PageChecked(page))
248 ext2_check_page(page);
254 ldlm_lock_decref(&lockh, LCK_PR);
259 page = ERR_PTR(-EIO);
264 * p is at least 6 bytes before the end of page
266 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
268 return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
271 static inline unsigned
272 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
274 ext2_dirent *de = (ext2_dirent*)(base + offset);
275 ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
276 while ((char*)p < (char*)de)
277 p = ext2_next_entry(p);
278 return (char *)p - base;
281 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
282 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
283 [EXT2_FT_REG_FILE] DT_REG,
284 [EXT2_FT_DIR] DT_DIR,
285 [EXT2_FT_CHRDEV] DT_CHR,
286 [EXT2_FT_BLKDEV] DT_BLK,
287 [EXT2_FT_FIFO] DT_FIFO,
288 [EXT2_FT_SOCK] DT_SOCK,
289 [EXT2_FT_SYMLINK] DT_LNK,
293 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
295 struct inode *inode = filp->f_dentry->d_inode;
296 loff_t pos = filp->f_pos;
297 // XXX struct super_block *sb = inode->i_sb;
298 unsigned offset = pos & ~PAGE_CACHE_MASK;
299 unsigned long n = pos >> PAGE_CACHE_SHIFT;
300 unsigned long npages = dir_pages(inode);
301 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
302 unsigned char *types = NULL;
303 int need_revalidate = (filp->f_version != inode->i_version);
307 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
308 inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
310 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
313 types = ext2_filetype_table;
315 for ( ; n < npages; n++, offset = 0) {
320 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
321 PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
322 n, npages, inode->i_size);
323 page = ll_get_dir_page(inode, n);
325 /* size might have been updated by mdc_readpage */
326 npages = dir_pages(inode);
330 CERROR("error reading dir %lu/%u page %lu: rc %d\n",
331 inode->i_ino, inode->i_generation, n, rc);
335 kaddr = page_address(page);
336 if (need_revalidate) {
337 offset = ext2_validate_entry(kaddr, offset, chunk_mask);
340 de = (ext2_dirent *)(kaddr+offset);
341 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
342 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
345 unsigned char d_type = DT_UNKNOWN;
347 rc = 0; /* no error if we return something */
348 if (types && de->file_type < EXT2_FT_MAX)
349 d_type = types[de->file_type];
351 offset = (char *)de - kaddr;
352 over = filldir(dirent, de->name, de->name_len,
353 (n<<PAGE_CACHE_SHIFT) | offset,
354 le32_to_cpu(de->inode), d_type);
365 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
366 filp->f_version = inode->i_version;
371 static int ll_dir_ioctl(struct inode *inode, struct file *file,
372 unsigned int cmd, unsigned long arg)
374 struct ll_sb_info *sbi = ll_i2sbi(inode);
375 struct obd_ioctl_data *data;
378 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
379 inode->i_ino, inode->i_generation, inode, cmd);
381 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
384 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
386 case EXT3_IOC_GETFLAGS:
387 case EXT3_IOC_SETFLAGS:
388 RETURN( ll_iocontrol(inode, file, cmd, arg) );
389 case IOC_MDC_LOOKUP: {
390 struct ptlrpc_request *request = NULL;
394 int namelen, rc, len = 0;
397 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
402 filename = data->ioc_inlbuf1;
403 namelen = data->ioc_inllen1;
406 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
407 GOTO(out, rc = -EINVAL);
411 ll_inode2fid(&fid, inode);
412 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid,
413 filename, namelen, valid, 0, &request);
415 CDEBUG(D_INFO, "mdc_getattr_name: %d\n", rc);
419 ptlrpc_req_finished(request);
423 obd_ioctl_freedata(buf, len);
426 case LL_IOC_LOV_SETSTRIPE:
427 case LL_IOC_LOV_GETSTRIPE:
429 case IOC_MDC_GETSTRIPE: {
430 struct ptlrpc_request *request = NULL;
432 struct mds_body *body;
433 struct lov_user_md *lump = (struct lov_user_md *)arg;
434 struct lov_mds_md *lmm;
438 filename = getname((const char *)arg);
439 if (IS_ERR(filename))
440 RETURN(PTR_ERR(filename));
442 ll_inode2fid(&fid, inode);
443 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, filename,
444 strlen(filename)+1, OBD_MD_FLEASIZE,
445 obd_size_diskmd(sbi->ll_osc_exp, NULL),
448 CDEBUG(D_INFO, "mdc_getattr_name failed on %s: rc %d\n",
453 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
454 LASSERT(body != NULL); /* checked by mdc_getattr_name */
455 LASSERT_REPSWABBED(request, 0);/* swabbed by mdc_getattr_name */
457 lmmsize = body->eadatasize;
459 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
460 GOTO(out_req, rc = -ENODATA);
463 GOTO(out_req, rc = -EFBIG);
465 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
466 LASSERT(lmm != NULL);
467 LASSERT_REPSWABBED(request, 1);
469 rc = copy_to_user(lump, lmm, lmmsize);
471 GOTO(out_req, rc = -EFAULT);
475 ptlrpc_req_finished(request);
481 struct ptlrpc_request *req = NULL;
484 struct client_obd *cli;
485 struct obd_device *obd;
487 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
492 obd = class_name2obd(data->ioc_inlbuf1);
495 GOTO(out_ping, rc = -ENODEV);
497 if (!obd->obd_attached) {
498 CERROR("Device %d not attached\n", obd->obd_minor);
499 GOTO(out_ping, rc = -ENODEV);
501 if (!obd->obd_set_up) {
502 CERROR("Device %d still not setup\n", obd->obd_minor);
503 GOTO(out_ping, rc = -ENODEV);
506 req = ptlrpc_prep_req(cli->cl_import, OBD_PING, 0, NULL, NULL);
508 GOTO(out_ping, rc = -ENOMEM);
510 req->rq_replen = lustre_msg_size(0, NULL);
511 req->rq_send_state = LUSTRE_IMP_FULL;
513 rc = ptlrpc_queue_wait(req);
515 ptlrpc_req_finished(req);
517 obd_ioctl_freedata(buf, len);
520 case OBD_IOC_LLOG_CATINFO: {
521 struct ptlrpc_request *req = NULL;
527 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
532 if (!data->ioc_inlbuf1) {
533 obd_ioctl_freedata(buf, len);
537 lens[0] = data->ioc_inllen1;
538 bufs[0] = data->ioc_inlbuf1;
539 if (data->ioc_inllen2) {
540 lens[1] = data->ioc_inllen2;
541 bufs[1] = data->ioc_inlbuf2;
546 size = data->ioc_plen1;
547 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import, LLOG_CATINFO,
550 GOTO(out_catinfo, rc = -ENOMEM);
551 req->rq_replen = lustre_msg_size(1, &size);
553 rc = ptlrpc_queue_wait(req);
554 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
556 rc = copy_to_user(data->ioc_pbuf1, str,
558 ptlrpc_req_finished(req);
560 obd_ioctl_freedata(buf, len);
564 return obd_iocontrol(cmd, sbi->ll_osc_exp,0,NULL,(void *)arg);
568 int ll_dir_open(struct inode *inode, struct file *file)
570 return ll_file_open(inode, file);
573 int ll_dir_release(struct inode *inode, struct file *file)
575 return ll_file_release(inode, file);
578 struct file_operations ll_dir_operations = {
580 release: ll_dir_release,
581 read: generic_read_dir,