Whamcloud - gitweb
b97edda445cc311e20fa2fc5c9ed1f4df0bebf55
[fs/lustre-release.git] / lustre / llite / dir.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
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/llite/dir.c
37  *
38  * Directory code for lustre client.
39  */
40
41 #include <linux/fs.h>
42 #include <linux/pagemap.h>
43 #include <linux/mm.h>
44 #include <linux/version.h>
45 #include <asm/uaccess.h>
46 #include <linux/buffer_head.h>   // for wait_on_buffer
47 #include <linux/pagevec.h>
48
49 #define DEBUG_SUBSYSTEM S_LLITE
50
51 #include <lustre/lustre_idl.h>
52 #include <obd_support.h>
53 #include <obd_class.h>
54 #include <lustre_lib.h>
55 #include <lustre/lustre_idl.h>
56 #include <lustre_lite.h>
57 #include <lustre_dlm.h>
58 #include <lustre_fid.h>
59 #include "llite_internal.h"
60
61 /*
62  * (new) readdir implementation overview.
63  *
64  * Original lustre readdir implementation cached exact copy of raw directory
65  * pages on the client. These pages were indexed in client page cache by
66  * logical offset in the directory file. This design, while very simple and
67  * intuitive had some inherent problems:
68  *
69  *     . it implies that byte offset to the directory entry serves as a
70  *     telldir(3)/seekdir(3) cookie, but that offset is not stable: in
71  *     ext3/htree directory entries may move due to splits, and more
72  *     importantly,
73  *
74  *     . it is incompatible with the design of split directories for cmd3,
75  *     that assumes that names are distributed across nodes based on their
76  *     hash, and so readdir should be done in hash order.
77  *
78  * New readdir implementation does readdir in hash order, and uses hash of a
79  * file name as a telldir/seekdir cookie. This led to number of complications:
80  *
81  *     . hash is not unique, so it cannot be used to index cached directory
82  *     pages on the client (note, that it requires a whole pageful of hash
83  *     collided entries to cause two pages to have identical hashes);
84  *
85  *     . hash is not unique, so it cannot, strictly speaking, be used as an
86  *     entry cookie. ext3/htree has the same problem and lustre implementation
87  *     mimics their solution: seekdir(hash) positions directory at the first
88  *     entry with the given hash.
89  *
90  * Client side.
91  *
92  * 0. caching
93  *
94  * Client caches directory pages using hash of the first entry as an index. As
95  * noted above hash is not unique, so this solution doesn't work as is:
96  * special processing is needed for "page hash chains" (i.e., sequences of
97  * pages filled with entries all having the same hash value).
98  *
99  * First, such chains have to be detected. To this end, server returns to the
100  * client the hash of the first entry on the page next to one returned. When
101  * client detects that this hash is the same as hash of the first entry on the
102  * returned page, page hash collision has to be handled. Pages in the
103  * hash chain, except first one, are termed "overflow pages".
104  *
105  * Solution to index uniqueness problem is to not cache overflow
106  * pages. Instead, when page hash collision is detected, all overflow pages
107  * from emerging chain are immediately requested from the server and placed in
108  * a special data structure (struct ll_dir_chain). This data structure is used
109  * by ll_readdir() to process entries from overflow pages. When readdir
110  * invocation finishes, overflow pages are discarded. If page hash collision
111  * chain weren't completely processed, next call to readdir will again detect
112  * page hash collision, again read overflow pages in, process next portion of
113  * entries and again discard the pages. This is not as wasteful as it looks,
114  * because, given reasonable hash, page hash collisions are extremely rare.
115  *
116  * 1. directory positioning
117  *
118  * When seekdir(hash) is called, original
119  *
120  *
121  *
122  *
123  *
124  *
125  *
126  *
127  * Server.
128  *
129  * identification of and access to overflow pages
130  *
131  * page format
132  *
133  * Page in MDS_READPAGE RPC is packed in LU_PAGE_SIZE, and each page contains
134  * a header lu_dirpage which describes the start/end hash, and whether this
135  * page is empty (contains no dir entry) or hash collide with next page.
136  * After client receives reply, several pages will be integrated into dir page
137  * in PAGE_CACHE_SIZE (if PAGE_CACHE_SIZE greater than LU_PAGE_SIZE), and the
138  * lu_dirpage for this integrated page will be adjusted. See
139  * lmv_adjust_dirpages().
140  *
141  */
142 /**
143  * The following three APIs will be used by llite to iterate directory
144  * entries from MDC dir page caches.
145  *
146  * ll_dir_entry_start(next) will lookup(return) entry by op_hash_offset.
147  * To avoid extra memory allocation, the @entry will be pointed to
148  * the dir entries in MDC page directly, so these pages can not be released
149  * until the entry has been accessed in ll_readdir(or statahead).
150  *
151  * The iterate process will be
152  *
153  * ll_dir_entry_start: locate the page in MDC, and return the first entry.
154  *                     hold the page.
155  *
156  * ll_dir_entry_next: return the next entry in the current page, if it reaches
157  *                    to the end, release current page.
158  *
159  * ll_dir_entry_end: release the last page.
160  **/
161 struct lu_dirent *ll_dir_entry_start(struct inode *dir,
162                                      struct md_op_data *op_data,
163                                      struct page **ppage)
164 {
165         struct lu_dirent *entry;
166         struct md_callback cb_op;
167         int rc;
168
169         LASSERT(*ppage == NULL);
170         cb_op.md_blocking_ast = ll_md_blocking_ast;
171         rc = md_read_entry(ll_i2mdexp(dir), op_data, &cb_op, &entry, ppage);
172         if (rc != 0)
173                 entry = ERR_PTR(rc);
174         return entry;
175 }
176
177 struct lu_dirent *ll_dir_entry_next(struct inode *dir,
178                                     struct md_op_data *op_data,
179                                     struct lu_dirent *ent,
180                                     struct page **ppage)
181 {
182         struct lu_dirent *entry;
183         struct md_callback cb_op;
184         int rc;
185
186         LASSERT(*ppage != NULL);
187         cb_op.md_blocking_ast = ll_md_blocking_ast;
188         op_data->op_hash_offset = le64_to_cpu(ent->lde_hash);
189         kunmap(*ppage);
190         page_cache_release(*ppage);
191         *ppage = NULL;
192         rc = md_read_entry(ll_i2mdexp(dir), op_data, &cb_op, &entry, ppage);
193         if (rc != 0)
194                 entry = ERR_PTR(rc);
195         return entry;
196 }
197
198 int ll_dir_read(struct inode *inode, struct md_op_data *op_data,
199                 void *cookie, filldir_t filldir)
200 {
201         struct ll_sb_info       *sbi = ll_i2sbi(inode);
202         struct ll_dir_chain     chain;
203         struct lu_dirent        *ent;
204         int                     api32 = ll_need_32bit_api(sbi);
205         int                     hash64 = sbi->ll_flags & LL_SBI_64BIT_HASH;
206         int                     done = 0;
207         int                     rc = 0;
208         __u64                   hash = MDS_DIR_END_OFF;
209         __u64                   last_hash = MDS_DIR_END_OFF;
210         struct page             *page = NULL;
211         ENTRY;
212
213         ll_dir_chain_init(&chain);
214         for (ent = ll_dir_entry_start(inode, op_data, &page);
215              ent != NULL && !IS_ERR(ent) && !done;
216              ent = ll_dir_entry_next(inode, op_data, ent, &page)) {
217                 __u16          type;
218                 int            namelen;
219                 struct lu_fid  fid;
220                 __u64          lhash;
221                 __u64          ino;
222
223                 hash = le64_to_cpu(ent->lde_hash);
224                 if (hash < op_data->op_hash_offset)
225                         /*
226                          * Skip until we find target hash
227                          * value.
228                          */
229                         continue;
230                 namelen = le16_to_cpu(ent->lde_namelen);
231                 if (namelen == 0)
232                         /*
233                          * Skip dummy record.
234                          */
235                         continue;
236
237                 if (api32 && hash64)
238                         lhash = hash >> 32;
239                 else
240                         lhash = hash;
241                 fid_le_to_cpu(&fid, &ent->lde_fid);
242                 ino = cl_fid_build_ino(&fid, api32);
243                 type = ll_dirent_type_get(ent);
244                 /* For 'll_nfs_get_name_filldir()', it will try
245                  * to access the 'ent' through its 'lde_name',
246                  * so the parameter 'name' for 'filldir()' must
247                  * be part of the 'ent'. */
248                 done = filldir(cookie, ent->lde_name, namelen, lhash,
249                                ino, type);
250                 if (done) {
251                         if (op_data->op_hash_offset != MDS_DIR_END_OFF)
252                                 op_data->op_hash_offset = last_hash;
253                         break;
254                 } else {
255                         last_hash = hash;
256                 }
257         }
258
259         if (IS_ERR(ent))
260                 rc = PTR_ERR(ent);
261
262         if (page != NULL) {
263                 kunmap(page);
264                 page_cache_release(page);
265         }
266
267         ll_dir_chain_fini(&chain);
268         RETURN(rc);
269 }
270
271 static int ll_readdir(struct file *filp, void *cookie, filldir_t filldir)
272 {
273         struct inode            *inode  = filp->f_dentry->d_inode;
274         struct ll_file_data     *lfd    = LUSTRE_FPRIVATE(filp);
275         struct ll_sb_info       *sbi    = ll_i2sbi(inode);
276         __u64                   pos     = lfd->lfd_pos;
277         int                     hash64  = sbi->ll_flags & LL_SBI_64BIT_HASH;
278         int                     api32   = ll_need_32bit_api(sbi);
279         struct md_op_data       *op_data;
280         int                     rc;
281 #ifdef HAVE_TOUCH_ATIME_1ARG
282         struct path             path;
283 #endif
284         ENTRY;
285
286         if (lfd != NULL)
287                 pos = lfd->lfd_pos;
288         else
289                 pos = 0;
290
291         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) pos/size"
292                "%lu/%llu 32bit_api %d\n", PFID(ll_inode2fid(inode)),
293                inode, (unsigned long)pos, i_size_read(inode), api32);
294
295         if (pos == MDS_DIR_END_OFF)
296                 /*
297                  * end-of-file.
298                  */
299                 GOTO(out, rc = 0);
300
301         op_data = ll_prep_md_op_data(NULL, inode, inode, NULL, 0, 0,
302                                      LUSTRE_OPC_ANY, inode);
303         if (IS_ERR(op_data))
304                 GOTO(out, rc = PTR_ERR(op_data));
305
306         op_data->op_hash_offset = pos;
307         op_data->op_max_pages = sbi->ll_md_brw_size >> PAGE_CACHE_SHIFT;
308         rc = ll_dir_read(inode, op_data, cookie, filldir);
309         if (lfd != NULL)
310                 lfd->lfd_pos = op_data->op_hash_offset;
311
312         if (pos == MDS_DIR_END_OFF) {
313                 if (api32)
314                         filp->f_pos = LL_DIR_END_OFF_32BIT;
315                 else
316                         filp->f_pos = LL_DIR_END_OFF;
317         } else {
318                 if (api32 && hash64)
319                         filp->f_pos = op_data->op_hash_offset >> 32;
320                 else
321                         filp->f_pos = op_data->op_hash_offset;
322         }
323
324         ll_finish_md_op_data(op_data);
325         filp->f_version = inode->i_version;
326 #ifdef HAVE_TOUCH_ATIME_1ARG
327 #ifdef HAVE_F_PATH_MNT
328         path.mnt = filp->f_path.mnt;
329 #else
330         path.mnt = filp->f_vfsmnt;
331 #endif
332         path.dentry = filp->f_dentry;
333         touch_atime(&path);
334 #else
335         touch_atime(filp->f_vfsmnt, filp->f_dentry);
336 #endif
337
338 out:
339         if (!rc)
340                 ll_stats_ops_tally(sbi, LPROC_LL_READDIR, 1);
341
342         RETURN(rc);
343 }
344
345 int ll_send_mgc_param(struct obd_export *mgc, char *string)
346 {
347         struct mgs_send_param *msp;
348         int rc = 0;
349
350         OBD_ALLOC_PTR(msp);
351         if (!msp)
352                 return -ENOMEM;
353
354         strncpy(msp->mgs_param, string, MGS_PARAM_MAXLEN);
355         rc = obd_set_info_async(NULL, mgc, sizeof(KEY_SET_INFO), KEY_SET_INFO,
356                                 sizeof(struct mgs_send_param), msp, NULL);
357         if (rc)
358                 CERROR("Failed to set parameter: %d\n", rc);
359         OBD_FREE_PTR(msp);
360
361         return rc;
362 }
363
364 static int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
365                                const char *filename)
366 {
367         struct ptlrpc_request *request = NULL;
368         struct md_op_data *op_data;
369         struct ll_sb_info *sbi = ll_i2sbi(dir);
370         int mode;
371         int err;
372         ENTRY;
373
374         if (unlikely(lump->lum_magic != LMV_USER_MAGIC))
375                 RETURN(-EINVAL);
376
377         if (lump->lum_stripe_offset == (__u32)-1) {
378                 int mdtidx;
379
380                 mdtidx = ll_get_mdt_idx(dir);
381                 if (mdtidx < 0)
382                         RETURN(mdtidx);
383
384                 lump->lum_stripe_offset = mdtidx;
385         }
386
387         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p) name %s"
388                "stripe_offset %d, stripe_count: %u\n",
389                PFID(ll_inode2fid(dir)), dir, filename,
390                (int)lump->lum_stripe_offset, lump->lum_stripe_count);
391
392         if (lump->lum_magic != cpu_to_le32(LMV_USER_MAGIC))
393                 lustre_swab_lmv_user_md(lump);
394
395         mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
396         op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
397                                      strlen(filename), mode, LUSTRE_OPC_MKDIR,
398                                      lump);
399         if (IS_ERR(op_data))
400                 GOTO(err_exit, err = PTR_ERR(op_data));
401
402         op_data->op_cli_flags |= CLI_SET_MEA;
403         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
404                         current_fsuid(), current_fsgid(),
405                         cfs_curproc_cap_pack(), 0, &request);
406         ll_finish_md_op_data(op_data);
407         if (err)
408                 GOTO(err_exit, err);
409 err_exit:
410         ptlrpc_req_finished(request);
411         return err;
412 }
413
414 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
415                      int set_default)
416 {
417         struct ll_sb_info *sbi = ll_i2sbi(inode);
418         struct md_op_data *op_data;
419         struct ptlrpc_request *req = NULL;
420         int rc = 0;
421         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
422         struct obd_device *mgc = lsi->lsi_mgc;
423         int lum_size;
424         ENTRY;
425
426         if (lump != NULL) {
427                 /*
428                  * This is coming from userspace, so should be in
429                  * local endian.  But the MDS would like it in little
430                  * endian, so we swab it before we send it.
431                  */
432                 switch (lump->lmm_magic) {
433                 case LOV_USER_MAGIC_V1: {
434                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
435                                 lustre_swab_lov_user_md_v1(lump);
436                         lum_size = sizeof(struct lov_user_md_v1);
437                         break;
438                 }
439                 case LOV_USER_MAGIC_V3: {
440                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
441                                 lustre_swab_lov_user_md_v3(
442                                         (struct lov_user_md_v3 *)lump);
443                         lum_size = sizeof(struct lov_user_md_v3);
444                         break;
445                 }
446                 default: {
447                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
448                                         " %#08x != %#08x nor %#08x\n",
449                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
450                                         LOV_USER_MAGIC_V3);
451                         RETURN(-EINVAL);
452                 }
453                 }
454         } else {
455                 lum_size = sizeof(struct lov_user_md_v1);
456         }
457
458         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
459                                      LUSTRE_OPC_ANY, NULL);
460         if (IS_ERR(op_data))
461                 RETURN(PTR_ERR(op_data));
462
463         /* swabbing is done in lov_setstripe() on server side */
464         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
465                         NULL, 0, &req, NULL);
466         ll_finish_md_op_data(op_data);
467         ptlrpc_req_finished(req);
468         if (rc) {
469                 if (rc != -EPERM && rc != -EACCES)
470                         CERROR("mdc_setattr fails: rc = %d\n", rc);
471         }
472
473         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
474          LOV_USER_MAGIC_V3 have the same initial fields so we do not
475          need the make the distiction between the 2 versions */
476         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
477                 char *param = NULL;
478                 char *buf;
479
480                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
481                 if (param == NULL)
482                         GOTO(end, rc = -ENOMEM);
483
484                 buf = param;
485                 /* Get fsname and assume devname to be -MDT0000. */
486                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
487                 strcat(buf, "-MDT0000.lov");
488                 buf += strlen(buf);
489
490                 /* Set root stripesize */
491                 sprintf(buf, ".stripesize=%u",
492                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
493                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
494                 if (rc)
495                         GOTO(end, rc);
496
497                 /* Set root stripecount */
498                 sprintf(buf, ".stripecount=%hd",
499                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
500                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
501                 if (rc)
502                         GOTO(end, rc);
503
504                 /* Set root stripeoffset */
505                 sprintf(buf, ".stripeoffset=%hd",
506                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
507                         (typeof(lump->lmm_stripe_offset))(-1));
508                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
509
510 end:
511                 if (param != NULL)
512                         OBD_FREE(param, MGS_PARAM_MAXLEN);
513         }
514         RETURN(rc);
515 }
516
517 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
518                      int *lmm_size, struct ptlrpc_request **request)
519 {
520         struct ll_sb_info *sbi = ll_i2sbi(inode);
521         struct mdt_body   *body;
522         struct lov_mds_md *lmm = NULL;
523         struct ptlrpc_request *req = NULL;
524         int rc, lmmsize;
525         struct md_op_data *op_data;
526
527         rc = ll_get_max_mdsize(sbi, &lmmsize);
528         if (rc)
529                 RETURN(rc);
530
531         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
532                                      0, lmmsize, LUSTRE_OPC_ANY,
533                                      NULL);
534         if (IS_ERR(op_data))
535                 RETURN(PTR_ERR(op_data));
536
537         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
538         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
539         ll_finish_md_op_data(op_data);
540         if (rc < 0) {
541                 CDEBUG(D_INFO, "md_getattr failed on inode "
542                        DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
543                 GOTO(out, rc);
544         }
545
546         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
547         LASSERT(body != NULL);
548
549         lmmsize = body->eadatasize;
550
551         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
552             lmmsize == 0) {
553                 GOTO(out, rc = -ENODATA);
554         }
555
556         lmm = req_capsule_server_sized_get(&req->rq_pill,
557                                            &RMF_MDT_MD, lmmsize);
558         LASSERT(lmm != NULL);
559
560         /*
561          * This is coming from the MDS, so is probably in
562          * little endian.  We convert it to host endian before
563          * passing it to userspace.
564          */
565         /* We don't swab objects for directories */
566         switch (le32_to_cpu(lmm->lmm_magic)) {
567         case LOV_MAGIC_V1:
568                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
569                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
570                 break;
571         case LOV_MAGIC_V3:
572                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
573                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
574                 break;
575         default:
576                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
577                 rc = -EPROTO;
578         }
579 out:
580         *lmmp = lmm;
581         *lmm_size = lmmsize;
582         *request = req;
583         return rc;
584 }
585
586 /*
587  *  Get MDT index for the inode.
588  */
589 int ll_get_mdt_idx(struct inode *inode)
590 {
591         struct ll_sb_info *sbi = ll_i2sbi(inode);
592         struct md_op_data *op_data;
593         int rc, mdtidx;
594         ENTRY;
595
596         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
597                                      0, LUSTRE_OPC_ANY, NULL);
598         if (IS_ERR(op_data))
599                 RETURN(PTR_ERR(op_data));
600
601         op_data->op_flags |= MF_GET_MDT_IDX;
602         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
603         mdtidx = op_data->op_mds;
604         ll_finish_md_op_data(op_data);
605         if (rc < 0) {
606                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
607                 RETURN(rc);
608         }
609         return mdtidx;
610 }
611
612 /**
613  * Generic handler to do any pre-copy work.
614  *
615  * It send a first hsm_progress (with extent length == 0) to coordinator as a
616  * first information for it that real work has started.
617  *
618  * Moreover, for a ARCHIVE request, it will sample the file data version and
619  * store it in \a copy.
620  *
621  * \return 0 on success.
622  */
623 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
624 {
625         struct ll_sb_info               *sbi = ll_s2sbi(sb);
626         struct hsm_progress_kernel       hpk;
627         int                              rc;
628         ENTRY;
629
630         /* Forge a hsm_progress based on data from copy. */
631         hpk.hpk_fid = copy->hc_hai.hai_fid;
632         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
633         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
634         hpk.hpk_extent.length = 0;
635         hpk.hpk_flags = 0;
636         hpk.hpk_errval = 0;
637         hpk.hpk_data_version = 0;
638
639
640         /* For archive request, we need to read the current file version. */
641         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
642                 struct inode    *inode;
643                 __u64            data_version = 0;
644
645                 /* Get inode for this fid */
646                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
647                 if (IS_ERR(inode)) {
648                         hpk.hpk_flags |= HP_FLAG_RETRY;
649                         /* hpk_errval is >= 0 */
650                         hpk.hpk_errval = -PTR_ERR(inode);
651                         GOTO(progress, rc = PTR_ERR(inode));
652                 }
653
654                 /* Read current file data version */
655                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
656                 iput(inode);
657                 if (rc != 0) {
658                         CDEBUG(D_HSM, "Could not read file data version of "
659                                       DFID" (rc = %d). Archive request ("
660                                       LPX64") could not be done.\n",
661                                       PFID(&copy->hc_hai.hai_fid), rc,
662                                       copy->hc_hai.hai_cookie);
663                         hpk.hpk_flags |= HP_FLAG_RETRY;
664                         /* hpk_errval must be >= 0 */
665                         hpk.hpk_errval = -rc;
666                         GOTO(progress, rc);
667                 }
668
669                 /* Store it the hsm_copy for later copytool use.
670                  * Always modified even if no lsm. */
671                 copy->hc_data_version = data_version;
672         }
673
674 progress:
675         /* On error, the request should be considered as completed */
676         if (hpk.hpk_errval > 0)
677                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
678         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
679                            &hpk, NULL);
680
681         RETURN(rc);
682 }
683
684 /**
685  * Generic handler to do any post-copy work.
686  *
687  * It will send the last hsm_progress update to coordinator to inform it
688  * that copy is finished and whether it was successful or not.
689  *
690  * Moreover,
691  * - for ARCHIVE request, it will sample the file data version and compare it
692  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
693  *   will be considered as failed.
694  * - for RESTORE request, it will sample the file data version and send it to
695  *   coordinator which is useful if the file was imported as 'released'.
696  *
697  * \return 0 on success.
698  */
699 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
700 {
701         struct ll_sb_info               *sbi = ll_s2sbi(sb);
702         struct hsm_progress_kernel       hpk;
703         int                              rc;
704         ENTRY;
705
706         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
707         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
708          * initialized if copy_end was called with copy == NULL.
709          */
710
711         /* Forge a hsm_progress based on data from copy. */
712         hpk.hpk_fid = copy->hc_hai.hai_fid;
713         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
714         hpk.hpk_extent = copy->hc_hai.hai_extent;
715         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
716         hpk.hpk_errval = copy->hc_errval;
717         hpk.hpk_data_version = 0;
718
719         /* For archive request, we need to check the file data was not changed.
720          *
721          * For restore request, we need to send the file data version, this is
722          * useful when the file was created using hsm_import.
723          */
724         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
725              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
726             (copy->hc_errval == 0)) {
727                 struct inode    *inode;
728                 __u64            data_version = 0;
729
730                 /* Get lsm for this fid */
731                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
732                 if (IS_ERR(inode)) {
733                         hpk.hpk_flags |= HP_FLAG_RETRY;
734                         /* hpk_errval must be >= 0 */
735                         hpk.hpk_errval = -PTR_ERR(inode);
736                         GOTO(progress, rc = PTR_ERR(inode));
737                 }
738
739                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
740                 iput(inode);
741                 if (rc) {
742                         CDEBUG(D_HSM, "Could not read file data version. "
743                                       "Request could not be confirmed.\n");
744                         if (hpk.hpk_errval == 0)
745                                 hpk.hpk_errval = -rc;
746                         GOTO(progress, rc);
747                 }
748
749                 /* Store it the hsm_copy for later copytool use.
750                  * Always modified even if no lsm. */
751                 hpk.hpk_data_version = data_version;
752
753                 /* File could have been stripped during archiving, so we need
754                  * to check anyway. */
755                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
756                     (copy->hc_data_version != data_version)) {
757                         CDEBUG(D_HSM, "File data version mismatched. "
758                               "File content was changed during archiving. "
759                                DFID", start:"LPX64" current:"LPX64"\n",
760                                PFID(&copy->hc_hai.hai_fid),
761                                copy->hc_data_version, data_version);
762                         /* File was changed, send error to cdt. Do not ask for
763                          * retry because if a file is modified frequently,
764                          * the cdt will loop on retried archive requests.
765                          * The policy engine will ask for a new archive later
766                          * when the file will not be modified for some tunable
767                          * time */
768                         /* we do not notify caller */
769                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
770                         /* hpk_errval must be >= 0 */
771                         hpk.hpk_errval = EBUSY;
772                 }
773
774         }
775
776 progress:
777         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
778                            &hpk, NULL);
779
780         RETURN(rc);
781 }
782
783
784 static int copy_and_ioctl(int cmd, struct obd_export *exp,
785                           const void __user *data, size_t size)
786 {
787         void *copy;
788         int rc;
789
790         OBD_ALLOC(copy, size);
791         if (copy == NULL)
792                 return -ENOMEM;
793
794         if (copy_from_user(copy, data, size)) {
795                 rc = -EFAULT;
796                 goto out;
797         }
798
799         rc = obd_iocontrol(cmd, exp, size, copy, NULL);
800 out:
801         OBD_FREE(copy, size);
802
803         return rc;
804 }
805
806 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
807 {
808         int cmd = qctl->qc_cmd;
809         int type = qctl->qc_type;
810         int id = qctl->qc_id;
811         int valid = qctl->qc_valid;
812         int rc = 0;
813         ENTRY;
814
815         switch (cmd) {
816         case LUSTRE_Q_INVALIDATE:
817         case LUSTRE_Q_FINVALIDATE:
818         case Q_QUOTAON:
819         case Q_QUOTAOFF:
820         case Q_SETQUOTA:
821         case Q_SETINFO:
822                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
823                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
824                         RETURN(-EPERM);
825                 break;
826         case Q_GETQUOTA:
827                 if (((type == USRQUOTA && current_euid() != id) ||
828                      (type == GRPQUOTA && !in_egroup_p(id))) &&
829                     (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
830                      sbi->ll_flags & LL_SBI_RMT_CLIENT))
831                         RETURN(-EPERM);
832                 break;
833         case Q_GETINFO:
834                 break;
835         default:
836                 CERROR("unsupported quotactl op: %#x\n", cmd);
837                 RETURN(-ENOTTY);
838         }
839
840         if (valid != QC_GENERAL) {
841                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
842                         RETURN(-EOPNOTSUPP);
843
844                 if (cmd == Q_GETINFO)
845                         qctl->qc_cmd = Q_GETOINFO;
846                 else if (cmd == Q_GETQUOTA)
847                         qctl->qc_cmd = Q_GETOQUOTA;
848                 else
849                         RETURN(-EINVAL);
850
851                 switch (valid) {
852                 case QC_MDTIDX:
853                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
854                                            sizeof(*qctl), qctl, NULL);
855                         break;
856                 case QC_OSTIDX:
857                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
858                                            sizeof(*qctl), qctl, NULL);
859                         break;
860                 case QC_UUID:
861                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
862                                            sizeof(*qctl), qctl, NULL);
863                         if (rc == -EAGAIN)
864                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
865                                                    sbi->ll_dt_exp,
866                                                    sizeof(*qctl), qctl, NULL);
867                         break;
868                 default:
869                         rc = -EINVAL;
870                         break;
871                 }
872
873                 if (rc)
874                         RETURN(rc);
875
876                 qctl->qc_cmd = cmd;
877         } else {
878                 struct obd_quotactl *oqctl;
879
880                 OBD_ALLOC_PTR(oqctl);
881                 if (oqctl == NULL)
882                         RETURN(-ENOMEM);
883
884                 QCTL_COPY(oqctl, qctl);
885                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
886                 if (rc) {
887                         if (rc != -EALREADY && cmd == Q_QUOTAON) {
888                                 oqctl->qc_cmd = Q_QUOTAOFF;
889                                 obd_quotactl(sbi->ll_md_exp, oqctl);
890                         }
891                         OBD_FREE_PTR(oqctl);
892                         RETURN(rc);
893                 }
894                 /* If QIF_SPACE is not set, client should collect the
895                  * space usage from OSSs by itself */
896                 if (cmd == Q_GETQUOTA &&
897                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
898                     !oqctl->qc_dqblk.dqb_curspace) {
899                         struct obd_quotactl *oqctl_tmp;
900
901                         OBD_ALLOC_PTR(oqctl_tmp);
902                         if (oqctl_tmp == NULL)
903                                 GOTO(out, rc = -ENOMEM);
904
905                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
906                         oqctl_tmp->qc_id = oqctl->qc_id;
907                         oqctl_tmp->qc_type = oqctl->qc_type;
908
909                         /* collect space usage from OSTs */
910                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
911                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
912                         if (!rc || rc == -EREMOTEIO) {
913                                 oqctl->qc_dqblk.dqb_curspace =
914                                         oqctl_tmp->qc_dqblk.dqb_curspace;
915                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
916                         }
917
918                         /* collect space & inode usage from MDTs */
919                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
920                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
921                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
922                         if (!rc || rc == -EREMOTEIO) {
923                                 oqctl->qc_dqblk.dqb_curspace +=
924                                         oqctl_tmp->qc_dqblk.dqb_curspace;
925                                 oqctl->qc_dqblk.dqb_curinodes =
926                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
927                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
928                         } else {
929                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
930                         }
931
932                         OBD_FREE_PTR(oqctl_tmp);
933                 }
934 out:
935                 QCTL_COPY(qctl, oqctl);
936                 OBD_FREE_PTR(oqctl);
937         }
938
939         RETURN(rc);
940 }
941
942 static char *
943 ll_getname(const char __user *filename)
944 {
945         int ret = 0, len;
946         char *tmp = __getname();
947
948         if (!tmp)
949                 return ERR_PTR(-ENOMEM);
950
951         len = strncpy_from_user(tmp, filename, PATH_MAX);
952         if (len == 0)
953                 ret = -ENOENT;
954         else if (len > PATH_MAX)
955                 ret = -ENAMETOOLONG;
956
957         if (ret) {
958                 __putname(tmp);
959                 tmp =  ERR_PTR(ret);
960         }
961         return tmp;
962 }
963
964 #define ll_putname(filename) __putname(filename)
965
966 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
967 {
968         struct inode *inode = file->f_dentry->d_inode;
969         struct ll_sb_info *sbi = ll_i2sbi(inode);
970         struct obd_ioctl_data *data;
971         int rc = 0;
972         ENTRY;
973
974         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
975                PFID(ll_inode2fid(inode)), inode, cmd);
976
977         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
978         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
979                 return -ENOTTY;
980
981         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
982         switch(cmd) {
983         case FSFILT_IOC_GETFLAGS:
984         case FSFILT_IOC_SETFLAGS:
985                 RETURN(ll_iocontrol(inode, file, cmd, arg));
986         case FSFILT_IOC_GETVERSION_OLD:
987         case FSFILT_IOC_GETVERSION:
988                 RETURN(put_user(inode->i_generation, (int *)arg));
989         /* We need to special case any other ioctls we want to handle,
990          * to send them to the MDS/OST as appropriate and to properly
991          * network encode the arg field.
992         case FSFILT_IOC_SETVERSION_OLD:
993         case FSFILT_IOC_SETVERSION:
994         */
995         case LL_IOC_GET_MDTIDX: {
996                 int mdtidx;
997
998                 mdtidx = ll_get_mdt_idx(inode);
999                 if (mdtidx < 0)
1000                         RETURN(mdtidx);
1001
1002                 if (put_user((int)mdtidx, (int*)arg))
1003                         RETURN(-EFAULT);
1004
1005                 return 0;
1006         }
1007         case IOC_MDC_LOOKUP: {
1008                 struct ptlrpc_request *request = NULL;
1009                 int namelen, len = 0;
1010                 char *buf = NULL;
1011                 char *filename;
1012                 struct md_op_data *op_data;
1013
1014                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1015                 if (rc)
1016                         RETURN(rc);
1017                 data = (void *)buf;
1018
1019                 filename = data->ioc_inlbuf1;
1020                 namelen = strlen(filename);
1021
1022                 if (namelen < 1) {
1023                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1024                         GOTO(out_free, rc = -EINVAL);
1025                 }
1026
1027                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1028                                              0, LUSTRE_OPC_ANY, NULL);
1029                 if (IS_ERR(op_data))
1030                         GOTO(out_free, rc = PTR_ERR(op_data));
1031
1032                 op_data->op_valid = OBD_MD_FLID;
1033                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1034                 ll_finish_md_op_data(op_data);
1035                 if (rc < 0) {
1036                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1037                         GOTO(out_free, rc);
1038                 }
1039                 ptlrpc_req_finished(request);
1040                 EXIT;
1041 out_free:
1042                 obd_ioctl_freedata(buf, len);
1043                 return rc;
1044         }
1045         case LL_IOC_LMV_SETSTRIPE: {
1046                 struct lmv_user_md  *lum;
1047                 char            *buf = NULL;
1048                 char            *filename;
1049                 int              namelen = 0;
1050                 int              lumlen = 0;
1051                 int              len;
1052                 int              rc;
1053
1054                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1055                 if (rc)
1056                         RETURN(rc);
1057
1058                 data = (void *)buf;
1059                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1060                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1061                         GOTO(lmv_out_free, rc = -EINVAL);
1062
1063                 filename = data->ioc_inlbuf1;
1064                 namelen = data->ioc_inllen1;
1065
1066                 if (namelen < 1) {
1067                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1068                         GOTO(lmv_out_free, rc = -EINVAL);
1069                 }
1070                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1071                 lumlen = data->ioc_inllen2;
1072
1073                 if (lum->lum_magic != LMV_USER_MAGIC ||
1074                     lumlen != sizeof(*lum)) {
1075                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1076                                filename, lum->lum_magic, lumlen, -EFAULT);
1077                         GOTO(lmv_out_free, rc = -EINVAL);
1078                 }
1079
1080                 /**
1081                  * ll_dir_setdirstripe will be used to set dir stripe
1082                  *  mdc_create--->mdt_reint_create (with dirstripe)
1083                  */
1084                 rc = ll_dir_setdirstripe(inode, lum, filename);
1085 lmv_out_free:
1086                 obd_ioctl_freedata(buf, len);
1087                 RETURN(rc);
1088
1089         }
1090         case LL_IOC_LOV_SETSTRIPE: {
1091                 struct lov_user_md_v3 lumv3;
1092                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1093                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1094                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1095
1096                 int set_default = 0;
1097
1098                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1099                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1100                         sizeof(lumv3p->lmm_objects[0]));
1101                 /* first try with v1 which is smaller than v3 */
1102                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1103                         RETURN(-EFAULT);
1104
1105                 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1106                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1107                                 RETURN(-EFAULT);
1108                 }
1109
1110                 if (inode->i_sb->s_root == file->f_dentry)
1111                         set_default = 1;
1112
1113                 /* in v1 and v3 cases lumv1 points to data */
1114                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1115
1116                 RETURN(rc);
1117         }
1118         case LL_IOC_LMV_GETSTRIPE: {
1119                 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1120                 struct lmv_user_md lum;
1121                 struct lmv_user_md *tmp;
1122                 int lum_size;
1123                 int rc = 0;
1124                 int mdtindex;
1125
1126                 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1127                         RETURN(-EFAULT);
1128
1129                 if (lum.lum_magic != LMV_MAGIC_V1)
1130                         RETURN(-EINVAL);
1131
1132                 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1133                 OBD_ALLOC(tmp, lum_size);
1134                 if (tmp == NULL)
1135                         GOTO(free_lmv, rc = -ENOMEM);
1136
1137                 memcpy(tmp, &lum, sizeof(lum));
1138                 tmp->lum_stripe_count = 1;
1139                 mdtindex = ll_get_mdt_idx(inode);
1140                 if (mdtindex < 0)
1141                         GOTO(free_lmv, rc = -ENOMEM);
1142
1143                 tmp->lum_stripe_offset = mdtindex;
1144                 tmp->lum_objects[0].lum_mds = mdtindex;
1145                 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1146                        sizeof(struct lu_fid));
1147                 if (copy_to_user((void *)arg, tmp, lum_size))
1148                         GOTO(free_lmv, rc = -EFAULT);
1149 free_lmv:
1150                 if (tmp)
1151                         OBD_FREE(tmp, lum_size);
1152                 RETURN(rc);
1153         }
1154         case LL_IOC_REMOVE_ENTRY: {
1155                 char            *filename = NULL;
1156                 int              namelen = 0;
1157                 int              rc;
1158
1159                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1160                  * unsupported server, which might crash the server(LU-2730),
1161                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1162                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1163                  * server will support REINT_RMENTRY XXX*/
1164                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1165                         RETURN(-ENOTSUPP);
1166
1167                 filename = ll_getname((const char *)arg);
1168                 if (IS_ERR(filename))
1169                         RETURN(PTR_ERR(filename));
1170
1171                 namelen = strlen(filename);
1172                 if (namelen < 1)
1173                         GOTO(out_rmdir, rc = -EINVAL);
1174
1175                 rc = ll_rmdir_entry(inode, filename, namelen);
1176 out_rmdir:
1177                 if (filename)
1178                         ll_putname(filename);
1179                 RETURN(rc);
1180         }
1181         case LL_IOC_LOV_SWAP_LAYOUTS:
1182                 RETURN(-EPERM);
1183         case LL_IOC_OBD_STATFS:
1184                 RETURN(ll_obd_statfs(inode, (void *)arg));
1185         case LL_IOC_LOV_GETSTRIPE:
1186         case LL_IOC_MDC_GETINFO:
1187         case IOC_MDC_GETFILEINFO:
1188         case IOC_MDC_GETFILESTRIPE: {
1189                 struct ptlrpc_request *request = NULL;
1190                 struct lov_user_md *lump;
1191                 struct lov_mds_md *lmm = NULL;
1192                 struct mdt_body *body;
1193                 char *filename = NULL;
1194                 int lmmsize;
1195
1196                 if (cmd == IOC_MDC_GETFILEINFO ||
1197                     cmd == IOC_MDC_GETFILESTRIPE) {
1198                         filename = ll_getname((const char *)arg);
1199                         if (IS_ERR(filename))
1200                                 RETURN(PTR_ERR(filename));
1201
1202                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1203                                                       &lmmsize, &request);
1204                 } else {
1205                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1206                 }
1207
1208                 if (request) {
1209                         body = req_capsule_server_get(&request->rq_pill,
1210                                                       &RMF_MDT_BODY);
1211                         LASSERT(body != NULL);
1212                 } else {
1213                         GOTO(out_req, rc);
1214                 }
1215
1216                 if (rc < 0) {
1217                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1218                                                cmd == LL_IOC_MDC_GETINFO))
1219                                 GOTO(skip_lmm, rc = 0);
1220                         else
1221                                 GOTO(out_req, rc);
1222                 }
1223
1224                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1225                     cmd == LL_IOC_LOV_GETSTRIPE) {
1226                         lump = (struct lov_user_md *)arg;
1227                 } else {
1228                         struct lov_user_mds_data *lmdp;
1229                         lmdp = (struct lov_user_mds_data *)arg;
1230                         lump = &lmdp->lmd_lmm;
1231                 }
1232                 if (copy_to_user(lump, lmm, lmmsize)) {
1233                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1234                                 GOTO(out_req, rc = -EFAULT);
1235                         rc = -EOVERFLOW;
1236                 }
1237         skip_lmm:
1238                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1239                         struct lov_user_mds_data *lmdp;
1240                         lstat_t st = { 0 };
1241
1242                         st.st_dev     = inode->i_sb->s_dev;
1243                         st.st_mode    = body->mode;
1244                         st.st_nlink   = body->nlink;
1245                         st.st_uid     = body->uid;
1246                         st.st_gid     = body->gid;
1247                         st.st_rdev    = body->rdev;
1248                         st.st_size    = body->size;
1249                         st.st_blksize = PAGE_CACHE_SIZE;
1250                         st.st_blocks  = body->blocks;
1251                         st.st_atime   = body->atime;
1252                         st.st_mtime   = body->mtime;
1253                         st.st_ctime   = body->ctime;
1254                         st.st_ino     = inode->i_ino;
1255
1256                         lmdp = (struct lov_user_mds_data *)arg;
1257                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1258                                 GOTO(out_req, rc = -EFAULT);
1259                 }
1260
1261                 EXIT;
1262         out_req:
1263                 ptlrpc_req_finished(request);
1264                 if (filename)
1265                         ll_putname(filename);
1266                 return rc;
1267         }
1268         case IOC_LOV_GETINFO: {
1269                 struct lov_user_mds_data *lumd;
1270                 struct lov_stripe_md *lsm;
1271                 struct lov_user_md *lum;
1272                 struct lov_mds_md *lmm;
1273                 int lmmsize;
1274                 lstat_t st;
1275
1276                 lumd = (struct lov_user_mds_data *)arg;
1277                 lum = &lumd->lmd_lmm;
1278
1279                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1280                 if (rc)
1281                         RETURN(rc);
1282
1283                 OBD_ALLOC_LARGE(lmm, lmmsize);
1284                 if (lmm == NULL)
1285                         RETURN(-ENOMEM);
1286
1287                 if (copy_from_user(lmm, lum, lmmsize))
1288                         GOTO(free_lmm, rc = -EFAULT);
1289
1290                 switch (lmm->lmm_magic) {
1291                 case LOV_USER_MAGIC_V1:
1292                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1293                                 break;
1294                         /* swab objects first so that stripes num will be sane */
1295                         lustre_swab_lov_user_md_objects(
1296                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1297                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1298                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1299                         break;
1300                 case LOV_USER_MAGIC_V3:
1301                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1302                                 break;
1303                         /* swab objects first so that stripes num will be sane */
1304                         lustre_swab_lov_user_md_objects(
1305                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1306                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1307                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1308                         break;
1309                 default:
1310                         GOTO(free_lmm, rc = -EINVAL);
1311                 }
1312
1313                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1314                 if (rc < 0)
1315                         GOTO(free_lmm, rc = -ENOMEM);
1316
1317                 /* Perform glimpse_size operation. */
1318                 memset(&st, 0, sizeof(st));
1319
1320                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1321                 if (rc)
1322                         GOTO(free_lsm, rc);
1323
1324                 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1325                         GOTO(free_lsm, rc = -EFAULT);
1326
1327                 EXIT;
1328         free_lsm:
1329                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1330         free_lmm:
1331                 OBD_FREE_LARGE(lmm, lmmsize);
1332                 return rc;
1333         }
1334         case OBD_IOC_LLOG_CATINFO: {
1335                 RETURN(-EOPNOTSUPP);
1336         }
1337         case OBD_IOC_QUOTACHECK: {
1338                 struct obd_quotactl *oqctl;
1339                 int error = 0;
1340
1341                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1342                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1343                         RETURN(-EPERM);
1344
1345                 OBD_ALLOC_PTR(oqctl);
1346                 if (!oqctl)
1347                         RETURN(-ENOMEM);
1348                 oqctl->qc_type = arg;
1349                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1350                 if (rc < 0) {
1351                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1352                         error = rc;
1353                 }
1354
1355                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1356                 if (rc < 0)
1357                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1358
1359                 OBD_FREE_PTR(oqctl);
1360                 return error ?: rc;
1361         }
1362         case OBD_IOC_POLL_QUOTACHECK: {
1363                 struct if_quotacheck *check;
1364
1365                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1366                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1367                         RETURN(-EPERM);
1368
1369                 OBD_ALLOC_PTR(check);
1370                 if (!check)
1371                         RETURN(-ENOMEM);
1372
1373                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1374                                    NULL);
1375                 if (rc) {
1376                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1377                         if (copy_to_user((void *)arg, check,
1378                                              sizeof(*check)))
1379                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1380                         GOTO(out_poll, rc);
1381                 }
1382
1383                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1384                                    NULL);
1385                 if (rc) {
1386                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1387                         if (copy_to_user((void *)arg, check,
1388                                              sizeof(*check)))
1389                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1390                         GOTO(out_poll, rc);
1391                 }
1392         out_poll:
1393                 OBD_FREE_PTR(check);
1394                 RETURN(rc);
1395         }
1396 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1397         case LL_IOC_QUOTACTL_18: {
1398                 /* copy the old 1.x quota struct for internal use, then copy
1399                  * back into old format struct.  For 1.8 compatibility. */
1400                 struct if_quotactl_18 *qctl_18;
1401                 struct if_quotactl *qctl_20;
1402
1403                 OBD_ALLOC_PTR(qctl_18);
1404                 if (!qctl_18)
1405                         RETURN(-ENOMEM);
1406
1407                 OBD_ALLOC_PTR(qctl_20);
1408                 if (!qctl_20)
1409                         GOTO(out_quotactl_18, rc = -ENOMEM);
1410
1411                 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1412                         GOTO(out_quotactl_20, rc = -ENOMEM);
1413
1414                 QCTL_COPY(qctl_20, qctl_18);
1415                 qctl_20->qc_idx = 0;
1416
1417                 /* XXX: dqb_valid was borrowed as a flag to mark that
1418                  *      only mds quota is wanted */
1419                 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1420                     qctl_18->qc_dqblk.dqb_valid) {
1421                         qctl_20->qc_valid = QC_MDTIDX;
1422                         qctl_20->qc_dqblk.dqb_valid = 0;
1423                 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1424                         qctl_20->qc_valid = QC_UUID;
1425                         qctl_20->obd_uuid = qctl_18->obd_uuid;
1426                 } else {
1427                         qctl_20->qc_valid = QC_GENERAL;
1428                 }
1429
1430                 rc = quotactl_ioctl(sbi, qctl_20);
1431
1432                 if (rc == 0) {
1433                         QCTL_COPY(qctl_18, qctl_20);
1434                         qctl_18->obd_uuid = qctl_20->obd_uuid;
1435
1436                         if (copy_to_user((void *)arg, qctl_18,
1437                                              sizeof(*qctl_18)))
1438                                 rc = -EFAULT;
1439                 }
1440
1441         out_quotactl_20:
1442                 OBD_FREE_PTR(qctl_20);
1443         out_quotactl_18:
1444                 OBD_FREE_PTR(qctl_18);
1445                 RETURN(rc);
1446         }
1447 #else
1448 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1449 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1450         case LL_IOC_QUOTACTL: {
1451                 struct if_quotactl *qctl;
1452
1453                 OBD_ALLOC_PTR(qctl);
1454                 if (!qctl)
1455                         RETURN(-ENOMEM);
1456
1457                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1458                         GOTO(out_quotactl, rc = -EFAULT);
1459
1460                 rc = quotactl_ioctl(sbi, qctl);
1461
1462                 if (rc == 0 && copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1463                         rc = -EFAULT;
1464
1465         out_quotactl:
1466                 OBD_FREE_PTR(qctl);
1467                 RETURN(rc);
1468         }
1469         case OBD_IOC_GETDTNAME:
1470         case OBD_IOC_GETMDNAME:
1471                 RETURN(ll_get_obd_name(inode, cmd, arg));
1472         case LL_IOC_FLUSHCTX:
1473                 RETURN(ll_flush_ctx(inode));
1474 #ifdef CONFIG_FS_POSIX_ACL
1475         case LL_IOC_RMTACL: {
1476             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1477                 inode == inode->i_sb->s_root->d_inode) {
1478                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1479
1480                 LASSERT(fd != NULL);
1481                 rc = rct_add(&sbi->ll_rct, current_pid(), arg);
1482                 if (!rc)
1483                         fd->fd_flags |= LL_FILE_RMTACL;
1484                 RETURN(rc);
1485             } else
1486                 RETURN(0);
1487         }
1488 #endif
1489         case LL_IOC_GETOBDCOUNT: {
1490                 int count, vallen;
1491                 struct obd_export *exp;
1492
1493                 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1494                         RETURN(-EFAULT);
1495
1496                 /* get ost count when count is zero, get mdt count otherwise */
1497                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1498                 vallen = sizeof(count);
1499                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1500                                   KEY_TGT_COUNT, &vallen, &count, NULL);
1501                 if (rc) {
1502                         CERROR("get target count failed: %d\n", rc);
1503                         RETURN(rc);
1504                 }
1505
1506                 if (copy_to_user((int *)arg, &count, sizeof(int)))
1507                         RETURN(-EFAULT);
1508
1509                 RETURN(0);
1510         }
1511         case LL_IOC_PATH2FID:
1512                 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1513                                      sizeof(struct lu_fid)))
1514                         RETURN(-EFAULT);
1515                 RETURN(0);
1516         case LL_IOC_GET_CONNECT_FLAGS: {
1517                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1518         }
1519         case OBD_IOC_CHANGELOG_SEND:
1520         case OBD_IOC_CHANGELOG_CLEAR:
1521                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1522                                     sizeof(struct ioc_changelog));
1523                 RETURN(rc);
1524         case OBD_IOC_FID2PATH:
1525                 RETURN(ll_fid2path(inode, (void *)arg));
1526         case LL_IOC_HSM_REQUEST: {
1527                 struct hsm_user_request *hur;
1528                 int                      totalsize;
1529
1530                 OBD_ALLOC_PTR(hur);
1531                 if (hur == NULL)
1532                         RETURN(-ENOMEM);
1533
1534                 /* We don't know the true size yet; copy the fixed-size part */
1535                 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1536                         OBD_FREE_PTR(hur);
1537                         RETURN(-EFAULT);
1538                 }
1539
1540                 /* Compute the whole struct size */
1541                 totalsize = hur_len(hur);
1542                 OBD_FREE_PTR(hur);
1543
1544                 /* Make sure the size is reasonable */
1545                 if (totalsize >= MDS_MAXREQSIZE)
1546                         RETURN(-E2BIG);
1547
1548                 OBD_ALLOC_LARGE(hur, totalsize);
1549                 if (hur == NULL)
1550                         RETURN(-ENOMEM);
1551
1552                 /* Copy the whole struct */
1553                 if (copy_from_user(hur, (void *)arg, totalsize)) {
1554                         OBD_FREE_LARGE(hur, totalsize);
1555                         RETURN(-EFAULT);
1556                 }
1557
1558                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1559                         const struct lu_fid *fid;
1560                         struct inode *f;
1561                         int i;
1562
1563                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1564                                 fid = &hur->hur_user_item[i].hui_fid;
1565                                 f = search_inode_for_lustre(inode->i_sb, fid);
1566                                 if (IS_ERR(f)) {
1567                                         rc = PTR_ERR(f);
1568                                         break;
1569                                 }
1570
1571                                 rc = ll_hsm_release(f);
1572                                 iput(f);
1573                                 if (rc != 0)
1574                                         break;
1575                         }
1576                 } else {
1577                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1578                                            hur, NULL);
1579                 }
1580
1581                 OBD_FREE_LARGE(hur, totalsize);
1582
1583                 RETURN(rc);
1584         }
1585         case LL_IOC_HSM_PROGRESS: {
1586                 struct hsm_progress_kernel      hpk;
1587                 struct hsm_progress             hp;
1588
1589                 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1590                         RETURN(-EFAULT);
1591
1592                 hpk.hpk_fid = hp.hp_fid;
1593                 hpk.hpk_cookie = hp.hp_cookie;
1594                 hpk.hpk_extent = hp.hp_extent;
1595                 hpk.hpk_flags = hp.hp_flags;
1596                 hpk.hpk_errval = hp.hp_errval;
1597                 hpk.hpk_data_version = 0;
1598
1599                 /* File may not exist in Lustre; all progress
1600                  * reported to Lustre root */
1601                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1602                                    NULL);
1603                 RETURN(rc);
1604         }
1605         case LL_IOC_HSM_CT_START:
1606                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1607                         RETURN(-EPERM);
1608
1609                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1610                                     sizeof(struct lustre_kernelcomm));
1611                 RETURN(rc);
1612
1613         case LL_IOC_HSM_COPY_START: {
1614                 struct hsm_copy *copy;
1615                 int              rc;
1616
1617                 OBD_ALLOC_PTR(copy);
1618                 if (copy == NULL)
1619                         RETURN(-ENOMEM);
1620                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1621                         OBD_FREE_PTR(copy);
1622                         RETURN(-EFAULT);
1623                 }
1624
1625                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1626                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1627                         rc = -EFAULT;
1628
1629                 OBD_FREE_PTR(copy);
1630                 RETURN(rc);
1631         }
1632         case LL_IOC_HSM_COPY_END: {
1633                 struct hsm_copy *copy;
1634                 int              rc;
1635
1636                 OBD_ALLOC_PTR(copy);
1637                 if (copy == NULL)
1638                         RETURN(-ENOMEM);
1639                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1640                         OBD_FREE_PTR(copy);
1641                         RETURN(-EFAULT);
1642                 }
1643
1644                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1645                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1646                         rc = -EFAULT;
1647
1648                 OBD_FREE_PTR(copy);
1649                 RETURN(rc);
1650         }
1651         default:
1652                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1653                                      (void *)arg));
1654         }
1655 }
1656
1657 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1658 {
1659         struct inode *inode = file->f_mapping->host;
1660         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1661         struct ll_sb_info *sbi = ll_i2sbi(inode);
1662         int api32 = ll_need_32bit_api(sbi);
1663         loff_t ret = -EINVAL;
1664         ENTRY;
1665
1666         mutex_lock(&inode->i_mutex);
1667         switch (origin) {
1668                 case SEEK_SET:
1669                         break;
1670                 case SEEK_CUR:
1671                         offset += file->f_pos;
1672                         break;
1673                 case SEEK_END:
1674                         if (offset > 0)
1675                                 GOTO(out, ret);
1676                         if (api32)
1677                                 offset += LL_DIR_END_OFF_32BIT;
1678                         else
1679                                 offset += LL_DIR_END_OFF;
1680                         break;
1681                 default:
1682                         GOTO(out, ret);
1683         }
1684
1685         if (offset >= 0 &&
1686             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1687              (!api32 && offset <= LL_DIR_END_OFF))) {
1688                 if (offset != file->f_pos) {
1689                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1690                             (!api32 && offset == LL_DIR_END_OFF))
1691                                 fd->lfd_pos = MDS_DIR_END_OFF;
1692                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1693                                 fd->lfd_pos = offset << 32;
1694                         else
1695                                 fd->lfd_pos = offset;
1696                         file->f_pos = offset;
1697                         file->f_version = 0;
1698                 }
1699                 ret = offset;
1700         }
1701         GOTO(out, ret);
1702
1703 out:
1704         mutex_unlock(&inode->i_mutex);
1705         return ret;
1706 }
1707
1708 int ll_dir_open(struct inode *inode, struct file *file)
1709 {
1710         ENTRY;
1711         RETURN(ll_file_open(inode, file));
1712 }
1713
1714 int ll_dir_release(struct inode *inode, struct file *file)
1715 {
1716         ENTRY;
1717         RETURN(ll_file_release(inode, file));
1718 }
1719
1720 struct file_operations ll_dir_operations = {
1721         .llseek   = ll_dir_seek,
1722         .open     = ll_dir_open,
1723         .release  = ll_dir_release,
1724         .read     = generic_read_dir,
1725         .readdir  = ll_readdir,
1726         .unlocked_ioctl   = ll_dir_ioctl,
1727         .fsync    = ll_fsync,
1728 };