1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/dir.c
38 * Lustre Light directory handling
41 #define DEBUG_SUBSYSTEM S_LLITE
48 #include <sys/types.h>
51 #include <sys/queue.h>
66 #ifdef HAVE_ASM_TYPES_H
67 #include <asm/types.h>
68 #elif defined(HAVE_SYS_TYPES_H)
69 #include <sys/types.h>
72 #ifdef HAVE_LINUX_UNISTD_H
73 #include <linux/unistd.h>
74 #elif defined(HAVE_UNISTD_H)
80 #include "llite_lib.h"
82 static int llu_dir_do_readpage(struct inode *inode, struct page *page)
84 struct llu_inode_info *lli = llu_i2info(inode);
85 struct intnl_stat *st = llu_i2stat(inode);
86 struct llu_sb_info *sbi = llu_i2sbi(inode);
87 struct ll_fid mdc_fid;
90 struct ptlrpc_request *request;
91 struct lustre_handle lockh;
92 struct mds_body *body;
93 struct lookup_intent it = { .it_op = IT_READDIR };
94 struct mdc_op_data data;
95 struct obd_device *obddev = class_exp2obd(sbi->ll_mdc_exp);
96 struct ldlm_res_id res_id =
97 { .name = {st->st_ino, (__u64)lli->lli_st_generation} };
98 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
101 rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
102 &res_id, LDLM_IBITS, &policy, LCK_CR, &lockh);
104 struct ldlm_enqueue_info einfo = {LDLM_IBITS, LCK_CR,
105 llu_mdc_blocking_ast, ldlm_completion_ast, NULL, inode};
107 llu_prepare_mdc_op_data(&data, inode, NULL, NULL, 0, 0);
109 rc = mdc_enqueue(sbi->ll_mdc_exp, &einfo, &it,
110 &data, &lockh, NULL, 0,
111 LDLM_FL_CANCEL_ON_BLOCK);
112 request = (struct ptlrpc_request *)it.d.lustre.it_data;
114 ptlrpc_req_finished(request);
116 CERROR("lock enqueue: err: %d\n", rc);
120 ldlm_lock_dump_handle(D_OTHER, &lockh);
122 ll_pack_fid(&mdc_fid, st->st_ino, lli->lli_st_generation, S_IFDIR);
124 offset = (__u64)page->index << CFS_PAGE_SHIFT;
125 rc = mdc_readpage(sbi->ll_mdc_exp, &mdc_fid,
126 offset, page, &request);
128 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
130 LASSERT(body != NULL); /* checked by mdc_readpage() */
131 /* swabbed by mdc_readpage() */
132 LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
134 st->st_size = body->size;
136 CERROR("read_dir_page(%ld) error %d\n", page->index, rc);
138 ptlrpc_req_finished(request);
141 ldlm_lock_decref(&lockh, LCK_CR);
145 static struct page *llu_dir_read_page(struct inode *ino, unsigned long pgidx)
151 OBD_PAGE_ALLOC(page, 0);
153 RETURN(ERR_PTR(-ENOMEM));
156 rc = llu_dir_do_readpage(ino, page);
177 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
178 [EXT2_FT_UNKNOWN] DT_UNKNOWN,
179 [EXT2_FT_REG_FILE] DT_REG,
180 [EXT2_FT_DIR] DT_DIR,
181 [EXT2_FT_CHRDEV] DT_CHR,
182 [EXT2_FT_BLKDEV] DT_BLK,
183 [EXT2_FT_FIFO] DT_FIFO,
184 [EXT2_FT_SOCK] DT_SOCK,
185 [EXT2_FT_SYMLINK] DT_LNK,
188 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
189 #define ROUND_UP64(x) (((x)+sizeof(__u64)-1) & ~(sizeof(__u64)-1))
191 static int filldir(char *buf, int buflen,
192 const char *name, int namelen, loff_t offset,
193 ino_t ino, unsigned int d_type, int *filled)
195 struct dirent64 *dirent = (struct dirent64 *) (buf + *filled);
196 int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namelen + 1);
199 if ((*filled + reclen) > buflen)
203 dirent->d_off = offset;
204 dirent->d_reclen = reclen;
206 dirent->d_type = (unsigned short) d_type;
208 memcpy(dirent->d_name, name, namelen);
209 dirent->d_name[namelen] = 0;
216 ssize_t llu_iop_filldirentries(struct inode *ino, _SYSIO_OFF_T *basep,
217 char *buf, size_t nbytes)
219 struct llu_inode_info *lli = llu_i2info(ino);
220 struct intnl_stat *st = llu_i2stat(ino);
221 loff_t pos = *basep, offset;
223 unsigned long pgidx, maxpages;
226 liblustre_wait_event(0);
228 if (st->st_size == 0) {
229 CWARN("dir size is 0?\n");
234 pos = lli->lli_dir_pos;
236 maxpages = (st->st_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
237 pgidx = pos >> CFS_PAGE_SHIFT;
238 offset = pos & ~CFS_PAGE_MASK;
240 for ( ; pgidx < maxpages ; pgidx++, offset = 0) {
242 struct ext2_dirent *de;
245 page = llu_dir_read_page(ino, pgidx);
249 /* size might have been updated by mdc_readpage */
250 maxpages = (st->st_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
254 limit = addr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
255 de = (struct ext2_dirent *) (addr + offset);
257 for ( ; (char*) de <= limit; de = ext2_next_entry(de)) {
260 unsigned char d_type = DT_UNKNOWN;
262 if (de->file_type < EXT2_FT_MAX)
263 d_type = ext2_filetype_table[de->file_type];
265 offset = (char*) de - addr;
266 over = filldir(buf, nbytes, de->name, de->name_len,
267 (((__u64)pgidx << CFS_PAGE_SHIFT) | offset)
268 + le16_to_cpu(de->rec_len),
269 le32_to_cpu(de->inode), d_type, &filled);
273 * if buffer overflow with no data
274 * returned yet, then report error
288 lli->lli_dir_pos = (__u64)pgidx << CFS_PAGE_SHIFT | offset;
289 *basep = lli->lli_dir_pos;
290 liblustre_wait_event(0);