Whamcloud - gitweb
5b9955ee5e084513ae9860152c60218d3ad27f88
[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  * Directory code for lustre client.
5  *
6  *  Copyright (C) 2002--2007 Cluster File Systems, Inc.
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  *
26  */
27
28 #include <linux/fs.h>
29 #include <linux/pagemap.h>
30 #include <linux/mm.h>
31 #include <linux/version.h>
32 #include <linux/smp_lock.h>
33 #include <asm/uaccess.h>
34 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
35 # include <linux/locks.h>   // for wait_on_buffer
36 #else
37 # include <linux/buffer_head.h>   // for wait_on_buffer
38 #endif
39
40 #define DEBUG_SUBSYSTEM S_LLITE
41
42 #include <obd_support.h>
43 #include <obd_class.h>
44 #include <lustre_lib.h>
45 #include <lustre/lustre_idl.h>
46 #include <lustre_lite.h>
47 #include <lustre_dlm.h>
48 #include "llite_internal.h"
49
50 /*
51  * Directory entries are currently in the same format as ext2/ext3, but will
52  * be changed in the future to accomodate FIDs
53  */
54 #define LL_DIR_NAME_LEN (255)
55
56 static const int LL_DIR_PAD = 4;
57
58 struct ll_dir_entry {
59         /* number of inode, referenced by this entry */
60         __le32  lde_inode;
61         /* total record length, multiple of LL_DIR_PAD */
62         __le16  lde_rec_len;
63         /* length of name */
64         __u8    lde_name_len;
65         /* file type: regular, directory, device, etc. */
66         __u8    lde_file_type;
67         /* name. NOT NUL-terminated */
68         char    lde_name[LL_DIR_NAME_LEN];
69 };
70
71 static inline unsigned ll_dir_rec_len(unsigned name_len)
72 {
73         return (name_len + 8 + LL_DIR_PAD - 1) & ~(LL_DIR_PAD - 1);
74 }
75
76 #ifndef HAVE_PAGE_CHECKED
77 #ifdef HAVE_PG_FS_MISC
78 #define PageChecked(page)        test_bit(PG_fs_misc, &(page)->flags)
79 #define SetPageChecked(page)     set_bit(PG_fs_misc, &(page)->flags)
80 #else
81 #error PageChecked or PageFsMisc not defined in kernel
82 #endif
83 #endif
84
85 /* returns the page unlocked, but with a reference */
86 static int ll_dir_readpage(struct file *file, struct page *page)
87 {
88         struct inode *inode = page->mapping->host;
89         struct ll_fid mdc_fid;
90         __u64 offset;
91         struct ptlrpc_request *request;
92         struct mds_body *body;
93         int rc = 0;
94         ENTRY;
95
96         offset = (__u64)page->index << CFS_PAGE_SHIFT;
97         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) off "LPU64"\n",
98                inode->i_ino, inode->i_generation, inode, offset);
99
100         mdc_pack_fid(&mdc_fid, inode->i_ino, inode->i_generation, S_IFDIR);
101
102         rc = mdc_readpage(ll_i2sbi(inode)->ll_mdc_exp, &mdc_fid,
103                           offset, page, &request);
104         if (!rc) {
105                 body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
106                                       sizeof(*body));
107                 LASSERT(body != NULL); /* checked by mdc_readpage() */
108                 /* swabbed by mdc_readpage() */
109                 LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
110
111                 if (body->size != i_size_read(inode)) {
112                         ll_inode_size_lock(inode, 0);
113                         i_size_write(inode, body->size);
114                         ll_inode_size_unlock(inode, 0);
115                 }
116
117                 SetPageUptodate(page);
118         }
119         ptlrpc_req_finished(request);
120
121         unlock_page(page);
122         EXIT;
123         return rc;
124 }
125
126 struct address_space_operations ll_dir_aops = {
127         .readpage  = ll_dir_readpage,
128 };
129
130 static inline unsigned ll_dir_page_mask(struct inode *inode)
131 {
132         return ~(inode->i_sb->s_blocksize - 1);
133 }
134
135 /*
136  * Check consistency of a single entry.
137  */
138 static int ll_dir_check_entry(struct inode *dir, struct ll_dir_entry *ent,
139                               unsigned offset, unsigned rec_len, pgoff_t index)
140 {
141         const char *msg;
142
143         /*
144          * Consider adding more checks.
145          */
146
147         if (unlikely(rec_len < ll_dir_rec_len(1)))
148                 msg = "entry is too short";
149         else if (unlikely(rec_len & 3))
150                 msg = "wrong alignment";
151         else if (unlikely(rec_len < ll_dir_rec_len(ent->lde_name_len)))
152                 msg = "rec_len doesn't match name_len";
153         else if (unlikely(((offset + rec_len - 1) ^ offset) &
154                           ll_dir_page_mask(dir)))
155                 msg = "directory entry across blocks";
156         else
157                 return 0;
158         CERROR("%s: bad entry in directory %lu/%u: %s - "
159                "offset=%lu+%u, inode=%lu, rec_len=%d,"
160                " name_len=%d\n", ll_i2mdcexp(dir)->exp_obd->obd_name,
161                dir->i_ino, dir->i_generation, msg,
162                index << CFS_PAGE_SHIFT,
163                offset, (unsigned long)le32_to_cpu(ent->lde_inode),
164                rec_len, ent->lde_name_len);
165         return -EIO;
166 }
167
168 static inline struct ll_dir_entry *ll_entry_at(void *base, unsigned offset)
169 {
170         return (struct ll_dir_entry *)(base + offset);
171 }
172
173 static void ll_dir_check_page(struct inode *dir, struct page *page)
174 {
175         int      err;
176         unsigned size = dir->i_sb->s_blocksize;
177         char    *addr = page_address(page);
178         unsigned off;
179         unsigned limit;
180         unsigned reclen;
181
182         struct ll_dir_entry *ent;
183
184         err = 0;
185         if ((i_size_read(dir) >> CFS_PAGE_SHIFT) == (__u64)page->index) {
186                 /*
187                  * Last page.
188                  */
189                 limit = i_size_read(dir) & ~CFS_PAGE_MASK;
190                 if (limit & (size - 1)) {
191                         CERROR("%s: dir %lu/%u size %llu doesn't match %u\n",
192                                ll_i2mdcexp(dir)->exp_obd->obd_name, dir->i_ino,
193                                dir->i_generation, i_size_read(dir), size);
194                         err++;
195                 } else {
196                         /*
197                          * Place dummy forwarding entries to streamline
198                          * ll_readdir().
199                          */
200                         for (off = limit; off < CFS_PAGE_SIZE; off += size) {
201                                 ent = ll_entry_at(addr, off);
202                                 ent->lde_rec_len = cpu_to_le16(size);
203                                 ent->lde_name_len = 0;
204                                 ent->lde_inode = 0;
205                         }
206                 }
207         } else
208                 limit = CFS_PAGE_SIZE;
209
210         for (off = 0;
211              !err && off <= limit - ll_dir_rec_len(1); off += reclen) {
212                 ent    = ll_entry_at(addr, off);
213                 reclen = le16_to_cpu(ent->lde_rec_len);
214                 err    = ll_dir_check_entry(dir, ent, off, reclen, page->index);
215         }
216
217         if (!err && off != limit) {
218                 ent = ll_entry_at(addr, off);
219                 CERROR("%s: entry in directory %lu/%u spans the page boundary "
220                        "offset="LPU64"+%u, inode=%lu\n",
221                        ll_i2mdcexp(dir)->exp_obd->obd_name,
222                        dir->i_ino, dir->i_generation,
223                        (__u64)page->index << CFS_PAGE_SHIFT,
224                        off, (unsigned long)le32_to_cpu(ent->lde_inode));
225                 err++;
226         }
227         if (err)
228                 SetPageError(page);
229         SetPageChecked(page);
230 }
231
232 struct page *ll_get_dir_page(struct inode *dir, unsigned long n)
233 {
234         struct ldlm_res_id res_id =
235                 { .name = { dir->i_ino, (__u64)dir->i_generation} };
236         struct lustre_handle lockh;
237         struct obd_device *obddev = class_exp2obd(ll_i2sbi(dir)->ll_mdc_exp);
238         struct address_space *mapping = dir->i_mapping;
239         struct page *page;
240         ldlm_policy_data_t policy = {.l_inodebits = {MDS_INODELOCK_UPDATE} };
241         int rc;
242
243         rc = ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
244                              &res_id, LDLM_IBITS, &policy, LCK_CR, &lockh);
245         if (!rc) {
246                 struct lookup_intent it = { .it_op = IT_READDIR };
247                 struct ldlm_enqueue_info einfo = { LDLM_IBITS, LCK_CR,
248                        ll_mdc_blocking_ast, ldlm_completion_ast, NULL, dir };
249                 struct ptlrpc_request *request;
250                 struct mdc_op_data data;
251
252                 ll_prepare_mdc_op_data(&data, dir, NULL, NULL, 0, 0, NULL);
253
254                 rc = mdc_enqueue(ll_i2sbi(dir)->ll_mdc_exp, &einfo, &it,
255                                  &data, &lockh, NULL, 0, 0);
256
257                 request = (struct ptlrpc_request *)it.d.lustre.it_data;
258                 if (request)
259                         ptlrpc_req_finished(request);
260                 if (rc < 0) {
261                         CERROR("lock enqueue: rc: %d\n", rc);
262                         return ERR_PTR(rc);
263                 }
264         }
265         ldlm_lock_dump_handle(D_OTHER, &lockh);
266
267         page = read_cache_page(mapping, n,
268                                (filler_t*)mapping->a_ops->readpage, NULL);
269         if (IS_ERR(page))
270                 GOTO(out_unlock, page);
271
272         wait_on_page(page);
273         (void)kmap(page);
274         if (!PageUptodate(page))
275                 goto fail;
276         if (!PageChecked(page))
277                 ll_dir_check_page(dir, page);
278         if (PageError(page))
279                 goto fail;
280
281 out_unlock:
282         ldlm_lock_decref(&lockh, LCK_CR);
283         return page;
284
285 fail:
286         kunmap(page);
287         page_cache_release(page);
288         page = ERR_PTR(-EIO);
289         goto out_unlock;
290 }
291
292 /*
293  * p is at least 6 bytes before the end of page
294  */
295 static inline struct ll_dir_entry *ll_dir_next_entry(struct ll_dir_entry *p)
296 {
297         return ll_entry_at(p, le16_to_cpu(p->lde_rec_len));
298 }
299
300 static inline unsigned ll_dir_validate_entry(char *base, unsigned offset,
301                                              unsigned mask)
302 {
303         struct ll_dir_entry *de = ll_entry_at(base, offset);
304         struct ll_dir_entry *p  = ll_entry_at(base, offset & mask);
305         while (p < de && p->lde_rec_len > 0)
306                 p = ll_dir_next_entry(p);
307         return (char *)p - base;
308 }
309
310 /*
311  * File type constants. The same as in ext2 for compatibility.
312  */
313
314 enum {
315         LL_DIR_FT_UNKNOWN,
316         LL_DIR_FT_REG_FILE,
317         LL_DIR_FT_DIR,
318         LL_DIR_FT_CHRDEV,
319         LL_DIR_FT_BLKDEV,
320         LL_DIR_FT_FIFO,
321         LL_DIR_FT_SOCK,
322         LL_DIR_FT_SYMLINK,
323         LL_DIR_FT_MAX
324 };
325
326 static unsigned char ll_dir_filetype_table[LL_DIR_FT_MAX] = {
327         [LL_DIR_FT_UNKNOWN]  = DT_UNKNOWN,
328         [LL_DIR_FT_REG_FILE] = DT_REG,
329         [LL_DIR_FT_DIR]      = DT_DIR,
330         [LL_DIR_FT_CHRDEV]   = DT_CHR,
331         [LL_DIR_FT_BLKDEV]   = DT_BLK,
332         [LL_DIR_FT_FIFO]     = DT_FIFO,
333         [LL_DIR_FT_SOCK]     = DT_SOCK,
334         [LL_DIR_FT_SYMLINK]  = DT_LNK,
335 };
336
337 /*
338  * Process one page. Returns:
339  *
340  *     -ve: filldir commands readdir to stop.
341  *     +ve: number of entries submitted to filldir.
342  *       0: no live entries on this page.
343  */
344
345 int ll_readdir_page(char *addr, __u64 base, unsigned *offset,
346                     filldir_t filldir, void *cookie)
347 {
348         struct ll_dir_entry *de;
349         char *end;
350         int nr;
351
352         de = ll_entry_at(addr, *offset);
353         end = addr + CFS_PAGE_SIZE - ll_dir_rec_len(1);
354         for (nr = 0 ;(char*)de <= end; de = ll_dir_next_entry(de)) {
355                 if (de->lde_inode != 0) {
356                         nr++;
357                         *offset = (char *)de - addr;
358                         if (filldir(cookie, de->lde_name, de->lde_name_len,
359                                     base | *offset, le32_to_cpu(de->lde_inode),
360                                     ll_dir_filetype_table[de->lde_file_type &
361                                                           (LL_DIR_FT_MAX - 1)]))
362                                 return -1;
363                 }
364         }
365         return nr;
366 }
367
368 int ll_readdir(struct file *filp, void *dirent, filldir_t filldir)
369 {
370         struct inode *inode = filp->f_dentry->d_inode;
371         loff_t pos          = filp->f_pos;
372         unsigned offset     = pos & ~CFS_PAGE_MASK;
373         pgoff_t idx         = pos >> CFS_PAGE_SHIFT;
374         pgoff_t npages      = dir_pages(inode);
375         unsigned chunk_mask = ll_dir_page_mask(inode);
376         int need_revalidate = (filp->f_version != inode->i_version);
377         int rc              = 0;
378         int done; /* when this becomes negative --- stop iterating */
379
380         ENTRY;
381
382         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p) pos %llu/%llu\n",
383                inode->i_ino, inode->i_generation, inode,
384                pos, i_size_read(inode));
385
386         /*
387          * Checking ->i_size without the lock. Should be harmless, as server
388          * re-checks.
389          */
390         if (pos > i_size_read(inode) - ll_dir_rec_len(1))
391                 RETURN(0);
392
393         for (done = 0; idx < npages; idx++, offset = 0) {
394                 /*
395                  * We can assume that all blocks on this page are filled with
396                  * entries, because ll_dir_check_page() placed special dummy
397                  * entries for us.
398                  */
399
400                 char *kaddr;
401                 struct page *page;
402
403                 CDEBUG(D_EXT2,"read %lu of dir %lu/%u page %lu/%lu "
404                        "size %llu\n",
405                        CFS_PAGE_SIZE, inode->i_ino, inode->i_generation,
406                        idx, npages, i_size_read(inode));
407                 page = ll_get_dir_page(inode, idx);
408
409                 /* size might have been updated by mdc_readpage */
410                 npages = dir_pages(inode);
411
412                 if (IS_ERR(page)) {
413                         rc = PTR_ERR(page);
414                         CERROR("error reading dir %lu/%u page %lu: rc %d\n",
415                                inode->i_ino, inode->i_generation, idx, rc);
416                         continue;
417                 }
418
419                 kaddr = page_address(page);
420                 if (need_revalidate) {
421                         /*
422                          * File offset was changed by lseek() and possibly
423                          * points in the middle of an entry. Re-scan from the
424                          * beginning of the chunk.
425                          */
426                         offset = ll_dir_validate_entry(kaddr, offset,
427                                                        chunk_mask);
428                         need_revalidate = 0;
429                 }
430                 done = ll_readdir_page(kaddr, idx << CFS_PAGE_SHIFT,
431                                        &offset, filldir, dirent);
432                 kunmap(page);
433                 page_cache_release(page);
434                 if (done > 0)
435                         /*
436                          * Some entries were sent to the user space, return
437                          * success.
438                          */
439                         rc = 0;
440                 else if (done < 0)
441                         /*
442                          * filldir is satisfied.
443                          */
444                         break;
445         }
446
447         filp->f_pos = (idx << CFS_PAGE_SHIFT) | offset;
448         filp->f_version = inode->i_version;
449         touch_atime(filp->f_vfsmnt, filp->f_dentry);
450
451         RETURN(rc);
452 }
453
454 #define QCTL_COPY(out, in)              \
455 do {                                    \
456         Q_COPY(out, in, qc_cmd);        \
457         Q_COPY(out, in, qc_type);       \
458         Q_COPY(out, in, qc_id);         \
459         Q_COPY(out, in, qc_stat);       \
460         Q_COPY(out, in, qc_dqinfo);     \
461         Q_COPY(out, in, qc_dqblk);      \
462 } while (0)
463
464 int ll_send_mgc_param(struct obd_export *mgc, char *string)
465 {
466         struct mgs_send_param *msp;
467         int rc = 0;
468
469         OBD_ALLOC_PTR(msp);
470         if (!msp)
471                 return -ENOMEM;
472
473         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
474         rc = obd_set_info_async(mgc, strlen(KEY_SET_INFO), KEY_SET_INFO,
475                                 sizeof(struct mgs_send_param), msp, NULL);
476         if (rc)
477                 CERROR("Failed to set parameter: %d\n", rc);
478
479         OBD_FREE_PTR(msp);
480         return rc;
481 }
482
483 char *ll_get_fsname(struct inode *inode)
484 {
485         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
486         char *ptr, *fsname;
487         int len;
488
489         OBD_ALLOC(fsname, MGS_PARAM_MAXLEN);
490         len = strlen(lsi->lsi_lmd->lmd_profile);
491         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
492         if (ptr && (strcmp(ptr, "-client") == 0))
493                 len -= 7;
494         strncpy(fsname, lsi->lsi_lmd->lmd_profile, len);
495         fsname[len] = '\0';
496
497         return fsname;
498 }
499
500 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
501                      int set_default)
502 {
503         struct ll_sb_info *sbi = ll_i2sbi(inode);
504         struct mdc_op_data data;
505         struct ptlrpc_request *req = NULL;
506         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
507         struct obd_device *mgc = lsi->lsi_mgc;
508         char *fsname = NULL, *param = NULL;
509
510         struct iattr attr = { 0 };
511         int rc = 0;
512
513         /*
514          * This is coming from userspace, so should be in
515          * local endian.  But the MDS would like it in little
516          * endian, so we swab it before we send it.
517          */
518         if (lump->lmm_magic != LOV_USER_MAGIC)
519                 RETURN(-EINVAL);
520
521         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC))
522                 lustre_swab_lov_user_md(lump);
523
524         ll_prepare_mdc_op_data(&data, inode, NULL, NULL, 0, 0, NULL);
525
526         /* swabbing is done in lov_setstripe() on server side */
527         rc = mdc_setattr(sbi->ll_mdc_exp, &data,
528                          &attr, lump, sizeof(*lump), NULL, 0, &req);
529         if (rc) {
530                 ptlrpc_req_finished(req);
531                 if (rc != -EPERM && rc != -EACCES)
532                         CERROR("mdc_setattr fails: rc = %d\n", rc);
533                 return rc;
534         }
535         ptlrpc_req_finished(req);
536
537         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
538                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
539
540                 /* Get fsname and assume devname to be -MDT0000. */
541                 fsname = ll_get_fsname(inode);
542                 /* Set root stripesize */
543                 sprintf(param, "%s-MDT0000.lov.stripesize=%u", fsname,
544                         lump->lmm_stripe_size);
545                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
546                 if (rc)
547                         goto end;
548
549                 /* Set root stripecount */
550                 sprintf(param, "%s-MDT0000.lov.stripecount=%u", fsname,
551                         lump->lmm_stripe_count);
552                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
553                 if (rc)
554                         goto end;
555
556                 /* Set root stripeoffset */
557                 sprintf(param, "%s-MDT0000.lov.stripeoffset=%u", fsname,
558                         lump->lmm_stripe_offset);
559                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
560                 if (rc)
561                         goto end;
562 end:
563                 if (fsname)
564                         OBD_FREE(fsname, MGS_PARAM_MAXLEN);
565                 if (param)
566                         OBD_FREE(param, MGS_PARAM_MAXLEN);
567         }
568         return rc;
569 }
570
571 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
572                      int *lmm_size, struct ptlrpc_request **request)
573 {
574         struct ll_sb_info *sbi = ll_i2sbi(inode);
575         struct ll_fid     fid;
576         struct mds_body   *body;
577         struct lov_mds_md *lmm = NULL;
578         struct ptlrpc_request *req = NULL;
579         int rc, lmmsize;
580
581         ll_inode2fid(&fid, inode);
582
583         rc = ll_get_max_mdsize(sbi, &lmmsize);
584         if (rc)
585                 RETURN(rc);
586
587         rc = mdc_getattr(sbi->ll_mdc_exp, &fid,
588                         OBD_MD_FLEASIZE|OBD_MD_FLDIREA,
589                         lmmsize, &req);
590         if (rc < 0) {
591                 CDEBUG(D_INFO, "mdc_getattr failed on inode "
592                        "%lu/%u: rc %d\n", inode->i_ino,
593                        inode->i_generation, rc);
594                 GOTO(out, rc);
595         }
596         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
597                         sizeof(*body));
598         LASSERT(body != NULL); /* checked by mdc_getattr_name */
599         /* swabbed by mdc_getattr_name */
600         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF));
601
602         lmmsize = body->eadatasize;
603
604         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
605             lmmsize == 0) {
606                 GOTO(out, rc = -ENODATA);
607         }
608
609         lmm = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, lmmsize);
610         LASSERT(lmm != NULL);
611         LASSERT(lustre_rep_swabbed(req, REPLY_REC_OFF + 1));
612
613         /*
614          * This is coming from the MDS, so is probably in
615          * little endian.  We convert it to host endian before
616          * passing it to userspace.
617          */
618         if (lmm->lmm_magic == __swab32(LOV_MAGIC)) {
619                 lustre_swab_lov_user_md((struct lov_user_md *)lmm);
620         }
621 out:
622         *lmmp = lmm;
623         *lmm_size = lmmsize;
624         *request = req;
625         return rc;
626 }
627
628 static int ll_dir_ioctl(struct inode *inode, struct file *file,
629                         unsigned int cmd, unsigned long arg)
630 {
631         struct ll_sb_info *sbi = ll_i2sbi(inode);
632         struct obd_ioctl_data *data;
633         ENTRY;
634
635         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), cmd=%#x\n",
636                inode->i_ino, inode->i_generation, inode, cmd);
637
638         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
639         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
640                 return -ENOTTY;
641
642         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
643         switch(cmd) {
644         case EXT3_IOC_GETFLAGS:
645         case EXT3_IOC_SETFLAGS:
646                 RETURN(ll_iocontrol(inode, file, cmd, arg));
647         case EXT3_IOC_GETVERSION_OLD:
648         case EXT3_IOC_GETVERSION:
649                 RETURN(put_user(inode->i_generation, (int *)arg));
650         /* We need to special case any other ioctls we want to handle,
651          * to send them to the MDS/OST as appropriate and to properly
652          * network encode the arg field.
653         case EXT3_IOC_SETVERSION_OLD:
654         case EXT3_IOC_SETVERSION:
655         */
656         case IOC_MDC_LOOKUP: {
657                 struct ptlrpc_request *request = NULL;
658                 struct ll_fid fid;
659                 char *buf = NULL;
660                 char *filename;
661                 int namelen, rc, len = 0;
662
663                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
664                 if (rc)
665                         RETURN(rc);
666                 data = (void *)buf;
667
668                 filename = data->ioc_inlbuf1;
669                 namelen = data->ioc_inllen1;
670
671                 if (namelen < 1) {
672                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
673                         GOTO(out, rc = -EINVAL);
674                 }
675
676                 ll_inode2fid(&fid, inode);
677                 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, filename, namelen,
678                                       OBD_MD_FLID, 0, &request);
679                 if (rc < 0) {
680                         CDEBUG(D_INFO, "mdc_getattr_name: %d\n", rc);
681                         GOTO(out, rc);
682                 }
683
684                 ptlrpc_req_finished(request);
685
686                 EXIT;
687         out:
688                 obd_ioctl_freedata(buf, len);
689                 return rc;
690         }
691         case LL_IOC_LOV_SETSTRIPE: {
692                 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
693                 int rc = 0;
694                 int set_default = 0;
695
696                 LASSERT(sizeof(lum) == sizeof(*lump));
697                 LASSERT(sizeof(lum.lmm_objects[0]) ==
698                         sizeof(lump->lmm_objects[0]));
699                 rc = copy_from_user(&lum, lump, sizeof(lum));
700                 if (rc)
701                         return(-EFAULT);
702
703                 if (inode->i_sb->s_root == file->f_dentry)
704                         set_default = 1;
705
706                 rc = ll_dir_setstripe(inode, &lum, set_default);
707
708                 return rc;
709         }
710         case LL_IOC_OBD_STATFS:
711                 RETURN(ll_obd_statfs(inode, (void *)arg));
712         case LL_IOC_LOV_GETSTRIPE:
713         case LL_IOC_MDC_GETINFO:
714         case IOC_MDC_GETFILEINFO:
715         case IOC_MDC_GETFILESTRIPE: {
716                 struct ptlrpc_request *request = NULL;
717                 struct mds_body *body;
718                 struct lov_user_md *lump;
719                 struct lov_mds_md *lmm = NULL;
720                 char *filename = NULL;
721                 int rc, lmmsize;
722
723                 if (cmd == IOC_MDC_GETFILEINFO ||
724                     cmd == IOC_MDC_GETFILESTRIPE) {
725                         filename = getname((const char *)arg);
726                         if (IS_ERR(filename))
727                                 RETURN(PTR_ERR(filename));
728
729                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
730                                                       &lmmsize, &request);
731                 } else {
732                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
733                 }
734
735                 if (request) {
736                         body = lustre_msg_buf(request->rq_repmsg, REPLY_REC_OFF,
737                                               sizeof(*body));
738                         LASSERT(body != NULL); /* checked by mdc_getattr_name */
739                         /* swabbed by mdc_getattr_name */
740                         LASSERT(lustre_rep_swabbed(request, REPLY_REC_OFF));
741                 } else {
742                         GOTO(out_req, rc);
743                 }
744
745                 if (rc < 0) {
746                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
747                                                cmd == LL_IOC_MDC_GETINFO))
748                                 GOTO(skip_lmm, rc = 0);
749                         else
750                                 GOTO(out_req, rc);
751                 }
752
753                 if (cmd == IOC_MDC_GETFILESTRIPE ||
754                     cmd == LL_IOC_LOV_GETSTRIPE) {
755                         lump = (struct lov_user_md *)arg;
756                 } else {
757                         struct lov_user_mds_data *lmdp;
758                         lmdp = (struct lov_user_mds_data *)arg;
759                         lump = &lmdp->lmd_lmm;
760                 }
761                 rc = copy_to_user(lump, lmm, lmmsize);
762                 if (rc)
763                         GOTO(out_lmm, rc = -EFAULT);
764         skip_lmm:
765                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
766                         struct lov_user_mds_data *lmdp;
767                         lstat_t st = { 0 };
768
769                         st.st_dev     = inode->i_sb->s_dev;
770                         st.st_mode    = body->mode;
771                         st.st_nlink   = body->nlink;
772                         st.st_uid     = body->uid;
773                         st.st_gid     = body->gid;
774                         st.st_rdev    = body->rdev;
775                         st.st_size    = body->size;
776                         st.st_blksize = CFS_PAGE_SIZE;
777                         st.st_blocks  = body->blocks;
778                         st.st_atime   = body->atime;
779                         st.st_mtime   = body->mtime;
780                         st.st_ctime   = body->ctime;
781                         st.st_ino     = body->ino;
782
783                         lmdp = (struct lov_user_mds_data *)arg;
784                         rc = copy_to_user(&lmdp->lmd_st, &st, sizeof(st));
785                         if (rc)
786                                 GOTO(out_lmm, rc = -EFAULT);
787                 }
788
789                 EXIT;
790         out_lmm:
791                 if (lmm && lmm->lmm_magic == LOV_MAGIC_JOIN)
792                         OBD_FREE(lmm, lmmsize);
793         out_req:
794                 ptlrpc_req_finished(request);
795                 if (filename)
796                         putname(filename);
797                 return rc;
798         }
799         case IOC_LOV_GETINFO: {
800                 struct lov_user_mds_data *lumd;
801                 struct lov_stripe_md *lsm;
802                 struct lov_user_md *lum;
803                 struct lov_mds_md *lmm;
804                 int lmmsize;
805                 lstat_t st;
806                 int rc;
807
808                 lumd = (struct lov_user_mds_data *)arg;
809                 lum = &lumd->lmd_lmm;
810
811                 rc = ll_get_max_mdsize(sbi, &lmmsize);
812                 if (rc)
813                         RETURN(rc);
814
815                 OBD_ALLOC(lmm, lmmsize);
816                 rc = copy_from_user(lmm, lum, lmmsize);
817                 if (rc)
818                         GOTO(free_lmm, rc = -EFAULT);
819
820                 rc = obd_unpackmd(sbi->ll_osc_exp, &lsm, lmm, lmmsize);
821                 if (rc < 0)
822                         GOTO(free_lmm, rc = -ENOMEM);
823
824                 rc = obd_checkmd(sbi->ll_osc_exp, sbi->ll_mdc_exp, lsm);
825                 if (rc)
826                         GOTO(free_lsm, rc);
827
828                 /* Perform glimpse_size operation. */
829                 memset(&st, 0, sizeof(st));
830
831                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
832                 if (rc)
833                         GOTO(free_lsm, rc);
834
835                 rc = copy_to_user(&lumd->lmd_st, &st, sizeof(st));
836                 if (rc)
837                         GOTO(free_lsm, rc = -EFAULT);
838
839                 EXIT;
840         free_lsm:
841                 obd_free_memmd(sbi->ll_osc_exp, &lsm);
842         free_lmm:
843                 OBD_FREE(lmm, lmmsize);
844                 return rc;
845         }
846         case OBD_IOC_LLOG_CATINFO: {
847                 struct ptlrpc_request *req = NULL;
848                 char *buf = NULL;
849                 int rc, len = 0;
850                 char *bufs[3] = { NULL }, *str;
851                 int lens[3] = { sizeof(struct ptlrpc_body) };
852                 int size[2] = { sizeof(struct ptlrpc_body) };
853
854                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
855                 if (rc)
856                         RETURN(rc);
857                 data = (void *)buf;
858
859                 if (!data->ioc_inlbuf1) {
860                         obd_ioctl_freedata(buf, len);
861                         RETURN(-EINVAL);
862                 }
863
864                 lens[REQ_REC_OFF] = data->ioc_inllen1;
865                 bufs[REQ_REC_OFF] = data->ioc_inlbuf1;
866                 if (data->ioc_inllen2) {
867                         lens[REQ_REC_OFF + 1] = data->ioc_inllen2;
868                         bufs[REQ_REC_OFF + 1] = data->ioc_inlbuf2;
869                 } else {
870                         lens[REQ_REC_OFF + 1] = 0;
871                         bufs[REQ_REC_OFF + 1] = NULL;
872                 }
873
874                 req = ptlrpc_prep_req(sbi2mdc(sbi)->cl_import,
875                                       LUSTRE_LOG_VERSION, LLOG_CATINFO, 3, lens,
876                                       bufs);
877                 if (!req)
878                         GOTO(out_catinfo, rc = -ENOMEM);
879
880                 size[REPLY_REC_OFF] = data->ioc_plen1;
881                 ptlrpc_req_set_repsize(req, 2, size);
882
883                 rc = ptlrpc_queue_wait(req);
884                 str = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF,
885                                         data->ioc_plen1);
886                 if (!rc)
887                         rc = copy_to_user(data->ioc_pbuf1, str,data->ioc_plen1);
888                 ptlrpc_req_finished(req);
889         out_catinfo:
890                 obd_ioctl_freedata(buf, len);
891                 RETURN(rc);
892         }
893         case OBD_IOC_QUOTACHECK: {
894                 struct obd_quotactl *oqctl;
895                 int rc, error = 0;
896
897                 if (!capable(CAP_SYS_ADMIN))
898                         RETURN(-EPERM);
899
900                 OBD_ALLOC_PTR(oqctl);
901                 if (!oqctl)
902                         RETURN(-ENOMEM);
903                 oqctl->qc_type = arg;
904                 rc = obd_quotacheck(sbi->ll_mdc_exp, oqctl);
905                 if (rc < 0) {
906                         CDEBUG(D_INFO, "mdc_quotacheck failed: rc %d\n", rc);
907                         error = rc;
908                 }
909
910                 rc = obd_quotacheck(sbi->ll_osc_exp, oqctl);
911                 if (rc < 0)
912                         CDEBUG(D_INFO, "osc_quotacheck failed: rc %d\n", rc);
913
914                 OBD_FREE_PTR(oqctl);
915                 return error ?: rc;
916         }
917         case OBD_IOC_POLL_QUOTACHECK: {
918                 struct if_quotacheck *check;
919                 int rc;
920
921                 if (!capable(CAP_SYS_ADMIN))
922                         RETURN(-EPERM);
923
924                 OBD_ALLOC_PTR(check);
925                 if (!check)
926                         RETURN(-ENOMEM);
927
928                 rc = obd_iocontrol(cmd, sbi->ll_mdc_exp, 0, (void *)check,
929                                    NULL);
930                 if (rc) {
931                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
932                         if (copy_to_user((void *)arg, check, sizeof(*check)))
933                                 rc = -EFAULT;
934                         GOTO(out_poll, rc);
935                 }
936
937                 rc = obd_iocontrol(cmd, sbi->ll_osc_exp, 0, (void *)check,
938                                    NULL);
939                 if (rc) {
940                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
941                         if (copy_to_user((void *)arg, check, sizeof(*check)))
942                                 rc = -EFAULT;
943                         GOTO(out_poll, rc);
944                 }
945         out_poll:
946                 OBD_FREE_PTR(check);
947                 RETURN(rc);
948         }
949 #ifdef HAVE_QUOTA_SUPPORT
950         case OBD_IOC_QUOTACTL: {
951                 struct if_quotactl *qctl;
952                 struct obd_quotactl *oqctl;
953
954                 int cmd, type, id, rc = 0;
955
956                 OBD_ALLOC_PTR(qctl);
957                 if (!qctl)
958                         RETURN(-ENOMEM);
959
960                 OBD_ALLOC_PTR(oqctl);
961                 if (!oqctl) {
962                         OBD_FREE_PTR(qctl);
963                         RETURN(-ENOMEM);
964                 }
965                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
966                         GOTO(out_quotactl, rc = -EFAULT);
967
968                 cmd = qctl->qc_cmd;
969                 type = qctl->qc_type;
970                 id = qctl->qc_id;
971                 switch (cmd) {
972                 case LUSTRE_Q_INVALIDATE:
973                 case Q_QUOTAON:
974                 case Q_QUOTAOFF:
975                 case Q_SETQUOTA:
976                 case Q_SETINFO:
977                         if (!capable(CAP_SYS_ADMIN))
978                                 GOTO(out_quotactl, rc = -EPERM);
979                         break;
980                 case Q_GETQUOTA:
981                         if (((type == USRQUOTA && current->euid != id) ||
982                              (type == GRPQUOTA && !in_egroup_p(id))) &&
983                             !capable(CAP_SYS_ADMIN))
984                                 GOTO(out_quotactl, rc = -EPERM);
985
986                         /* XXX: dqb_valid is borrowed as a flag to mark that
987                          *      only mds quota is wanted */
988                         if (qctl->qc_dqblk.dqb_valid) {
989                                 qctl->obd_uuid = sbi->ll_mdc_exp->exp_obd->
990                                                         u.cli.cl_target_uuid;
991                                 qctl->qc_dqblk.dqb_valid = 0;
992                         }
993
994                         break;
995                 case Q_GETINFO:
996                         break;
997                 default:
998                         CERROR("unsupported quotactl op: %#x\n", cmd);
999                         GOTO(out_quotactl, -ENOTTY);
1000                 }
1001
1002                 QCTL_COPY(oqctl, qctl);
1003
1004                 if (qctl->obd_uuid.uuid[0]) {
1005                         struct obd_device *obd;
1006                         struct obd_uuid *uuid = &qctl->obd_uuid;
1007
1008                         obd = class_find_client_notype(uuid,
1009                                          &sbi->ll_osc_exp->exp_obd->obd_uuid);
1010                         if (!obd)
1011                                 GOTO(out_quotactl, rc = -ENOENT);
1012
1013                         if (cmd == Q_GETINFO)
1014                                 oqctl->qc_cmd = Q_GETOINFO;
1015                         else if (cmd == Q_GETQUOTA)
1016                                 oqctl->qc_cmd = Q_GETOQUOTA;
1017                         else
1018                                 GOTO(out_quotactl, rc = -EINVAL);
1019
1020                         if (sbi->ll_mdc_exp->exp_obd == obd) {
1021                                 rc = obd_quotactl(sbi->ll_mdc_exp, oqctl);
1022                         } else {
1023                                 int i;
1024                                 struct obd_export *exp;
1025                                 struct lov_obd *lov = &sbi->ll_osc_exp->
1026                                                             exp_obd->u.lov;
1027
1028                                 for (i = 0; i < lov->desc.ld_tgt_count; i++) {
1029                                         if (!lov->lov_tgts[i] ||
1030                                             !lov->lov_tgts[i]->ltd_active)
1031                                                 continue;
1032                                         exp = lov->lov_tgts[i]->ltd_exp;
1033                                         if (exp->exp_obd == obd) {
1034                                                 rc = obd_quotactl(exp, oqctl);
1035                                                 break;
1036                                         }
1037                                 }
1038                         }
1039
1040                         oqctl->qc_cmd = cmd;
1041                         QCTL_COPY(qctl, oqctl);
1042
1043                         if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1044                                 rc = -EFAULT;
1045
1046                         GOTO(out_quotactl, rc);
1047                 }
1048
1049                 rc = obd_quotactl(sbi->ll_mdc_exp, oqctl);
1050                 if (rc && rc != -EBUSY && cmd == Q_QUOTAON) {
1051                         oqctl->qc_cmd = Q_QUOTAOFF;
1052                         obd_quotactl(sbi->ll_mdc_exp, oqctl);
1053                 }
1054
1055                 QCTL_COPY(qctl, oqctl);
1056
1057                 if (copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1058                         rc = -EFAULT;
1059         out_quotactl:
1060                 OBD_FREE_PTR(qctl);
1061                 OBD_FREE_PTR(oqctl);
1062                 RETURN(rc);
1063         }
1064 #endif /* HAVE_QUOTA_SUPPORT */
1065         case OBD_IOC_GETNAME_OLD:
1066         case OBD_IOC_GETNAME: {
1067                 struct obd_device *obd = class_exp2obd(sbi->ll_osc_exp);
1068                 if (!obd)
1069                         RETURN(-EFAULT);
1070                 if (copy_to_user((void *)arg, obd->obd_name,
1071                                 strlen(obd->obd_name) + 1))
1072                         RETURN (-EFAULT);
1073                 RETURN(0);
1074         }
1075         default:
1076                 RETURN(obd_iocontrol(cmd, sbi->ll_osc_exp,0,NULL,(void *)arg));
1077         }
1078 }
1079
1080 struct file_operations ll_dir_operations = {
1081         .open     = ll_file_open,
1082         .release  = ll_file_release,
1083         .read     = generic_read_dir,
1084         .readdir  = ll_readdir,
1085         .ioctl    = ll_dir_ioctl
1086 };
1087