Whamcloud - gitweb
b=20563
[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 (c) 2004, 2010, Oracle and/or its affiliates. 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 #ifdef HAVE_LINUX_UNISTD_H
54 #include <linux/unistd.h>
55 #elif defined(HAVE_UNISTD_H)
56 #include <unistd.h>
57 #endif
58
59
60 #include "llite_lib.h"
61 #include <dirent.h>
62
63 /* (new) readdir implementation overview can be found in lustre/llite/dir.c */
64
65 static int llu_dir_do_readpage(struct inode *inode, struct page *page)
66 {
67         struct llu_inode_info *lli = llu_i2info(inode);
68         struct intnl_stat     *st = llu_i2stat(inode);
69         struct llu_sb_info    *sbi = llu_i2sbi(inode);
70         struct ptlrpc_request *request;
71         struct lustre_handle   lockh;
72         struct mdt_body       *body;
73         struct lookup_intent   it = { .it_op = IT_READDIR };
74         struct md_op_data      op_data = {{ 0 }};
75         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_UPDATE } };
76         __u64 offset;
77         int rc = 0;
78         ENTRY;
79
80         rc = md_lock_match(sbi->ll_md_exp, LDLM_FL_BLOCK_GRANTED,
81                            &lli->lli_fid, LDLM_IBITS, &policy, LCK_CR, &lockh);
82         if (!rc) {
83                 struct ldlm_enqueue_info einfo = {LDLM_IBITS, LCK_CR,
84                         llu_md_blocking_ast, ldlm_completion_ast, NULL, NULL,
85                         inode};
86
87                 llu_prep_md_op_data(&op_data, inode, NULL, NULL, 0, 0,
88                                     LUSTRE_OPC_ANY);
89
90                 rc = md_enqueue(sbi->ll_md_exp, &einfo, &it,
91                                 &op_data, &lockh, NULL, 0, NULL,
92                                 LDLM_FL_CANCEL_ON_BLOCK);
93                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
94                 if (request)
95                         ptlrpc_req_finished(request);
96                 if (rc < 0) {
97                         CERROR("lock enqueue: err: %d\n", rc);
98                         RETURN(rc);
99                 }
100         }
101         ldlm_lock_dump_handle(D_OTHER, &lockh);
102
103         offset = (__u64)hash_x_index(page->index);
104         rc = md_readpage(sbi->ll_md_exp, &lli->lli_fid, NULL,
105                          offset, page, &request);
106         if (!rc) {
107                 body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
108                 LASSERT(body != NULL);         /* checked by md_readpage() */
109
110                 if (body->valid & OBD_MD_FLSIZE)
111                         st->st_size = body->size;
112         } else {
113                 CERROR("read_dir_page(%ld) error %d\n", page->index, rc);
114         }
115         ptlrpc_req_finished(request);
116         EXIT;
117
118         ldlm_lock_decref(&lockh, LCK_CR);
119         return rc;
120 }
121
122 static cfs_page_t *llu_dir_read_page(struct inode *ino, __u64 hash,
123                                      int exact, struct ll_dir_chain *chain)
124 {
125         cfs_page_t *page;
126         int rc;
127         ENTRY;
128
129         OBD_PAGE_ALLOC(page, 0);
130         if (!page)
131                 RETURN(ERR_PTR(-ENOMEM));
132         page->index = hash_x_index(hash);
133
134         rc = llu_dir_do_readpage(ino, page);
135         if (rc) {
136                 OBD_PAGE_FREE(page);
137                 RETURN(ERR_PTR(rc));
138         }
139
140         return page;
141 }
142
143 void *(*memmover)(void *, const void *, size_t) = memmove;
144
145 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
146 #define ROUND_UP64(x)   (((x)+sizeof(__u64)-1) & ~(sizeof(__u64)-1))
147 static int filldir(char *buf, int buflen,
148                    const char *name, int namelen, loff_t offset,
149                    ino_t ino, unsigned int d_type, int *filled)
150 {
151         cfs_dirent_t *dirent = (cfs_dirent_t *) (buf + *filled);
152         cfs_dirent_t  holder;
153         int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namelen + 1);
154
155         /*
156          * @buf is not guaranteed to be properly aligned. To work around,
157          * first fill stack-allocated @holder, then copy @holder into @buf by
158          * memmove().
159          */
160
161         /* check overflow */
162         if ((*filled + reclen) > buflen)
163                 return 1;
164
165         holder.d_ino = ino;
166 #ifdef _DIRENT_HAVE_D_OFF
167         holder.d_off = offset;
168 #endif
169         holder.d_reclen = reclen;
170 #ifdef _DIRENT_HAVE_D_TYPE
171         holder.d_type = (unsigned short) d_type;
172 #endif
173         /* gcc unrolls memcpy() of structs into field-wise assignments,
174          * assuming proper alignment. Humor it. */
175         (*memmover)(dirent, &holder, NAME_OFFSET(dirent));
176         memcpy(dirent->d_name, name, namelen);
177         dirent->d_name[namelen] = 0;
178
179         *filled += reclen;
180
181         return 0;
182 }
183
184 /*
185  * TODO: much of the code here is similar/identical to llite ll_readdir().
186  * These code can be factored out and shared in a common module.
187  */
188
189 ssize_t llu_iop_filldirentries(struct inode *dir, _SYSIO_OFF_T *basep,
190                                char *buf, size_t nbytes)
191 {
192         struct llu_inode_info *lli = llu_i2info(dir);
193         struct intnl_stat     *st = llu_i2stat(dir);
194         loff_t                 pos = *basep;
195         struct ll_dir_chain    chain;
196         cfs_page_t            *page;
197         int filled = 0;
198         int rc;
199         int done;
200         int shift;
201         __u16 type;
202         ENTRY;
203
204         liblustre_wait_event(0);
205
206         if (st->st_size == 0) {
207                 CWARN("dir size is 0?\n");
208                 RETURN(0);
209         }
210
211         if (pos == DIR_END_OFF)
212                 /*
213                  * end-of-file.
214                  */
215                 RETURN(0);
216
217         rc    = 0;
218         done  = 0;
219         shift = 0;
220         ll_dir_chain_init(&chain);
221
222         page = llu_dir_read_page(dir, pos, 0, &chain);
223         while (rc == 0 && !done) {
224                 struct lu_dirpage *dp;
225                 struct lu_dirent  *ent;
226
227                 if (!IS_ERR(page)) {
228                         /*
229                          * If page is empty (end of directoryis reached),
230                          * use this value.
231                          */
232                         __u64 hash = DIR_END_OFF;
233                         __u64 next;
234
235                         dp = page->addr;
236                         for (ent = lu_dirent_start(dp); ent != NULL && !done;
237                              ent = lu_dirent_next(ent)) {
238                                 char          *name;
239                                 int            namelen;
240                                 struct lu_fid  fid;
241                                 __u64          ino;
242
243                                 hash    = le64_to_cpu(ent->lde_hash);
244                                 namelen = le16_to_cpu(ent->lde_namelen);
245
246                                 if (hash < pos)
247                                         /*
248                                          * Skip until we find target hash
249                                          * value.
250                                          */
251                                         continue;
252
253                                 if (namelen == 0)
254                                         /*
255                                          * Skip dummy record.
256                                          */
257                                         continue;
258
259                                 fid  = ent->lde_fid;
260                                 name = ent->lde_name;
261                                 fid_le_to_cpu(&fid, &fid);
262                                 ino  = cl_fid_build_ino(&fid);
263                                 type = ll_dirent_type_get(ent);
264                                 done = filldir(buf, nbytes, name, namelen,
265                                                (loff_t)hash, ino, type,
266                                                &filled);
267                         }
268                         next = le64_to_cpu(dp->ldp_hash_end);
269                         OBD_PAGE_FREE(page);
270                         if (!done) {
271                                 pos = next;
272                                 if (pos == DIR_END_OFF)
273                                         /*
274                                          * End of directory reached.
275                                          */
276                                         done = 1;
277                                 else if (1 /* chain is exhausted*/)
278                                         /*
279                                          * Normal case: continue to the next
280                                          * page.
281                                          */
282                                         page = llu_dir_read_page(dir, pos, 1,
283                                                                &chain);
284                                 else {
285                                         /*
286                                          * go into overflow page.
287                                          */
288                                 }
289                         } else {
290                                 pos = hash;
291                                 if (filled == 0)
292                                         GOTO(out, filled = -EINVAL);
293                         }
294                 } else {
295                         rc = PTR_ERR(page);
296                         CERROR("error reading dir "DFID" at %lu: rc %d\n",
297                                PFID(&lli->lli_fid), (unsigned long)pos, rc);
298                 }
299         }
300         lli->lli_dir_pos = (loff_t)pos;
301         *basep = lli->lli_dir_pos;
302 out:
303         ll_dir_chain_fini(&chain);
304         liblustre_wait_event(0);
305         RETURN(filled);
306 }