Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[fs/lustre-release.git] / lustre / liblustre / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/liblustre/dir.c
37  *
38  * Lustre Light directory handling
39  */
40
41 #define DEBUG_SUBSYSTEM S_LLITE
42
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
47 #include <time.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include <sys/queue.h>
52
53 #include <sysio.h>
54 #ifdef HAVE_XTIO_H
55 #include <xtio.h>
56 #endif
57 #include <fs.h>
58 #include <mount.h>
59 #include <inode.h>
60 #ifdef HAVE_FILE_H
61 #include <file.h>
62 #endif
63
64 #undef LIST_HEAD
65
66 #ifdef HAVE_ASM_TYPES_H
67 #include <asm/types.h>
68 #elif defined(HAVE_SYS_TYPES_H)
69 #include <sys/types.h>
70 #endif
71
72 #ifdef HAVE_LINUX_UNISTD_H
73 #include <linux/unistd.h>
74 #elif defined(HAVE_UNISTD_H)
75 #include <unistd.h>
76 #endif
77
78 #include <dirent.h>
79
80 #include "llite_lib.h"
81
82 static int llu_dir_do_readpage(struct inode *inode, struct page *page)
83 {
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;
88         __u64 offset;
89         int rc = 0;
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 } };
99         ENTRY;
100
101         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
102                              &res_id, LDLM_IBITS, &policy, LCK_CR, &lockh);
103         if (!rc) {
104                 struct ldlm_enqueue_info einfo = {LDLM_IBITS, LCK_CR,
105                         llu_mdc_blocking_ast, ldlm_completion_ast, NULL, inode};
106
107                 llu_prepare_mdc_op_data(&data, inode, NULL, NULL, 0, 0);
108
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;
113                 if (request)
114                         ptlrpc_req_finished(request);
115                 if (rc < 0) {
116                         CERROR("lock enqueue: err: %d\n", rc);
117                         RETURN(rc);
118                 }
119         }
120         ldlm_lock_dump_handle(D_OTHER, &lockh);
121
122         ll_pack_fid(&mdc_fid, st->st_ino, lli->lli_st_generation, S_IFDIR);
123
124         offset = (__u64)page->index << CFS_PAGE_SHIFT;
125         rc = mdc_readpage(sbi->ll_mdc_exp, &mdc_fid,
126                           offset, page, &request);
127         if (!rc) {
128                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
129                                       sizeof(*body));
130                 LASSERT(body != NULL);         /* checked by mdc_readpage() */
131                 /* swabbed by mdc_readpage() */
132                 LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
133
134                 st->st_size = body->size;
135         } else {
136                 CERROR("read_dir_page(%ld) error %d\n", page->index, rc);
137         }
138         ptlrpc_req_finished(request);
139         EXIT;
140
141         ldlm_lock_decref(&lockh, LCK_CR);
142         return rc;
143 }
144
145 static struct page *llu_dir_read_page(struct inode *ino, unsigned long pgidx)
146 {
147         struct page *page;
148         int rc;
149         ENTRY;
150
151         OBD_PAGE_ALLOC(page, 0);
152         if (!page)
153                 RETURN(ERR_PTR(-ENOMEM));
154         page->index = pgidx;
155
156         rc = llu_dir_do_readpage(ino, page);
157         if (rc) {
158                 OBD_PAGE_FREE(page);
159                 RETURN(ERR_PTR(rc));
160         }
161
162         return page;
163 }
164
165 enum {
166         EXT2_FT_UNKNOWN,
167         EXT2_FT_REG_FILE,
168         EXT2_FT_DIR,
169         EXT2_FT_CHRDEV,
170         EXT2_FT_BLKDEV,
171         EXT2_FT_FIFO,
172         EXT2_FT_SOCK,
173         EXT2_FT_SYMLINK,
174         EXT2_FT_MAX
175 };
176
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,
186 };
187
188 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
189 #define ROUND_UP64(x)   (((x)+sizeof(__u64)-1) & ~(sizeof(__u64)-1))
190
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)
194 {
195         struct dirent64 *dirent = (struct dirent64 *) (buf + *filled);
196         int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namelen + 1);
197
198         /* check overflow */
199         if ((*filled + reclen) > buflen)
200                 return 1;
201
202         dirent->d_ino = ino;
203         dirent->d_off = offset;
204         dirent->d_reclen = reclen;
205 #ifndef _AIX
206         dirent->d_type = (unsigned short) d_type;
207 #endif
208         memcpy(dirent->d_name, name, namelen);
209         dirent->d_name[namelen] = 0;
210
211         *filled += reclen;
212
213         return 0;
214 }
215
216 ssize_t llu_iop_filldirentries(struct inode *ino, _SYSIO_OFF_T *basep, 
217                                char *buf, size_t nbytes)
218 {
219         struct llu_inode_info *lli = llu_i2info(ino);
220         struct intnl_stat *st = llu_i2stat(ino);
221         loff_t pos = *basep, offset;
222         int filled = 0;
223         unsigned long pgidx, maxpages;
224         ENTRY;
225
226         liblustre_wait_event(0);
227
228         if (st->st_size == 0) {
229                 CWARN("dir size is 0?\n");
230                 RETURN(0);
231         }
232
233         if (pos == -1)
234                 pos = lli->lli_dir_pos;
235
236         maxpages = (st->st_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
237         pgidx = pos >> CFS_PAGE_SHIFT;
238         offset = pos & ~CFS_PAGE_MASK;
239
240         for ( ; pgidx < maxpages ; pgidx++, offset = 0) {
241                 struct page *page;
242                 struct ext2_dirent *de;
243                 char *addr, *limit;
244
245                 page = llu_dir_read_page(ino, pgidx);
246                 if (IS_ERR(page))
247                         continue;
248
249                 /* size might have been updated by mdc_readpage */
250                 maxpages = (st->st_size + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
251
252                 /* fill in buffer */
253                 addr = page->addr;
254                 limit = addr + CFS_PAGE_SIZE - EXT2_DIR_REC_LEN(1);
255                 de = (struct ext2_dirent *) (addr + offset);
256
257                 for ( ; (char*) de <= limit; de = ext2_next_entry(de)) {
258                         if (de->inode) {
259                                 int over;
260                                 unsigned char d_type = DT_UNKNOWN;
261
262                                 if (de->file_type < EXT2_FT_MAX)
263                                         d_type = ext2_filetype_table[de->file_type];
264
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);
270                                 if (over) {
271                                         OBD_PAGE_FREE(page);
272                                         /*
273                                          * if buffer overflow with no data
274                                          * returned yet, then report error
275                                          * instead of eof
276                                          */
277                                         if (filled == 0)
278                                                 RETURN(-EINVAL);
279
280                                         GOTO(done, 0);
281                                 }
282                         }
283                 }
284                 
285                 OBD_PAGE_FREE(page);
286         }
287 done:
288         lli->lli_dir_pos = (__u64)pgidx << CFS_PAGE_SHIFT | offset;
289         *basep = lli->lli_dir_pos;
290         liblustre_wait_event(0);
291         RETURN(filled);
292 }