Whamcloud - gitweb
b: 1991
[fs/lustre-release.git] / lustre / llite / dir.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
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)
8  *
9  *  from
10  *
11  *  linux/fs/minix/dir.c
12  *  linux/fs/ext2/dir.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  ext2 directory handling functions
17  *
18  *  Big-endian to little-endian byte-swapping/bitmaps by
19  *        David S. Miller (davem@caip.rutgers.edu), 1995
20  *
21  *  All code that works with directory layout had been switched to pagecache
22  *  and moved here. AV
23  *
24  *  Adapted for Lustre Light
25  *  Copyright (C) 2002-2003, Cluster File Systems, Inc.
26  *
27  */
28
29 #include <linux/fs.h>
30 #include <linux/ext2_fs.h>
31 #include <linux/pagemap.h>
32 #include <linux/mm.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
38 #else
39 # include <linux/buffer_head.h>   // for wait_on_buffer
40 #endif
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
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"
52
53 typedef struct ext2_dir_entry_2 ext2_dirent;
54
55 #define PageChecked(page)        test_bit(PG_checked, &(page)->flags)
56 #define SetPageChecked(page)     set_bit(PG_checked, &(page)->flags)
57
58 /* returns the page unlocked, but with a reference */
59 static int ll_dir_readpage(struct file *file, struct page *page)
60 {
61         struct inode *inode = page->mapping->host;
62         struct ll_sb_info *sbi = ll_i2sbi(inode);
63         struct ll_fid mdc_fid;
64         __u64 offset;
65         int rc = 0;
66         struct ptlrpc_request *request;
67         struct lustre_handle lockh;
68         struct mds_body *body;
69         struct lookup_intent it = { .it_op = IT_READDIR };
70         struct mdc_op_data data;
71         struct obd_device *obddev = class_exp2obd(sbi->ll_mdc_exp);
72         struct ldlm_res_id res_id =
73                 { .name = {inode->i_ino, (__u64)inode->i_generation} };
74         ENTRY;
75
76         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
77                inode->i_generation, inode);
78         if ((inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_SHIFT <= page->index){
79                 /* XXX why do we need this exactly, and why do we think that
80                  *     an all-zero directory page is useful?
81                  */
82                 CERROR("memsetting dir page %lu to zero (size %lld)\n",
83                        page->index, inode->i_size);
84                 memset(kmap(page), 0, PAGE_CACHE_SIZE);
85                 kunmap(page);
86                 GOTO(readpage_out, rc);
87         }
88
89         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
90                              &res_id, LDLM_PLAIN, NULL, 0, LCK_PR, &lockh);
91         if (!rc) {
92                 ll_prepare_mdc_op_data(&data, inode, NULL, NULL, 0, 0);
93
94                 rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_PLAIN, &it, LCK_PR,
95                                  &data, &lockh, NULL, 0,
96                                  ldlm_completion_ast, ll_mdc_blocking_ast,
97                                  inode);
98                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
99                 if (request)
100                         ptlrpc_req_finished(request);
101                 if (rc < 0) {
102                         CERROR("lock enqueue: err: %d\n", rc);
103                         unlock_page(page);
104                         RETURN(rc);
105                 }
106         }
107         ldlm_lock_dump_handle(D_OTHER, &lockh);
108
109         if (PageUptodate(page)) {
110                 CERROR("Explain this please?\n");
111                 GOTO(readpage_out, rc);
112         }
113
114         mdc_pack_fid(&mdc_fid, inode->i_ino, inode->i_generation, S_IFDIR);
115
116         offset = page->index << PAGE_SHIFT;
117         rc = mdc_readpage(sbi->ll_mdc_exp, &mdc_fid,
118                           offset, page, &request);
119         if (!rc) {
120                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
121                 LASSERT (body != NULL);         /* checked by mdc_readpage() */
122                 LASSERT_REPSWABBED (request, 0); /* swabbed by mdc_readpage() */
123
124                 inode->i_size = body->size;
125         }
126         ptlrpc_req_finished(request);
127         EXIT;
128
129  readpage_out:
130         if (!rc)
131                 SetPageUptodate(page);
132
133         unlock_page(page);
134         ldlm_lock_decref(&lockh, LCK_PR);
135         return rc;
136 }
137
138 struct address_space_operations ll_dir_aops = {
139         readpage: ll_dir_readpage,
140 };
141
142 /*
143  * ext2 uses block-sized chunks. Arguably, sector-sized ones would be
144  * more robust, but we have what we have
145  */
146 static inline unsigned ext2_chunk_size(struct inode *inode)
147 {
148         return inode->i_sb->s_blocksize;
149 }
150
151 static inline void ext2_put_page(struct page *page)
152 {
153         kunmap(page);
154         page_cache_release(page);
155 }
156
157 static inline unsigned long dir_pages(struct inode *inode)
158 {
159         return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
160 }
161
162
163 static void ext2_check_page(struct page *page)
164 {
165         struct inode *dir = page->mapping->host;
166         unsigned chunk_size = ext2_chunk_size(dir);
167         char *kaddr = page_address(page);
168         //      u32 max_inumber = le32_to_cpu(sb->u.ext2_sb.s_es->s_inodes_count);
169         unsigned offs, rec_len;
170         unsigned limit = PAGE_CACHE_SIZE;
171         ext2_dirent *p;
172         char *error;
173
174         if ((dir->i_size >> PAGE_CACHE_SHIFT) == page->index) {
175                 limit = dir->i_size & ~PAGE_CACHE_MASK;
176                 if (limit & (chunk_size - 1)) {
177                         CERROR("limit %d dir size %lld index %ld\n",
178                                limit, dir->i_size, page->index);
179                         goto Ebadsize;
180                 }
181                 for (offs = limit; offs<PAGE_CACHE_SIZE; offs += chunk_size) {
182                         ext2_dirent *p = (ext2_dirent*)(kaddr + offs);
183                         p->rec_len = cpu_to_le16(chunk_size);
184                         p->name_len = 0;
185                         p->inode = 0;
186                 }
187                 if (!limit)
188                         goto out;
189         }
190         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
191                 p = (ext2_dirent *)(kaddr + offs);
192                 rec_len = le16_to_cpu(p->rec_len);
193
194                 if (rec_len < EXT2_DIR_REC_LEN(1))
195                         goto Eshort;
196                 if (rec_len & 3)
197                         goto Ealign;
198                 if (rec_len < EXT2_DIR_REC_LEN(p->name_len))
199                         goto Enamelen;
200                 if (((offs + rec_len - 1) ^ offs) & ~(chunk_size-1))
201                         goto Espan;
202                 //              if (le32_to_cpu(p->inode) > max_inumber)
203                 //goto Einumber;
204         }
205         if (offs != limit)
206                 goto Eend;
207 out:
208         SetPageChecked(page);
209         return;
210
211         /* Too bad, we had an error */
212
213 Ebadsize:
214         CERROR("ext2_check_page"
215                 "size of directory #%lu is not a multiple of chunk size\n",
216                 dir->i_ino
217         );
218         goto fail;
219 Eshort:
220         error = "rec_len is smaller than minimal";
221         goto bad_entry;
222 Ealign:
223         error = "unaligned directory entry";
224         goto bad_entry;
225 Enamelen:
226         error = "rec_len is too small for name_len";
227         goto bad_entry;
228 Espan:
229         error = "directory entry across blocks";
230         goto bad_entry;
231         //Einumber:
232         // error = "inode out of bounds";
233 bad_entry:
234         CERROR("ext2_check_page: bad entry in directory #%lu: %s - "
235                 "offset=%lu+%u, inode=%lu, rec_len=%d, name_len=%d",
236                 dir->i_ino, error, (page->index<<PAGE_CACHE_SHIFT), offs,
237                 (unsigned long) le32_to_cpu(p->inode),
238                 rec_len, p->name_len);
239         goto fail;
240 Eend:
241         p = (ext2_dirent *)(kaddr + offs);
242         CERROR("ext2_check_page"
243                 "entry in directory #%lu spans the page boundary"
244                 "offset=%lu, inode=%lu",
245                 dir->i_ino, (page->index<<PAGE_CACHE_SHIFT)+offs,
246                 (unsigned long) le32_to_cpu(p->inode));
247 fail:
248         SetPageChecked(page);
249         SetPageError(page);
250         LBUG();
251 }
252
253 static struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
254 {
255         struct address_space *mapping = dir->i_mapping;
256         struct page *page = read_cache_page(mapping, n,
257                                 (filler_t*)mapping->a_ops->readpage, NULL);
258         if (!IS_ERR(page)) {
259                 wait_on_page(page);
260                 kmap(page);
261                 if (!PageUptodate(page))
262                         goto fail;
263                 if (!PageChecked(page))
264                         ext2_check_page(page);
265                 if (PageError(page))
266                         goto fail;
267         }
268         return page;
269
270 fail:
271         ext2_put_page(page);
272         return ERR_PTR(-EIO);
273 }
274
275
276 /*
277  * p is at least 6 bytes before the end of page
278  */
279 static inline ext2_dirent *ext2_next_entry(ext2_dirent *p)
280 {
281         return (ext2_dirent *)((char*)p + le16_to_cpu(p->rec_len));
282 }
283
284 static inline unsigned
285 ext2_validate_entry(char *base, unsigned offset, unsigned mask)
286 {
287         ext2_dirent *de = (ext2_dirent*)(base + offset);
288         ext2_dirent *p = (ext2_dirent*)(base + (offset&mask));
289         while ((char*)p < (char*)de)
290                 p = ext2_next_entry(p);
291         return (char *)p - base;
292 }
293
294 static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
295         [EXT2_FT_UNKNOWN]       DT_UNKNOWN,
296         [EXT2_FT_REG_FILE]      DT_REG,
297         [EXT2_FT_DIR]           DT_DIR,
298         [EXT2_FT_CHRDEV]        DT_CHR,
299         [EXT2_FT_BLKDEV]        DT_BLK,
300         [EXT2_FT_FIFO]          DT_FIFO,
301         [EXT2_FT_SOCK]          DT_SOCK,
302         [EXT2_FT_SYMLINK]       DT_LNK,
303 };
304
305
306 int ll_readdir(struct file * filp, void * dirent, filldir_t filldir)
307 {
308         loff_t pos = filp->f_pos;
309         struct inode *inode = filp->f_dentry->d_inode;
310         // XXX struct super_block *sb = inode->i_sb;
311         unsigned offset = pos & ~PAGE_CACHE_MASK;
312         unsigned long n = pos >> PAGE_CACHE_SHIFT;
313         unsigned long npages = dir_pages(inode);
314         unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
315         unsigned char *types = NULL;
316         int need_revalidate = (filp->f_version != inode->i_version);
317         ENTRY;
318
319         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
320                inode->i_generation, inode);
321         if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
322                 GOTO(done, 0);
323
324         types = ext2_filetype_table;
325
326         for ( ; n < npages; n++, offset = 0) {
327                 char *kaddr, *limit;
328                 ext2_dirent *de;
329                 struct page *page;
330
331                 CDEBUG(D_EXT2, "reading %lu of dir %lu page %lu, size %llu\n",
332                        PAGE_CACHE_SIZE, inode->i_ino, n, inode->i_size);
333                 page = ll_get_dir_page(inode, n);
334
335                 /* size might have been updated by mdc_readpage */
336                 npages = dir_pages(inode);
337
338                 if (IS_ERR(page))
339                         continue;
340                 kaddr = page_address(page);
341                 if (need_revalidate) {
342                         offset = ext2_validate_entry(kaddr, offset, chunk_mask);
343                         need_revalidate = 0;
344                 }
345                 de = (ext2_dirent *)(kaddr+offset);
346                 limit = kaddr + PAGE_CACHE_SIZE - EXT2_DIR_REC_LEN(1);
347                 for ( ;(char*)de <= limit; de = ext2_next_entry(de)) {
348                         if (de->inode) {
349                                 int over;
350                                 unsigned char d_type = DT_UNKNOWN;
351
352                                 if (types && de->file_type < EXT2_FT_MAX)
353                                         d_type = types[de->file_type];
354
355                                 offset = (char *)de - kaddr;
356                                 over = filldir(dirent, de->name, de->name_len,
357                                                (n<<PAGE_CACHE_SHIFT) | offset,
358                                                le32_to_cpu(de->inode), d_type);
359                                 if (over) {
360                                         ext2_put_page(page);
361                                         GOTO(done,0);
362                                 }
363                         }
364                 }
365                 ext2_put_page(page);
366         }
367
368 done:
369         filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
370         filp->f_version = inode->i_version;
371         update_atime(inode);
372         RETURN(0);
373 }
374
375 static int ll_dir_ioctl(struct inode *inode, struct file *file,
376                         unsigned int cmd, unsigned long arg)
377 {
378         struct ll_sb_info *sbi = ll_i2sbi(inode);
379         struct obd_ioctl_data *data;
380         ENTRY;
381
382         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
383                inode->i_ino, inode->i_generation, inode, cmd);
384
385         if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
386                 return -ENOTTY;
387
388         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
389         switch(cmd) {
390         case EXT3_IOC_GETFLAGS:
391         case EXT3_IOC_SETFLAGS:
392                 RETURN( ll_iocontrol(inode, file, cmd, arg) );
393         case IOC_MDC_LOOKUP: {
394                 struct ptlrpc_request *request = NULL;
395                 struct ll_fid fid;
396                 char *buf = NULL;
397                 char *filename;
398                 int namelen, rc, len = 0;
399                 unsigned long valid;
400
401                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
402                 if (rc)
403                         RETURN(rc);
404                 data = (void *)buf;
405
406                 filename = data->ioc_inlbuf1;
407                 namelen = data->ioc_inllen1;
408
409                 if (namelen < 1) {
410                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
411                         GOTO(out, rc = -EINVAL);
412                 }
413
414                 valid = OBD_MD_FLID;
415                 ll_inode2fid(&fid, inode);
416                 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid,
417                                       filename, namelen, valid, 0, &request);
418                 if (rc < 0) {
419                         CDEBUG(D_INFO, "mdc_getattr_name: %d\n", rc);
420                         GOTO(out, rc);
421                 }
422
423                 ptlrpc_req_finished(request);
424
425                 EXIT;
426         out:
427                 obd_ioctl_freedata(buf, len);
428                 return rc;
429         }
430         case LL_IOC_LOV_SETSTRIPE:
431         case LL_IOC_LOV_GETSTRIPE:
432                 RETURN(-ENOTTY);
433         case IOC_MDC_GETSTRIPE: {
434                 struct ptlrpc_request *request = NULL;
435                 struct ll_fid fid;
436                 struct mds_body *body;
437                 struct lov_user_md *lump = (struct lov_user_md *)arg;
438                 struct lov_mds_md *lmm;
439                 char *filename;
440                 int rc, lmmsize;
441
442                 filename = getname((const char *)arg);
443                 if (IS_ERR(filename))
444                         RETURN(PTR_ERR(filename));
445
446                 ll_inode2fid(&fid, inode);
447                 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, filename,
448                                       strlen(filename)+1, OBD_MD_FLEASIZE,
449                                       obd_size_diskmd(sbi->ll_osc_exp, NULL),
450                                       &request);
451                 if (rc < 0) {
452                         CDEBUG(D_INFO, "mdc_getattr_name failed on %s: rc %d\n",
453                                filename, rc);
454                         GOTO(out_name, rc);
455                 }
456
457                 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof (*body));
458                 LASSERT(body != NULL);         /* checked by mdc_getattr_name */
459                 LASSERT_REPSWABBED(request, 0);/* swabbed by mdc_getattr_name */
460
461                 lmmsize = body->eadatasize;
462
463                 if (!(body->valid & OBD_MD_FLEASIZE) || lmmsize == 0)
464                         GOTO(out_req, rc = -ENODATA);
465
466                 if (lmmsize > 4096)
467                         GOTO(out_req, rc = -EFBIG);
468
469                 lmm = lustre_msg_buf(request->rq_repmsg, 1, lmmsize);
470                 LASSERT(lmm != NULL);
471                 LASSERT_REPSWABBED(request, 1);
472
473                 rc = copy_to_user(lump, lmm, lmmsize);
474                 if (rc)
475                         GOTO(out_req, rc = -EFAULT);
476
477                 EXIT;
478         out_req:
479                 ptlrpc_req_finished(request);
480         out_name:
481                 putname(filename);
482                 return rc;
483         }
484         case OBD_IOC_PING: {
485                 struct ptlrpc_request *req = NULL;
486                 char *buf = NULL;
487                 int rc, len=0;
488                 struct client_obd *cli;
489                 struct obd_device *obd;
490                                                                                                                              
491                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
492                 if (rc)
493                         RETURN(rc);
494                 data = (void *)buf;
495
496                 obd = class_name2obd(data->ioc_inlbuf1);
497                                                                                                                              
498                 if (!obd )
499                         GOTO(out_ping, rc = -ENODEV);
500                                                                                                                              
501                 if (!obd->obd_attached) {
502                         CERROR("Device %d not attached\n", obd->obd_minor);
503                         GOTO(out_ping, rc = -ENODEV);
504                 }
505                 if (!obd->obd_set_up) {
506                         CERROR("Device %d still not setup\n", obd->obd_minor);
507                         GOTO(out_ping, rc = -ENODEV);
508                 }
509                 cli = &obd->u.cli;
510                 req = ptlrpc_prep_req(cli->cl_import, OBD_PING, 0, NULL, NULL);
511                 if (!req)
512                         GOTO(out_ping, rc = -ENOMEM);
513
514                 req->rq_replen = lustre_msg_size(0, NULL);
515                 req->rq_send_state = LUSTRE_IMP_FULL;
516
517                 rc = ptlrpc_queue_wait(req);
518
519                 ptlrpc_req_finished(req);                                                                                                 
520         out_ping:
521                 obd_ioctl_freedata(buf, len);
522                 return rc;
523         }
524         case OBD_IOC_LLOG_CATINFO: {
525                 struct ptlrpc_request *req = NULL;
526                 char *buf = NULL;
527                 int rc, len = 0;
528                 char *bufs[2], *str;
529                 int lens[2], size;
530                 
531                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
532                 if (rc)
533                         RETURN(rc);
534                 data = (void *)buf;
535
536                 if (!data->ioc_inlbuf1) {
537                         obd_ioctl_freedata(buf, len);
538                         RETURN(-EINVAL);
539                 }
540                 
541                 lens[0] = data->ioc_inllen1;
542                 bufs[0] = data->ioc_inlbuf1;
543                 if (data->ioc_inllen2) {
544                         lens[1] = data->ioc_inllen2;
545                         bufs[1] = data->ioc_inlbuf2;
546                 } else {
547                         lens[1] = 0;
548                         bufs[1] = NULL;
549                 }
550                 size = data->ioc_plen1;
551                 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import, LLOG_CATINFO, 
552                                       2, lens, bufs);
553                 if (!req)
554                         GOTO(out_catinfo, rc = -ENOMEM);
555                 req->rq_replen = lustre_msg_size(1, &size);
556                
557                 rc = ptlrpc_queue_wait(req);
558                 str = lustre_msg_string(req->rq_repmsg, 0, data->ioc_plen1);
559                 if (!rc)
560                         rc = copy_to_user(data->ioc_pbuf1, str, 
561                                           data->ioc_plen1);
562                 ptlrpc_req_finished(req);
563         out_catinfo:
564                 obd_ioctl_freedata(buf, len);
565                 RETURN(rc);
566         }                  
567         default:
568                 return obd_iocontrol(cmd, sbi->ll_osc_exp,0,NULL,(void *)arg);
569         }
570 }
571
572 int ll_dir_open(struct inode *inode, struct file *file)
573 {
574         return ll_file_open(inode, file);
575 }
576
577 int ll_dir_release(struct inode *inode, struct file *file)
578 {
579         return ll_file_release(inode, file);
580 }
581
582 struct file_operations ll_dir_operations = {
583         open: ll_dir_open,
584         release: ll_dir_release,
585         read: generic_read_dir,
586         readdir: ll_readdir,
587         ioctl: ll_dir_ioctl
588 };
589