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 <obd_support.h>
45 #include <obd_class.h>
46 #include <lustre_lib.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre_lite.h>
49 #include <lustre_dlm.h>
50 #include "llite_internal.h"
52 #define PageChecked(page) test_bit(PG_checked, &(page)->flags)
53 #define SetPageChecked(page) set_bit(PG_checked, &(page)->flags)
55 /* returns the page unlocked, but with a reference */
56 static int ll_dir_readpage(struct file *file, struct page *page)
58 struct inode *inode = page->mapping->host;
59 struct ptlrpc_request *request;
60 struct mdt_body *body;
65 offset = (__u64)page->index << PAGE_SHIFT;
66 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off "LPU64"\n",
67 inode->i_ino, inode->i_generation, inode, offset);
69 rc = md_readpage(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode),
70 offset, page, &request);
72 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
73 LASSERT (body != NULL); /* checked by md_readpage() */
74 LASSERT_REPSWABBED (request, 0); /* swabbed by md_readpage() */
76 if (body->valid & OBD_MD_FLSIZE)
77 inode->i_size = body->size;
78 SetPageUptodate(page);
80 ptlrpc_req_finished(request);
87 struct address_space_operations ll_dir_aops = {
88 .readpage = ll_dir_readpage,
91 static inline unsigned long dir_pages(struct inode *inode)
93 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
96 static inline unsigned ll_chunk_size(struct inode *inode)
98 return inode->i_sb->s_blocksize;
101 static void ll_check_page(struct inode *dir, struct page *page)
103 /* XXX: check page format later */
104 SetPageChecked(page);
107 static inline void ll_put_page(struct page *page)
110 page_cache_release(page);
113 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
115 ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
116 struct address_space *mapping = dir->i_mapping;
117 struct lustre_handle lockh;
121 rc = md_lock_match(ll_i2sbi(dir)->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
122 ll_inode2fid(dir), LDLM_IBITS, &policy, LCK_CR, &lockh);
124 struct lookup_intent it = { .it_op = IT_READDIR };
125 struct md_op_data op_data = { { 0 } };
126 struct ptlrpc_request *request;
128 ll_prepare_md_op_data(&op_data, dir, NULL, NULL, 0, 0);
130 rc = md_enqueue(ll_i2sbi(dir)->ll_md_exp, LDLM_IBITS, &it,
131 LCK_CR, &op_data, &lockh, NULL, 0,
132 ldlm_completion_ast, ll_md_blocking_ast, dir,
135 request = (struct ptlrpc_request *)it.d.lustre.it_data;
137 ptlrpc_req_finished(request);
139 CERROR("lock enqueue: rc: %d\n", rc);
143 ldlm_lock_dump_handle(D_OTHER, &lockh);
145 page = read_cache_page(mapping, n,
146 (filler_t*)mapping->a_ops->readpage, NULL);
148 GOTO(out_unlock, page);
152 if (!PageUptodate(page))
154 if (!PageChecked(page))
155 ll_check_page(dir, page);
160 ldlm_lock_decref(&lockh, LCK_CR);
165 page = ERR_PTR(-EIO);
169 static inline struct lu_dir_entry *ll_next_entry(struct lu_dir_entry *p)
171 return (struct lu_dir_entry *)((char *)p + le16_to_cpu(p->de_rec_len));
174 int ll_readdir(struct file *filp, void *dirent, filldir_t filldir)
176 struct inode *inode = filp->f_dentry->d_inode;
177 loff_t pos = filp->f_pos;
178 unsigned offset = pos & ~PAGE_CACHE_MASK;
179 unsigned long n = pos >> PAGE_CACHE_SHIFT;
180 unsigned long npages = dir_pages(inode);
184 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
185 inode->i_ino, inode->i_generation, inode, pos, inode->i_size);
187 if (pos > inode->i_size - LU_DIR_REC_LEN(1))
190 for ( ; n < npages; n++, offset = 0) {
192 struct lu_dir_entry *de;
195 CDEBUG(D_VFSTRACE,"read %lu of dir %lu/%u page %lu/%lu size %llu\n",
196 PAGE_CACHE_SIZE, inode->i_ino, inode->i_generation,
197 n, npages, inode->i_size);
198 page = ll_get_dir_page(inode, n);
200 /* size might have been updated by md_readpage */
201 npages = dir_pages(inode);
205 CERROR("error reading dir %lu/%u page %lu: rc %d\n",
206 inode->i_ino, inode->i_generation, n, rc);
210 kaddr = page_address(page);
212 de = (struct lu_dir_entry *)(kaddr + offset);
213 limit = kaddr + PAGE_CACHE_SIZE - LU_DIR_REC_LEN(1);
214 for ( ;(char*)de <= limit; de = ll_next_entry(de)) {
215 if (fid_oid(&de->de_fid) && fid_seq(&de->de_fid)) {
216 struct ll_sb_info *sbi = ll_i2sbi(inode);
219 rc = 0; /* no error if we return something */
221 offset = (char *)de - kaddr;
222 fid_le_to_cpu(&de->de_fid);
223 over = filldir(dirent, de->de_name, de->de_name_len,
224 (n << PAGE_CACHE_SHIFT) | offset,
225 ll_fid_build_ino(sbi, &de->de_fid),
237 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
238 filp->f_version = inode->i_version;
239 touch_atime(filp->f_vfsmnt, filp->f_dentry);
244 #define QCTL_COPY(out, in) \
246 Q_COPY(out, in, qc_cmd); \
247 Q_COPY(out, in, qc_type); \
248 Q_COPY(out, in, qc_id); \
249 Q_COPY(out, in, qc_stat); \
250 Q_COPY(out, in, qc_dqinfo); \
251 Q_COPY(out, in, qc_dqblk); \
254 static int ll_dir_ioctl(struct inode *inode, struct file *file,
255 unsigned int cmd, unsigned long arg)
257 struct ll_sb_info *sbi = ll_i2sbi(inode);
258 struct obd_ioctl_data *data;
261 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
262 inode->i_ino, inode->i_generation, inode, cmd);
264 /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
265 if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
268 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
270 case EXT3_IOC_GETFLAGS:
271 case EXT3_IOC_SETFLAGS:
272 RETURN(ll_iocontrol(inode, file, cmd, arg));
273 case EXT3_IOC_GETVERSION_OLD:
274 case EXT3_IOC_GETVERSION:
275 RETURN(put_user(inode->i_generation, (int *)arg));
276 /* We need to special case any other ioctls we want to handle,
277 * to send them to the MDS/OST as appropriate and to properly
278 * network encode the arg field.
279 case EXT3_IOC_SETVERSION_OLD:
280 case EXT3_IOC_SETVERSION:
282 case IOC_MDC_LOOKUP: {
283 struct ptlrpc_request *request = NULL;
284 int namelen, rc, len = 0;
288 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
293 filename = data->ioc_inlbuf1;
294 namelen = data->ioc_inllen1;
297 CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
298 GOTO(out, rc = -EINVAL);
301 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
302 filename, namelen, OBD_MD_FLID, 0,
305 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
309 ptlrpc_req_finished(request);
313 obd_ioctl_freedata(buf, len);
316 case LL_IOC_LOV_SETSTRIPE: {
317 struct ptlrpc_request *request = NULL;
318 struct md_op_data op_data = { { 0 } };
319 struct iattr attr = { 0 };
320 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
323 ll_prepare_md_op_data(&op_data, inode,
326 LASSERT(sizeof(lum) == sizeof(*lump));
327 LASSERT(sizeof(lum.lmm_objects[0]) ==
328 sizeof(lump->lmm_objects[0]));
329 rc = copy_from_user(&lum, lump, sizeof(lum));
334 * This is coming from userspace, so should be in
335 * local endian. But the MDS would like it in little
336 * endian, so we swab it before we send it.
338 if (lum.lmm_magic != LOV_USER_MAGIC)
341 if (lum.lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
342 lustre_swab_lov_user_md(&lum);
344 /* swabbing is done in lov_setstripe() on server side */
345 rc = md_setattr(sbi->ll_md_exp, &op_data,
346 &attr, &lum, sizeof(lum), NULL, 0, &request);
348 ptlrpc_req_finished(request);
349 if (rc != -EPERM && rc != -EACCES)
350 CERROR("md_setattr fails: rc = %d\n", rc);
353 ptlrpc_req_finished(request);
357 case LL_IOC_LOV_GETSTRIPE: {
358 struct ptlrpc_request *request = NULL;
359 struct lov_user_md *lump = (struct lov_user_md *)arg;
360 struct lov_mds_md *lmm;
361 struct mdt_body *body;
364 rc = ll_get_max_mdsize(sbi, &lmmsize);
368 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode),
369 OBD_MD_FLDIREA, lmmsize, &request);
371 CDEBUG(D_INFO, "md_getattr failed: rc = %d\n", rc);
375 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
376 LASSERT(body != NULL); /* checked by md_getattr_name */
377 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_name */
379 lmmsize = body->eadatasize;
381 GOTO(out_get, rc = -ENODATA);
383 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
384 LASSERT(lmm != NULL);
385 LASSERT_REPSWABBED(request, 1);
388 * This is coming from the MDS, so is probably in
389 * little endian. We convert it to host endian before
390 * passing it to userspace.
392 if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
393 lustre_swab_lov_user_md((struct lov_user_md *)lmm);
394 lustre_swab_lov_user_md_objects((struct lov_user_md *)lmm);
397 rc = copy_to_user(lump, lmm, lmmsize);
399 GOTO(out_get, rc = -EFAULT);
403 ptlrpc_req_finished(request);
406 case LL_IOC_OBD_STATFS:
407 RETURN(ll_obd_statfs(inode, (void *)arg));
408 case IOC_MDC_GETFILEINFO:
409 case IOC_MDC_GETSTRIPE: {
410 struct ptlrpc_request *request = NULL;
411 struct lov_user_md *lump;
412 struct lov_mds_md *lmm;
413 struct mdt_body *body;
417 filename = getname((const char *)arg);
418 if (IS_ERR(filename))
419 RETURN(PTR_ERR(filename));
421 rc = ll_get_max_mdsize(sbi, &lmmsize);
425 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(inode),
426 filename, strlen(filename) + 1,
427 OBD_MD_FLEASIZE, lmmsize, &request);
429 CDEBUG(D_INFO, "md_getattr_name failed on %s: rc %d\n",
434 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
435 LASSERT(body != NULL); /* checked by md_getattr_name */
436 LASSERT_REPSWABBED(request, 0);/* swabbed by md_getattr_name */
438 lmmsize = body->eadatasize;
440 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
441 GOTO(out_req, rc = -ENODATA);
444 GOTO(out_req, rc = -EFBIG);
446 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
447 LASSERT(lmm != NULL);
448 LASSERT_REPSWABBED(request, 1);
451 * This is coming from the MDS, so is probably in
452 * little endian. We convert it to host endian before
453 * passing it to userspace.
455 if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
456 lustre_swab_lov_user_md((struct lov_user_md *)lmm);
457 lustre_swab_lov_user_md_objects((struct lov_user_md *)lmm);
458 } else if (lmm->lmm_magic == __swab32(LOV_MAGIC_JOIN)) {
459 lustre_swab_lov_user_md_join((struct lov_user_md_join *)lmm);
461 if (lmm->lmm_magic == LOV_MAGIC_JOIN) {
462 struct lov_stripe_md *lsm;
463 struct lov_user_md_join *lmj;
464 int lmj_size, i, aindex = 0, rc;
466 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
468 GOTO(out_req, rc = -ENOMEM);
469 rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp, lsm);
471 GOTO(out_free_memmd, rc);
473 lmj_size = sizeof(struct lov_user_md_join) +
474 lsm->lsm_stripe_count *
475 sizeof(struct lov_user_ost_data_join);
476 OBD_ALLOC(lmj, lmj_size);
478 GOTO(out_free_memmd, rc = -ENOMEM);
480 memcpy(lmj, lmm, sizeof(struct lov_user_md_join));
481 for (i = 0; i < lsm->lsm_stripe_count; i++) {
482 struct lov_array_info *lai = lsm->lsm_array;
483 if ((lai->lai_ext_array[aindex].le_loi_idx +
484 lai->lai_ext_array[aindex].le_stripe_count)<=i){
487 CDEBUG(D_INFO, "aindex %d i %d l_extent_start"
488 LPU64"len %d \n", aindex, i,
489 lai->lai_ext_array[aindex].le_start,
490 (int)lai->lai_ext_array[aindex].le_len);
491 lmj->lmm_objects[i].l_extent_start =
492 lai->lai_ext_array[aindex].le_start;
494 if ((int)lai->lai_ext_array[aindex].le_len == -1) {
495 lmj->lmm_objects[i].l_extent_end = -1;
497 lmj->lmm_objects[i].l_extent_end =
498 lai->lai_ext_array[aindex].le_start +
499 lai->lai_ext_array[aindex].le_len;
501 lmj->lmm_objects[i].l_object_id =
502 lsm->lsm_oinfo[i].loi_id;
503 lmj->lmm_objects[i].l_object_gr =
504 lsm->lsm_oinfo[i].loi_gr;
505 lmj->lmm_objects[i].l_ost_gen =
506 lsm->lsm_oinfo[i].loi_ost_gen;
507 lmj->lmm_objects[i].l_ost_idx =
508 lsm->lsm_oinfo[i].loi_ost_idx;
510 lmm = (struct lov_mds_md *)lmj;
513 obd_free_memmd(sbi->ll_dt_exp, &lsm);
517 if (cmd == IOC_MDC_GETFILEINFO) {
518 struct lov_user_mds_data *lmdp;
522 st.st_mode = body->mode;
523 st.st_nlink = body->nlink;
524 st.st_uid = body->uid;
525 st.st_gid = body->gid;
526 st.st_rdev = body->rdev;
527 st.st_size = body->size;
528 st.st_blksize = PAGE_SIZE;
529 st.st_blocks = body->blocks;
530 st.st_atime = body->atime;
531 st.st_mtime = body->mtime;
532 st.st_ctime = body->ctime;
533 st.st_ino = inode->i_ino;
535 lmdp = (struct lov_user_mds_data *)arg;
536 rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
538 GOTO(out_req, rc = -EFAULT);
539 lump = &lmdp->lmd_lmm;
541 lump = (struct lov_user_md *)arg;
544 rc = copy_to_user(lump, lmm, lmmsize);
545 if (lmm->lmm_magic == LOV_MAGIC_JOIN)
546 OBD_FREE(lmm, lmmsize);
548 GOTO(out_req, rc = -EFAULT);
552 ptlrpc_req_finished(request);
557 case OBD_IOC_LLOG_CATINFO: {
558 struct ptlrpc_request *req = NULL;
564 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
569 if (!data->ioc_inlbuf1) {
570 obd_ioctl_freedata(buf, len);
574 lens[0] = data->ioc_inllen1;
575 bufs[0] = data->ioc_inlbuf1;
576 if (data->ioc_inllen2) {
577 lens[1] = data->ioc_inllen2;
578 bufs[1] = data->ioc_inlbuf2;
583 size = data->ioc_plen1;
584 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
585 LUSTRE_LOG_VERSION, LLOG_CATINFO,
588 GOTO(out_catinfo, rc = -ENOMEM);
589 req->rq_replen = lustre_msg_size(1, &size);
591 rc = ptlrpc_queue_wait(req);
592 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
594 rc = copy_to_user(data->ioc_pbuf1, str,
596 ptlrpc_req_finished(req);
598 obd_ioctl_freedata(buf, len);
601 case OBD_IOC_QUOTACHECK: {
602 struct obd_quotactl *oqctl;
605 if (!capable(CAP_SYS_ADMIN))
608 OBD_ALLOC_PTR(oqctl);
611 oqctl->qc_type = arg;
612 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
614 CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
618 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
620 CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
625 case OBD_IOC_POLL_QUOTACHECK: {
626 struct if_quotacheck *check;
629 if (!capable(CAP_SYS_ADMIN))
632 OBD_ALLOC_PTR(check);
636 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
639 CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
640 if (copy_to_user((void *)arg, check, sizeof(*check)))
645 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
648 CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
649 if (copy_to_user((void *)arg, check, sizeof(*check)))
657 #if HAVE_QUOTA_SUPPORT
658 case OBD_IOC_QUOTACTL: {
659 struct if_quotactl *qctl;
660 struct obd_quotactl *oqctl;
662 int cmd, type, id, rc = 0;
668 OBD_ALLOC_PTR(oqctl);
673 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
674 GOTO(out_quotactl, rc = -EFAULT);
677 type = qctl->qc_type;
684 if (!capable(CAP_SYS_ADMIN))
685 GOTO(out_quotactl, rc = -EPERM);
688 if (((type == USRQUOTA && current->euid != id) ||
689 (type == GRPQUOTA && !in_egroup_p(id))) &&
690 !capable(CAP_SYS_ADMIN))
691 GOTO(out_quotactl, rc = -EPERM);
693 /* XXX: dqb_valid is borrowed as a flag to mark that
694 * only mds quota is wanted */
695 if (qctl->qc_dqblk.dqb_valid)
696 qctl->obd_uuid = sbi->ll_md_exp->exp_obd->
697 u.cli.cl_target_uuid;
702 CERROR("unsupported quotactl op: %#x\n", cmd);
703 GOTO(out_quotactl, -ENOTTY);
706 QCTL_COPY(oqctl, qctl);
708 if (qctl->obd_uuid.uuid[0]) {
709 struct obd_device *obd;
710 struct obd_uuid *uuid = &qctl->obd_uuid;
712 obd = class_find_client_notype(uuid,
713 &sbi->ll_dt_exp->exp_obd->obd_uuid);
715 GOTO(out_quotactl, rc = -ENOENT);
717 if (cmd == Q_GETINFO)
718 oqctl->qc_cmd = Q_GETOINFO;
719 else if (cmd == Q_GETQUOTA)
720 oqctl->qc_cmd = Q_GETOQUOTA;
722 GOTO(out_quotactl, rc = -EINVAL);
724 if (sbi->ll_md_exp->exp_obd == obd) {
725 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
728 struct obd_export *exp;
729 struct lov_obd *lov = &sbi->ll_dt_exp->
732 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
733 exp = lov->tgts[i].ltd_exp;
735 if (!lov->tgts[i].active)
738 if (exp->exp_obd == obd) {
739 rc = obd_quotactl(exp, oqctl);
746 QCTL_COPY(qctl, oqctl);
748 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
751 GOTO(out_quotactl, rc);
754 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
755 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
756 oqctl->qc_cmd = Q_QUOTAOFF;
757 obd_quotactl(sbi->ll_md_exp, oqctl);
760 QCTL_COPY(qctl, oqctl);
762 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
769 #endif /* HAVE_QUOTA_SUPPORT */
770 case OBD_IOC_GETNAME: {
771 struct obd_device *obd = class_exp2obd(sbi->ll_dt_exp);
774 if (copy_to_user((void *)arg, obd->obd_name,
775 strlen(obd->obd_name) + 1))
780 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp,0,NULL,(void *)arg));
784 int ll_dir_open(struct inode *inode, struct file *file)
787 RETURN(ll_file_open(inode, file));
790 int ll_dir_release(struct inode *inode, struct file *file)
793 RETURN(ll_file_release(inode, file));
796 struct file_operations ll_dir_operations = {
798 .release = ll_dir_release,
799 .read = generic_read_dir,
800 .readdir = ll_readdir,
801 .ioctl = ll_dir_ioctl