Whamcloud - gitweb
LU-3531 mdc: release dir page cache after accessing
[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 int ll_dir_setdirstripe(struct inode *dir, struct lmv_user_md *lump,
365                         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
373         ENTRY;
374
375         mode = (0755 & (S_IRWXUGO|S_ISVTX) & ~current->fs->umask) | S_IFDIR;
376         op_data = ll_prep_md_op_data(NULL, dir, NULL, filename,
377                                      strlen(filename), mode, LUSTRE_OPC_MKDIR,
378                                      lump);
379         if (IS_ERR(op_data))
380                 GOTO(err_exit, err = PTR_ERR(op_data));
381
382         op_data->op_cli_flags |= CLI_SET_MEA;
383         err = md_create(sbi->ll_md_exp, op_data, lump, sizeof(*lump), mode,
384                         current_fsuid(), current_fsgid(),
385                         cfs_curproc_cap_pack(), 0, &request);
386         ll_finish_md_op_data(op_data);
387         if (err)
388                 GOTO(err_exit, err);
389 err_exit:
390         ptlrpc_req_finished(request);
391         return err;
392 }
393
394 int ll_dir_setstripe(struct inode *inode, struct lov_user_md *lump,
395                      int set_default)
396 {
397         struct ll_sb_info *sbi = ll_i2sbi(inode);
398         struct md_op_data *op_data;
399         struct ptlrpc_request *req = NULL;
400         int rc = 0;
401         struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
402         struct obd_device *mgc = lsi->lsi_mgc;
403         int lum_size;
404         ENTRY;
405
406         if (lump != NULL) {
407                 /*
408                  * This is coming from userspace, so should be in
409                  * local endian.  But the MDS would like it in little
410                  * endian, so we swab it before we send it.
411                  */
412                 switch (lump->lmm_magic) {
413                 case LOV_USER_MAGIC_V1: {
414                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V1))
415                                 lustre_swab_lov_user_md_v1(lump);
416                         lum_size = sizeof(struct lov_user_md_v1);
417                         break;
418                 }
419                 case LOV_USER_MAGIC_V3: {
420                         if (lump->lmm_magic != cpu_to_le32(LOV_USER_MAGIC_V3))
421                                 lustre_swab_lov_user_md_v3(
422                                         (struct lov_user_md_v3 *)lump);
423                         lum_size = sizeof(struct lov_user_md_v3);
424                         break;
425                 }
426                 default: {
427                         CDEBUG(D_IOCTL, "bad userland LOV MAGIC:"
428                                         " %#08x != %#08x nor %#08x\n",
429                                         lump->lmm_magic, LOV_USER_MAGIC_V1,
430                                         LOV_USER_MAGIC_V3);
431                         RETURN(-EINVAL);
432                 }
433                 }
434         } else {
435                 lum_size = sizeof(struct lov_user_md_v1);
436         }
437
438         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0,
439                                      LUSTRE_OPC_ANY, NULL);
440         if (IS_ERR(op_data))
441                 RETURN(PTR_ERR(op_data));
442
443         if (lump != NULL && lump->lmm_magic == cpu_to_le32(LMV_USER_MAGIC))
444                 op_data->op_cli_flags |= CLI_SET_MEA;
445
446         /* swabbing is done in lov_setstripe() on server side */
447         rc = md_setattr(sbi->ll_md_exp, op_data, lump, lum_size,
448                         NULL, 0, &req, NULL);
449         ll_finish_md_op_data(op_data);
450         ptlrpc_req_finished(req);
451         if (rc) {
452                 if (rc != -EPERM && rc != -EACCES)
453                         CERROR("mdc_setattr fails: rc = %d\n", rc);
454         }
455
456         /* In the following we use the fact that LOV_USER_MAGIC_V1 and
457          LOV_USER_MAGIC_V3 have the same initial fields so we do not
458          need the make the distiction between the 2 versions */
459         if (set_default && mgc->u.cli.cl_mgc_mgsexp) {
460                 char *param = NULL;
461                 char *buf;
462
463                 OBD_ALLOC(param, MGS_PARAM_MAXLEN);
464                 if (param == NULL)
465                         GOTO(end, rc = -ENOMEM);
466
467                 buf = param;
468                 /* Get fsname and assume devname to be -MDT0000. */
469                 ll_get_fsname(inode->i_sb, buf, MTI_NAME_MAXLEN);
470                 strcat(buf, "-MDT0000.lov");
471                 buf += strlen(buf);
472
473                 /* Set root stripesize */
474                 sprintf(buf, ".stripesize=%u",
475                         lump ? le32_to_cpu(lump->lmm_stripe_size) : 0);
476                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
477                 if (rc)
478                         GOTO(end, rc);
479
480                 /* Set root stripecount */
481                 sprintf(buf, ".stripecount=%hd",
482                         lump ? le16_to_cpu(lump->lmm_stripe_count) : 0);
483                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
484                 if (rc)
485                         GOTO(end, rc);
486
487                 /* Set root stripeoffset */
488                 sprintf(buf, ".stripeoffset=%hd",
489                         lump ? le16_to_cpu(lump->lmm_stripe_offset) :
490                         (typeof(lump->lmm_stripe_offset))(-1));
491                 rc = ll_send_mgc_param(mgc->u.cli.cl_mgc_mgsexp, param);
492
493 end:
494                 if (param != NULL)
495                         OBD_FREE(param, MGS_PARAM_MAXLEN);
496         }
497         RETURN(rc);
498 }
499
500 int ll_dir_getstripe(struct inode *inode, struct lov_mds_md **lmmp,
501                      int *lmm_size, struct ptlrpc_request **request)
502 {
503         struct ll_sb_info *sbi = ll_i2sbi(inode);
504         struct mdt_body   *body;
505         struct lov_mds_md *lmm = NULL;
506         struct ptlrpc_request *req = NULL;
507         int rc, lmmsize;
508         struct md_op_data *op_data;
509
510         rc = ll_get_max_mdsize(sbi, &lmmsize);
511         if (rc)
512                 RETURN(rc);
513
514         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL,
515                                      0, lmmsize, LUSTRE_OPC_ANY,
516                                      NULL);
517         if (IS_ERR(op_data))
518                 RETURN(PTR_ERR(op_data));
519
520         op_data->op_valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
521         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
522         ll_finish_md_op_data(op_data);
523         if (rc < 0) {
524                 CDEBUG(D_INFO, "md_getattr failed on inode "
525                        DFID": rc %d\n", PFID(ll_inode2fid(inode)), rc);
526                 GOTO(out, rc);
527         }
528
529         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
530         LASSERT(body != NULL);
531
532         lmmsize = body->eadatasize;
533
534         if (!(body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) ||
535             lmmsize == 0) {
536                 GOTO(out, rc = -ENODATA);
537         }
538
539         lmm = req_capsule_server_sized_get(&req->rq_pill,
540                                            &RMF_MDT_MD, lmmsize);
541         LASSERT(lmm != NULL);
542
543         /*
544          * This is coming from the MDS, so is probably in
545          * little endian.  We convert it to host endian before
546          * passing it to userspace.
547          */
548         /* We don't swab objects for directories */
549         switch (le32_to_cpu(lmm->lmm_magic)) {
550         case LOV_MAGIC_V1:
551                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
552                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
553                 break;
554         case LOV_MAGIC_V3:
555                 if (LOV_MAGIC != cpu_to_le32(LOV_MAGIC))
556                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
557                 break;
558         default:
559                 CERROR("unknown magic: %lX\n", (unsigned long)lmm->lmm_magic);
560                 rc = -EPROTO;
561         }
562 out:
563         *lmmp = lmm;
564         *lmm_size = lmmsize;
565         *request = req;
566         return rc;
567 }
568
569 /*
570  *  Get MDT index for the inode.
571  */
572 int ll_get_mdt_idx(struct inode *inode)
573 {
574         struct ll_sb_info *sbi = ll_i2sbi(inode);
575         struct md_op_data *op_data;
576         int rc, mdtidx;
577         ENTRY;
578
579         op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0,
580                                      0, LUSTRE_OPC_ANY, NULL);
581         if (IS_ERR(op_data))
582                 RETURN(PTR_ERR(op_data));
583
584         op_data->op_flags |= MF_GET_MDT_IDX;
585         rc = md_getattr(sbi->ll_md_exp, op_data, NULL);
586         mdtidx = op_data->op_mds;
587         ll_finish_md_op_data(op_data);
588         if (rc < 0) {
589                 CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
590                 RETURN(rc);
591         }
592         return mdtidx;
593 }
594
595 /**
596  * Generic handler to do any pre-copy work.
597  *
598  * It send a first hsm_progress (with extent length == 0) to coordinator as a
599  * first information for it that real work has started.
600  *
601  * Moreover, for a ARCHIVE request, it will sample the file data version and
602  * store it in \a copy.
603  *
604  * \return 0 on success.
605  */
606 static int ll_ioc_copy_start(struct super_block *sb, struct hsm_copy *copy)
607 {
608         struct ll_sb_info               *sbi = ll_s2sbi(sb);
609         struct hsm_progress_kernel       hpk;
610         int                              rc;
611         ENTRY;
612
613         /* Forge a hsm_progress based on data from copy. */
614         hpk.hpk_fid = copy->hc_hai.hai_fid;
615         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
616         hpk.hpk_extent.offset = copy->hc_hai.hai_extent.offset;
617         hpk.hpk_extent.length = 0;
618         hpk.hpk_flags = 0;
619         hpk.hpk_errval = 0;
620         hpk.hpk_data_version = 0;
621
622
623         /* For archive request, we need to read the current file version. */
624         if (copy->hc_hai.hai_action == HSMA_ARCHIVE) {
625                 struct inode    *inode;
626                 __u64            data_version = 0;
627
628                 /* Get inode for this fid */
629                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
630                 if (IS_ERR(inode)) {
631                         hpk.hpk_flags |= HP_FLAG_RETRY;
632                         /* hpk_errval is >= 0 */
633                         hpk.hpk_errval = -PTR_ERR(inode);
634                         GOTO(progress, rc = PTR_ERR(inode));
635                 }
636
637                 /* Read current file data version */
638                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
639                 iput(inode);
640                 if (rc != 0) {
641                         CDEBUG(D_HSM, "Could not read file data version of "
642                                       DFID" (rc = %d). Archive request ("
643                                       LPX64") could not be done.\n",
644                                       PFID(&copy->hc_hai.hai_fid), rc,
645                                       copy->hc_hai.hai_cookie);
646                         hpk.hpk_flags |= HP_FLAG_RETRY;
647                         /* hpk_errval must be >= 0 */
648                         hpk.hpk_errval = -rc;
649                         GOTO(progress, rc);
650                 }
651
652                 /* Store it the hsm_copy for later copytool use.
653                  * Always modified even if no lsm. */
654                 copy->hc_data_version = data_version;
655         }
656
657 progress:
658         /* On error, the request should be considered as completed */
659         if (hpk.hpk_errval > 0)
660                 hpk.hpk_flags |= HP_FLAG_COMPLETED;
661         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
662                            &hpk, NULL);
663
664         RETURN(rc);
665 }
666
667 /**
668  * Generic handler to do any post-copy work.
669  *
670  * It will send the last hsm_progress update to coordinator to inform it
671  * that copy is finished and whether it was successful or not.
672  *
673  * Moreover,
674  * - for ARCHIVE request, it will sample the file data version and compare it
675  *   with the version saved in ll_ioc_copy_start(). If they do not match, copy
676  *   will be considered as failed.
677  * - for RESTORE request, it will sample the file data version and send it to
678  *   coordinator which is useful if the file was imported as 'released'.
679  *
680  * \return 0 on success.
681  */
682 static int ll_ioc_copy_end(struct super_block *sb, struct hsm_copy *copy)
683 {
684         struct ll_sb_info               *sbi = ll_s2sbi(sb);
685         struct hsm_progress_kernel       hpk;
686         int                              rc;
687         ENTRY;
688
689         /* If you modify the logic here, also check llapi_hsm_copy_end(). */
690         /* Take care: copy->hc_hai.hai_action, len, gid and data are not
691          * initialized if copy_end was called with copy == NULL.
692          */
693
694         /* Forge a hsm_progress based on data from copy. */
695         hpk.hpk_fid = copy->hc_hai.hai_fid;
696         hpk.hpk_cookie = copy->hc_hai.hai_cookie;
697         hpk.hpk_extent = copy->hc_hai.hai_extent;
698         hpk.hpk_flags = copy->hc_flags | HP_FLAG_COMPLETED;
699         hpk.hpk_errval = copy->hc_errval;
700         hpk.hpk_data_version = 0;
701
702         /* For archive request, we need to check the file data was not changed.
703          *
704          * For restore request, we need to send the file data version, this is
705          * useful when the file was created using hsm_import.
706          */
707         if (((copy->hc_hai.hai_action == HSMA_ARCHIVE) ||
708              (copy->hc_hai.hai_action == HSMA_RESTORE)) &&
709             (copy->hc_errval == 0)) {
710                 struct inode    *inode;
711                 __u64            data_version = 0;
712
713                 /* Get lsm for this fid */
714                 inode = search_inode_for_lustre(sb, &copy->hc_hai.hai_fid);
715                 if (IS_ERR(inode)) {
716                         hpk.hpk_flags |= HP_FLAG_RETRY;
717                         /* hpk_errval must be >= 0 */
718                         hpk.hpk_errval = -PTR_ERR(inode);
719                         GOTO(progress, rc = PTR_ERR(inode));
720                 }
721
722                 rc = ll_data_version(inode, &data_version, LL_DV_RD_FLUSH);
723                 iput(inode);
724                 if (rc) {
725                         CDEBUG(D_HSM, "Could not read file data version. "
726                                       "Request could not be confirmed.\n");
727                         if (hpk.hpk_errval == 0)
728                                 hpk.hpk_errval = -rc;
729                         GOTO(progress, rc);
730                 }
731
732                 /* Store it the hsm_copy for later copytool use.
733                  * Always modified even if no lsm. */
734                 hpk.hpk_data_version = data_version;
735
736                 /* File could have been stripped during archiving, so we need
737                  * to check anyway. */
738                 if ((copy->hc_hai.hai_action == HSMA_ARCHIVE) &&
739                     (copy->hc_data_version != data_version)) {
740                         CDEBUG(D_HSM, "File data version mismatched. "
741                               "File content was changed during archiving. "
742                                DFID", start:"LPX64" current:"LPX64"\n",
743                                PFID(&copy->hc_hai.hai_fid),
744                                copy->hc_data_version, data_version);
745                         /* File was changed, send error to cdt. Do not ask for
746                          * retry because if a file is modified frequently,
747                          * the cdt will loop on retried archive requests.
748                          * The policy engine will ask for a new archive later
749                          * when the file will not be modified for some tunable
750                          * time */
751                         /* we do not notify caller */
752                         hpk.hpk_flags &= ~HP_FLAG_RETRY;
753                         /* hpk_errval must be >= 0 */
754                         hpk.hpk_errval = EBUSY;
755                 }
756
757         }
758
759 progress:
760         rc = obd_iocontrol(LL_IOC_HSM_PROGRESS, sbi->ll_md_exp, sizeof(hpk),
761                            &hpk, NULL);
762
763         RETURN(rc);
764 }
765
766
767 static int copy_and_ioctl(int cmd, struct obd_export *exp,
768                           const void __user *data, size_t size)
769 {
770         void *copy;
771         int rc;
772
773         OBD_ALLOC(copy, size);
774         if (copy == NULL)
775                 return -ENOMEM;
776
777         if (copy_from_user(copy, data, size)) {
778                 rc = -EFAULT;
779                 goto out;
780         }
781
782         rc = obd_iocontrol(cmd, exp, size, copy, NULL);
783 out:
784         OBD_FREE(copy, size);
785
786         return rc;
787 }
788
789 static int quotactl_ioctl(struct ll_sb_info *sbi, struct if_quotactl *qctl)
790 {
791         int cmd = qctl->qc_cmd;
792         int type = qctl->qc_type;
793         int id = qctl->qc_id;
794         int valid = qctl->qc_valid;
795         int rc = 0;
796         ENTRY;
797
798         switch (cmd) {
799         case LUSTRE_Q_INVALIDATE:
800         case LUSTRE_Q_FINVALIDATE:
801         case Q_QUOTAON:
802         case Q_QUOTAOFF:
803         case Q_SETQUOTA:
804         case Q_SETINFO:
805                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
806                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
807                         RETURN(-EPERM);
808                 break;
809         case Q_GETQUOTA:
810                 if (((type == USRQUOTA && current_euid() != id) ||
811                      (type == GRPQUOTA && !in_egroup_p(id))) &&
812                     (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
813                      sbi->ll_flags & LL_SBI_RMT_CLIENT))
814                         RETURN(-EPERM);
815                 break;
816         case Q_GETINFO:
817                 break;
818         default:
819                 CERROR("unsupported quotactl op: %#x\n", cmd);
820                 RETURN(-ENOTTY);
821         }
822
823         if (valid != QC_GENERAL) {
824                 if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
825                         RETURN(-EOPNOTSUPP);
826
827                 if (cmd == Q_GETINFO)
828                         qctl->qc_cmd = Q_GETOINFO;
829                 else if (cmd == Q_GETQUOTA)
830                         qctl->qc_cmd = Q_GETOQUOTA;
831                 else
832                         RETURN(-EINVAL);
833
834                 switch (valid) {
835                 case QC_MDTIDX:
836                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
837                                            sizeof(*qctl), qctl, NULL);
838                         break;
839                 case QC_OSTIDX:
840                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_dt_exp,
841                                            sizeof(*qctl), qctl, NULL);
842                         break;
843                 case QC_UUID:
844                         rc = obd_iocontrol(OBD_IOC_QUOTACTL, sbi->ll_md_exp,
845                                            sizeof(*qctl), qctl, NULL);
846                         if (rc == -EAGAIN)
847                                 rc = obd_iocontrol(OBD_IOC_QUOTACTL,
848                                                    sbi->ll_dt_exp,
849                                                    sizeof(*qctl), qctl, NULL);
850                         break;
851                 default:
852                         rc = -EINVAL;
853                         break;
854                 }
855
856                 if (rc)
857                         RETURN(rc);
858
859                 qctl->qc_cmd = cmd;
860         } else {
861                 struct obd_quotactl *oqctl;
862
863                 OBD_ALLOC_PTR(oqctl);
864                 if (oqctl == NULL)
865                         RETURN(-ENOMEM);
866
867                 QCTL_COPY(oqctl, qctl);
868                 rc = obd_quotactl(sbi->ll_md_exp, oqctl);
869                 if (rc) {
870                         if (rc != -EALREADY && cmd == Q_QUOTAON) {
871                                 oqctl->qc_cmd = Q_QUOTAOFF;
872                                 obd_quotactl(sbi->ll_md_exp, oqctl);
873                         }
874                         OBD_FREE_PTR(oqctl);
875                         RETURN(rc);
876                 }
877                 /* If QIF_SPACE is not set, client should collect the
878                  * space usage from OSSs by itself */
879                 if (cmd == Q_GETQUOTA &&
880                     !(oqctl->qc_dqblk.dqb_valid & QIF_SPACE) &&
881                     !oqctl->qc_dqblk.dqb_curspace) {
882                         struct obd_quotactl *oqctl_tmp;
883
884                         OBD_ALLOC_PTR(oqctl_tmp);
885                         if (oqctl_tmp == NULL)
886                                 GOTO(out, rc = -ENOMEM);
887
888                         oqctl_tmp->qc_cmd = Q_GETOQUOTA;
889                         oqctl_tmp->qc_id = oqctl->qc_id;
890                         oqctl_tmp->qc_type = oqctl->qc_type;
891
892                         /* collect space usage from OSTs */
893                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
894                         rc = obd_quotactl(sbi->ll_dt_exp, oqctl_tmp);
895                         if (!rc || rc == -EREMOTEIO) {
896                                 oqctl->qc_dqblk.dqb_curspace =
897                                         oqctl_tmp->qc_dqblk.dqb_curspace;
898                                 oqctl->qc_dqblk.dqb_valid |= QIF_SPACE;
899                         }
900
901                         /* collect space & inode usage from MDTs */
902                         oqctl_tmp->qc_dqblk.dqb_curspace = 0;
903                         oqctl_tmp->qc_dqblk.dqb_curinodes = 0;
904                         rc = obd_quotactl(sbi->ll_md_exp, oqctl_tmp);
905                         if (!rc || rc == -EREMOTEIO) {
906                                 oqctl->qc_dqblk.dqb_curspace +=
907                                         oqctl_tmp->qc_dqblk.dqb_curspace;
908                                 oqctl->qc_dqblk.dqb_curinodes =
909                                         oqctl_tmp->qc_dqblk.dqb_curinodes;
910                                 oqctl->qc_dqblk.dqb_valid |= QIF_INODES;
911                         } else {
912                                 oqctl->qc_dqblk.dqb_valid &= ~QIF_SPACE;
913                         }
914
915                         OBD_FREE_PTR(oqctl_tmp);
916                 }
917 out:
918                 QCTL_COPY(qctl, oqctl);
919                 OBD_FREE_PTR(oqctl);
920         }
921
922         RETURN(rc);
923 }
924
925 static char *
926 ll_getname(const char __user *filename)
927 {
928         int ret = 0, len;
929         char *tmp = __getname();
930
931         if (!tmp)
932                 return ERR_PTR(-ENOMEM);
933
934         len = strncpy_from_user(tmp, filename, PATH_MAX);
935         if (len == 0)
936                 ret = -ENOENT;
937         else if (len > PATH_MAX)
938                 ret = -ENAMETOOLONG;
939
940         if (ret) {
941                 __putname(tmp);
942                 tmp =  ERR_PTR(ret);
943         }
944         return tmp;
945 }
946
947 #define ll_putname(filename) __putname(filename)
948
949 static long ll_dir_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
950 {
951         struct inode *inode = file->f_dentry->d_inode;
952         struct ll_sb_info *sbi = ll_i2sbi(inode);
953         struct obd_ioctl_data *data;
954         int rc = 0;
955         ENTRY;
956
957         CDEBUG(D_VFSTRACE, "VFS Op:inode="DFID"(%p), cmd=%#x\n",
958                PFID(ll_inode2fid(inode)), inode, cmd);
959
960         /* asm-ppc{,64} declares TCGETS, et. al. as type 't' not 'T' */
961         if (_IOC_TYPE(cmd) == 'T' || _IOC_TYPE(cmd) == 't') /* tty ioctls */
962                 return -ENOTTY;
963
964         ll_stats_ops_tally(ll_i2sbi(inode), LPROC_LL_IOCTL, 1);
965         switch(cmd) {
966         case FSFILT_IOC_GETFLAGS:
967         case FSFILT_IOC_SETFLAGS:
968                 RETURN(ll_iocontrol(inode, file, cmd, arg));
969         case FSFILT_IOC_GETVERSION_OLD:
970         case FSFILT_IOC_GETVERSION:
971                 RETURN(put_user(inode->i_generation, (int *)arg));
972         /* We need to special case any other ioctls we want to handle,
973          * to send them to the MDS/OST as appropriate and to properly
974          * network encode the arg field.
975         case FSFILT_IOC_SETVERSION_OLD:
976         case FSFILT_IOC_SETVERSION:
977         */
978         case LL_IOC_GET_MDTIDX: {
979                 int mdtidx;
980
981                 mdtidx = ll_get_mdt_idx(inode);
982                 if (mdtidx < 0)
983                         RETURN(mdtidx);
984
985                 if (put_user((int)mdtidx, (int*)arg))
986                         RETURN(-EFAULT);
987
988                 return 0;
989         }
990         case IOC_MDC_LOOKUP: {
991                 struct ptlrpc_request *request = NULL;
992                 int namelen, len = 0;
993                 char *buf = NULL;
994                 char *filename;
995                 struct md_op_data *op_data;
996
997                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
998                 if (rc)
999                         RETURN(rc);
1000                 data = (void *)buf;
1001
1002                 filename = data->ioc_inlbuf1;
1003                 namelen = strlen(filename);
1004
1005                 if (namelen < 1) {
1006                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1007                         GOTO(out_free, rc = -EINVAL);
1008                 }
1009
1010                 op_data = ll_prep_md_op_data(NULL, inode, NULL, filename, namelen,
1011                                              0, LUSTRE_OPC_ANY, NULL);
1012                 if (IS_ERR(op_data))
1013                         GOTO(out_free, rc = PTR_ERR(op_data));
1014
1015                 op_data->op_valid = OBD_MD_FLID;
1016                 rc = md_getattr_name(sbi->ll_md_exp, op_data, &request);
1017                 ll_finish_md_op_data(op_data);
1018                 if (rc < 0) {
1019                         CDEBUG(D_INFO, "md_getattr_name: %d\n", rc);
1020                         GOTO(out_free, rc);
1021                 }
1022                 ptlrpc_req_finished(request);
1023                 EXIT;
1024 out_free:
1025                 obd_ioctl_freedata(buf, len);
1026                 return rc;
1027         }
1028         case LL_IOC_LMV_SETSTRIPE: {
1029                 struct lmv_user_md  *lum;
1030                 char            *buf = NULL;
1031                 char            *filename;
1032                 int              namelen = 0;
1033                 int              lumlen = 0;
1034                 int              len;
1035                 int              rc;
1036
1037                 rc = obd_ioctl_getdata(&buf, &len, (void *)arg);
1038                 if (rc)
1039                         RETURN(rc);
1040
1041                 data = (void *)buf;
1042                 if (data->ioc_inlbuf1 == NULL || data->ioc_inlbuf2 == NULL ||
1043                     data->ioc_inllen1 == 0 || data->ioc_inllen2 == 0)
1044                         GOTO(lmv_out_free, rc = -EINVAL);
1045
1046                 filename = data->ioc_inlbuf1;
1047                 namelen = data->ioc_inllen1;
1048
1049                 if (namelen < 1) {
1050                         CDEBUG(D_INFO, "IOC_MDC_LOOKUP missing filename\n");
1051                         GOTO(lmv_out_free, rc = -EINVAL);
1052                 }
1053                 lum = (struct lmv_user_md *)data->ioc_inlbuf2;
1054                 lumlen = data->ioc_inllen2;
1055
1056                 if (lum->lum_magic != LMV_USER_MAGIC ||
1057                     lumlen != sizeof(*lum)) {
1058                         CERROR("%s: wrong lum magic %x or size %d: rc = %d\n",
1059                                filename, lum->lum_magic, lumlen, -EFAULT);
1060                         GOTO(lmv_out_free, rc = -EINVAL);
1061                 }
1062
1063                 /**
1064                  * ll_dir_setdirstripe will be used to set dir stripe
1065                  *  mdc_create--->mdt_reint_create (with dirstripe)
1066                  */
1067                 rc = ll_dir_setdirstripe(inode, lum, filename);
1068 lmv_out_free:
1069                 obd_ioctl_freedata(buf, len);
1070                 RETURN(rc);
1071
1072         }
1073         case LL_IOC_LOV_SETSTRIPE: {
1074                 struct lov_user_md_v3 lumv3;
1075                 struct lov_user_md_v1 *lumv1 = (struct lov_user_md_v1 *)&lumv3;
1076                 struct lov_user_md_v1 *lumv1p = (struct lov_user_md_v1 *)arg;
1077                 struct lov_user_md_v3 *lumv3p = (struct lov_user_md_v3 *)arg;
1078
1079                 int set_default = 0;
1080
1081                 LASSERT(sizeof(lumv3) == sizeof(*lumv3p));
1082                 LASSERT(sizeof(lumv3.lmm_objects[0]) ==
1083                         sizeof(lumv3p->lmm_objects[0]));
1084                 /* first try with v1 which is smaller than v3 */
1085                 if (copy_from_user(lumv1, lumv1p, sizeof(*lumv1)))
1086                         RETURN(-EFAULT);
1087
1088                 if ((lumv1->lmm_magic == LOV_USER_MAGIC_V3) ) {
1089                         if (copy_from_user(&lumv3, lumv3p, sizeof(lumv3)))
1090                                 RETURN(-EFAULT);
1091                 }
1092
1093                 if (inode->i_sb->s_root == file->f_dentry)
1094                         set_default = 1;
1095
1096                 /* in v1 and v3 cases lumv1 points to data */
1097                 rc = ll_dir_setstripe(inode, lumv1, set_default);
1098
1099                 RETURN(rc);
1100         }
1101         case LL_IOC_LMV_GETSTRIPE: {
1102                 struct lmv_user_md *lump = (struct lmv_user_md *)arg;
1103                 struct lmv_user_md lum;
1104                 struct lmv_user_md *tmp;
1105                 int lum_size;
1106                 int rc = 0;
1107                 int mdtindex;
1108
1109                 if (copy_from_user(&lum, lump, sizeof(struct lmv_user_md)))
1110                         RETURN(-EFAULT);
1111
1112                 if (lum.lum_magic != LMV_MAGIC_V1)
1113                         RETURN(-EINVAL);
1114
1115                 lum_size = lmv_user_md_size(1, LMV_MAGIC_V1);
1116                 OBD_ALLOC(tmp, lum_size);
1117                 if (tmp == NULL)
1118                         GOTO(free_lmv, rc = -ENOMEM);
1119
1120                 memcpy(tmp, &lum, sizeof(lum));
1121                 tmp->lum_type = LMV_STRIPE_TYPE;
1122                 tmp->lum_stripe_count = 1;
1123                 mdtindex = ll_get_mdt_idx(inode);
1124                 if (mdtindex < 0)
1125                         GOTO(free_lmv, rc = -ENOMEM);
1126
1127                 tmp->lum_stripe_offset = mdtindex;
1128                 tmp->lum_objects[0].lum_mds = mdtindex;
1129                 memcpy(&tmp->lum_objects[0].lum_fid, ll_inode2fid(inode),
1130                        sizeof(struct lu_fid));
1131                 if (copy_to_user((void *)arg, tmp, lum_size))
1132                         GOTO(free_lmv, rc = -EFAULT);
1133 free_lmv:
1134                 if (tmp)
1135                         OBD_FREE(tmp, lum_size);
1136                 RETURN(rc);
1137         }
1138         case LL_IOC_REMOVE_ENTRY: {
1139                 char            *filename = NULL;
1140                 int              namelen = 0;
1141                 int              rc;
1142
1143                 /* Here is a little hack to avoid sending REINT_RMENTRY to
1144                  * unsupported server, which might crash the server(LU-2730),
1145                  * Because both LVB_TYPE and REINT_RMENTRY will be supported
1146                  * on 2.4, we use OBD_CONNECT_LVB_TYPE to detect whether the
1147                  * server will support REINT_RMENTRY XXX*/
1148                 if (!(exp_connect_flags(sbi->ll_md_exp) & OBD_CONNECT_LVB_TYPE))
1149                         RETURN(-ENOTSUPP);
1150
1151                 filename = ll_getname((const char *)arg);
1152                 if (IS_ERR(filename))
1153                         RETURN(PTR_ERR(filename));
1154
1155                 namelen = strlen(filename);
1156                 if (namelen < 1)
1157                         GOTO(out_rmdir, rc = -EINVAL);
1158
1159                 rc = ll_rmdir_entry(inode, filename, namelen);
1160 out_rmdir:
1161                 if (filename)
1162                         ll_putname(filename);
1163                 RETURN(rc);
1164         }
1165         case LL_IOC_LOV_SWAP_LAYOUTS:
1166                 RETURN(-EPERM);
1167         case LL_IOC_OBD_STATFS:
1168                 RETURN(ll_obd_statfs(inode, (void *)arg));
1169         case LL_IOC_LOV_GETSTRIPE:
1170         case LL_IOC_MDC_GETINFO:
1171         case IOC_MDC_GETFILEINFO:
1172         case IOC_MDC_GETFILESTRIPE: {
1173                 struct ptlrpc_request *request = NULL;
1174                 struct lov_user_md *lump;
1175                 struct lov_mds_md *lmm = NULL;
1176                 struct mdt_body *body;
1177                 char *filename = NULL;
1178                 int lmmsize;
1179
1180                 if (cmd == IOC_MDC_GETFILEINFO ||
1181                     cmd == IOC_MDC_GETFILESTRIPE) {
1182                         filename = ll_getname((const char *)arg);
1183                         if (IS_ERR(filename))
1184                                 RETURN(PTR_ERR(filename));
1185
1186                         rc = ll_lov_getstripe_ea_info(inode, filename, &lmm,
1187                                                       &lmmsize, &request);
1188                 } else {
1189                         rc = ll_dir_getstripe(inode, &lmm, &lmmsize, &request);
1190                 }
1191
1192                 if (request) {
1193                         body = req_capsule_server_get(&request->rq_pill,
1194                                                       &RMF_MDT_BODY);
1195                         LASSERT(body != NULL);
1196                 } else {
1197                         GOTO(out_req, rc);
1198                 }
1199
1200                 if (rc < 0) {
1201                         if (rc == -ENODATA && (cmd == IOC_MDC_GETFILEINFO ||
1202                                                cmd == LL_IOC_MDC_GETINFO))
1203                                 GOTO(skip_lmm, rc = 0);
1204                         else
1205                                 GOTO(out_req, rc);
1206                 }
1207
1208                 if (cmd == IOC_MDC_GETFILESTRIPE ||
1209                     cmd == LL_IOC_LOV_GETSTRIPE) {
1210                         lump = (struct lov_user_md *)arg;
1211                 } else {
1212                         struct lov_user_mds_data *lmdp;
1213                         lmdp = (struct lov_user_mds_data *)arg;
1214                         lump = &lmdp->lmd_lmm;
1215                 }
1216                 if (copy_to_user(lump, lmm, lmmsize)) {
1217                         if (copy_to_user(lump, lmm, sizeof(*lump)))
1218                                 GOTO(out_req, rc = -EFAULT);
1219                         rc = -EOVERFLOW;
1220                 }
1221         skip_lmm:
1222                 if (cmd == IOC_MDC_GETFILEINFO || cmd == LL_IOC_MDC_GETINFO) {
1223                         struct lov_user_mds_data *lmdp;
1224                         lstat_t st = { 0 };
1225
1226                         st.st_dev     = inode->i_sb->s_dev;
1227                         st.st_mode    = body->mode;
1228                         st.st_nlink   = body->nlink;
1229                         st.st_uid     = body->uid;
1230                         st.st_gid     = body->gid;
1231                         st.st_rdev    = body->rdev;
1232                         st.st_size    = body->size;
1233                         st.st_blksize = PAGE_CACHE_SIZE;
1234                         st.st_blocks  = body->blocks;
1235                         st.st_atime   = body->atime;
1236                         st.st_mtime   = body->mtime;
1237                         st.st_ctime   = body->ctime;
1238                         st.st_ino     = inode->i_ino;
1239
1240                         lmdp = (struct lov_user_mds_data *)arg;
1241                         if (copy_to_user(&lmdp->lmd_st, &st, sizeof(st)))
1242                                 GOTO(out_req, rc = -EFAULT);
1243                 }
1244
1245                 EXIT;
1246         out_req:
1247                 ptlrpc_req_finished(request);
1248                 if (filename)
1249                         ll_putname(filename);
1250                 return rc;
1251         }
1252         case IOC_LOV_GETINFO: {
1253                 struct lov_user_mds_data *lumd;
1254                 struct lov_stripe_md *lsm;
1255                 struct lov_user_md *lum;
1256                 struct lov_mds_md *lmm;
1257                 int lmmsize;
1258                 lstat_t st;
1259
1260                 lumd = (struct lov_user_mds_data *)arg;
1261                 lum = &lumd->lmd_lmm;
1262
1263                 rc = ll_get_max_mdsize(sbi, &lmmsize);
1264                 if (rc)
1265                         RETURN(rc);
1266
1267                 OBD_ALLOC_LARGE(lmm, lmmsize);
1268                 if (lmm == NULL)
1269                         RETURN(-ENOMEM);
1270
1271                 if (copy_from_user(lmm, lum, lmmsize))
1272                         GOTO(free_lmm, rc = -EFAULT);
1273
1274                 switch (lmm->lmm_magic) {
1275                 case LOV_USER_MAGIC_V1:
1276                         if (LOV_USER_MAGIC_V1 == cpu_to_le32(LOV_USER_MAGIC_V1))
1277                                 break;
1278                         /* swab objects first so that stripes num will be sane */
1279                         lustre_swab_lov_user_md_objects(
1280                                 ((struct lov_user_md_v1 *)lmm)->lmm_objects,
1281                                 ((struct lov_user_md_v1 *)lmm)->lmm_stripe_count);
1282                         lustre_swab_lov_user_md_v1((struct lov_user_md_v1 *)lmm);
1283                         break;
1284                 case LOV_USER_MAGIC_V3:
1285                         if (LOV_USER_MAGIC_V3 == cpu_to_le32(LOV_USER_MAGIC_V3))
1286                                 break;
1287                         /* swab objects first so that stripes num will be sane */
1288                         lustre_swab_lov_user_md_objects(
1289                                 ((struct lov_user_md_v3 *)lmm)->lmm_objects,
1290                                 ((struct lov_user_md_v3 *)lmm)->lmm_stripe_count);
1291                         lustre_swab_lov_user_md_v3((struct lov_user_md_v3 *)lmm);
1292                         break;
1293                 default:
1294                         GOTO(free_lmm, rc = -EINVAL);
1295                 }
1296
1297                 rc = obd_unpackmd(sbi->ll_dt_exp, &lsm, lmm, lmmsize);
1298                 if (rc < 0)
1299                         GOTO(free_lmm, rc = -ENOMEM);
1300
1301                 /* Perform glimpse_size operation. */
1302                 memset(&st, 0, sizeof(st));
1303
1304                 rc = ll_glimpse_ioctl(sbi, lsm, &st);
1305                 if (rc)
1306                         GOTO(free_lsm, rc);
1307
1308                 if (copy_to_user(&lumd->lmd_st, &st, sizeof(st)))
1309                         GOTO(free_lsm, rc = -EFAULT);
1310
1311                 EXIT;
1312         free_lsm:
1313                 obd_free_memmd(sbi->ll_dt_exp, &lsm);
1314         free_lmm:
1315                 OBD_FREE_LARGE(lmm, lmmsize);
1316                 return rc;
1317         }
1318         case OBD_IOC_LLOG_CATINFO: {
1319                 RETURN(-EOPNOTSUPP);
1320         }
1321         case OBD_IOC_QUOTACHECK: {
1322                 struct obd_quotactl *oqctl;
1323                 int error = 0;
1324
1325                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1326                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1327                         RETURN(-EPERM);
1328
1329                 OBD_ALLOC_PTR(oqctl);
1330                 if (!oqctl)
1331                         RETURN(-ENOMEM);
1332                 oqctl->qc_type = arg;
1333                 rc = obd_quotacheck(sbi->ll_md_exp, oqctl);
1334                 if (rc < 0) {
1335                         CDEBUG(D_INFO, "md_quotacheck failed: rc %d\n", rc);
1336                         error = rc;
1337                 }
1338
1339                 rc = obd_quotacheck(sbi->ll_dt_exp, oqctl);
1340                 if (rc < 0)
1341                         CDEBUG(D_INFO, "obd_quotacheck failed: rc %d\n", rc);
1342
1343                 OBD_FREE_PTR(oqctl);
1344                 return error ?: rc;
1345         }
1346         case OBD_IOC_POLL_QUOTACHECK: {
1347                 struct if_quotacheck *check;
1348
1349                 if (!cfs_capable(CFS_CAP_SYS_ADMIN) ||
1350                     sbi->ll_flags & LL_SBI_RMT_CLIENT)
1351                         RETURN(-EPERM);
1352
1353                 OBD_ALLOC_PTR(check);
1354                 if (!check)
1355                         RETURN(-ENOMEM);
1356
1357                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, 0, (void *)check,
1358                                    NULL);
1359                 if (rc) {
1360                         CDEBUG(D_QUOTA, "mdc ioctl %d failed: %d\n", cmd, rc);
1361                         if (copy_to_user((void *)arg, check,
1362                                              sizeof(*check)))
1363                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1364                         GOTO(out_poll, rc);
1365                 }
1366
1367                 rc = obd_iocontrol(cmd, sbi->ll_dt_exp, 0, (void *)check,
1368                                    NULL);
1369                 if (rc) {
1370                         CDEBUG(D_QUOTA, "osc ioctl %d failed: %d\n", cmd, rc);
1371                         if (copy_to_user((void *)arg, check,
1372                                              sizeof(*check)))
1373                                 CDEBUG(D_QUOTA, "copy_to_user failed\n");
1374                         GOTO(out_poll, rc);
1375                 }
1376         out_poll:
1377                 OBD_FREE_PTR(check);
1378                 RETURN(rc);
1379         }
1380 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
1381         case LL_IOC_QUOTACTL_18: {
1382                 /* copy the old 1.x quota struct for internal use, then copy
1383                  * back into old format struct.  For 1.8 compatibility. */
1384                 struct if_quotactl_18 *qctl_18;
1385                 struct if_quotactl *qctl_20;
1386
1387                 OBD_ALLOC_PTR(qctl_18);
1388                 if (!qctl_18)
1389                         RETURN(-ENOMEM);
1390
1391                 OBD_ALLOC_PTR(qctl_20);
1392                 if (!qctl_20)
1393                         GOTO(out_quotactl_18, rc = -ENOMEM);
1394
1395                 if (copy_from_user(qctl_18, (void *)arg, sizeof(*qctl_18)))
1396                         GOTO(out_quotactl_20, rc = -ENOMEM);
1397
1398                 QCTL_COPY(qctl_20, qctl_18);
1399                 qctl_20->qc_idx = 0;
1400
1401                 /* XXX: dqb_valid was borrowed as a flag to mark that
1402                  *      only mds quota is wanted */
1403                 if (qctl_18->qc_cmd == Q_GETQUOTA &&
1404                     qctl_18->qc_dqblk.dqb_valid) {
1405                         qctl_20->qc_valid = QC_MDTIDX;
1406                         qctl_20->qc_dqblk.dqb_valid = 0;
1407                 } else if (qctl_18->obd_uuid.uuid[0] != '\0') {
1408                         qctl_20->qc_valid = QC_UUID;
1409                         qctl_20->obd_uuid = qctl_18->obd_uuid;
1410                 } else {
1411                         qctl_20->qc_valid = QC_GENERAL;
1412                 }
1413
1414                 rc = quotactl_ioctl(sbi, qctl_20);
1415
1416                 if (rc == 0) {
1417                         QCTL_COPY(qctl_18, qctl_20);
1418                         qctl_18->obd_uuid = qctl_20->obd_uuid;
1419
1420                         if (copy_to_user((void *)arg, qctl_18,
1421                                              sizeof(*qctl_18)))
1422                                 rc = -EFAULT;
1423                 }
1424
1425         out_quotactl_20:
1426                 OBD_FREE_PTR(qctl_20);
1427         out_quotactl_18:
1428                 OBD_FREE_PTR(qctl_18);
1429                 RETURN(rc);
1430         }
1431 #else
1432 #warning "remove old LL_IOC_QUOTACTL_18 compatibility code"
1433 #endif /* LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0) */
1434         case LL_IOC_QUOTACTL: {
1435                 struct if_quotactl *qctl;
1436
1437                 OBD_ALLOC_PTR(qctl);
1438                 if (!qctl)
1439                         RETURN(-ENOMEM);
1440
1441                 if (copy_from_user(qctl, (void *)arg, sizeof(*qctl)))
1442                         GOTO(out_quotactl, rc = -EFAULT);
1443
1444                 rc = quotactl_ioctl(sbi, qctl);
1445
1446                 if (rc == 0 && copy_to_user((void *)arg, qctl, sizeof(*qctl)))
1447                         rc = -EFAULT;
1448
1449         out_quotactl:
1450                 OBD_FREE_PTR(qctl);
1451                 RETURN(rc);
1452         }
1453         case OBD_IOC_GETDTNAME:
1454         case OBD_IOC_GETMDNAME:
1455                 RETURN(ll_get_obd_name(inode, cmd, arg));
1456         case LL_IOC_FLUSHCTX:
1457                 RETURN(ll_flush_ctx(inode));
1458 #ifdef CONFIG_FS_POSIX_ACL
1459         case LL_IOC_RMTACL: {
1460             if (sbi->ll_flags & LL_SBI_RMT_CLIENT &&
1461                 inode == inode->i_sb->s_root->d_inode) {
1462                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1463
1464                 LASSERT(fd != NULL);
1465                 rc = rct_add(&sbi->ll_rct, current_pid(), arg);
1466                 if (!rc)
1467                         fd->fd_flags |= LL_FILE_RMTACL;
1468                 RETURN(rc);
1469             } else
1470                 RETURN(0);
1471         }
1472 #endif
1473         case LL_IOC_GETOBDCOUNT: {
1474                 int count, vallen;
1475                 struct obd_export *exp;
1476
1477                 if (copy_from_user(&count, (int *)arg, sizeof(int)))
1478                         RETURN(-EFAULT);
1479
1480                 /* get ost count when count is zero, get mdt count otherwise */
1481                 exp = count ? sbi->ll_md_exp : sbi->ll_dt_exp;
1482                 vallen = sizeof(count);
1483                 rc = obd_get_info(NULL, exp, sizeof(KEY_TGT_COUNT),
1484                                   KEY_TGT_COUNT, &vallen, &count, NULL);
1485                 if (rc) {
1486                         CERROR("get target count failed: %d\n", rc);
1487                         RETURN(rc);
1488                 }
1489
1490                 if (copy_to_user((int *)arg, &count, sizeof(int)))
1491                         RETURN(-EFAULT);
1492
1493                 RETURN(0);
1494         }
1495         case LL_IOC_PATH2FID:
1496                 if (copy_to_user((void *)arg, ll_inode2fid(inode),
1497                                      sizeof(struct lu_fid)))
1498                         RETURN(-EFAULT);
1499                 RETURN(0);
1500         case LL_IOC_GET_CONNECT_FLAGS: {
1501                 RETURN(obd_iocontrol(cmd, sbi->ll_md_exp, 0, NULL, (void*)arg));
1502         }
1503         case OBD_IOC_CHANGELOG_SEND:
1504         case OBD_IOC_CHANGELOG_CLEAR:
1505                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1506                                     sizeof(struct ioc_changelog));
1507                 RETURN(rc);
1508         case OBD_IOC_FID2PATH:
1509                 RETURN(ll_fid2path(inode, (void *)arg));
1510         case LL_IOC_HSM_REQUEST: {
1511                 struct hsm_user_request *hur;
1512                 int                      totalsize;
1513
1514                 OBD_ALLOC_PTR(hur);
1515                 if (hur == NULL)
1516                         RETURN(-ENOMEM);
1517
1518                 /* We don't know the true size yet; copy the fixed-size part */
1519                 if (copy_from_user(hur, (void *)arg, sizeof(*hur))) {
1520                         OBD_FREE_PTR(hur);
1521                         RETURN(-EFAULT);
1522                 }
1523
1524                 /* Compute the whole struct size */
1525                 totalsize = hur_len(hur);
1526                 OBD_FREE_PTR(hur);
1527
1528                 /* Make sure the size is reasonable */
1529                 if (totalsize >= MDS_MAXREQSIZE)
1530                         RETURN(-E2BIG);
1531
1532                 OBD_ALLOC_LARGE(hur, totalsize);
1533                 if (hur == NULL)
1534                         RETURN(-ENOMEM);
1535
1536                 /* Copy the whole struct */
1537                 if (copy_from_user(hur, (void *)arg, totalsize)) {
1538                         OBD_FREE_LARGE(hur, totalsize);
1539                         RETURN(-EFAULT);
1540                 }
1541
1542                 if (hur->hur_request.hr_action == HUA_RELEASE) {
1543                         const struct lu_fid *fid;
1544                         struct inode *f;
1545                         int i;
1546
1547                         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
1548                                 fid = &hur->hur_user_item[i].hui_fid;
1549                                 f = search_inode_for_lustre(inode->i_sb, fid);
1550                                 if (IS_ERR(f)) {
1551                                         rc = PTR_ERR(f);
1552                                         break;
1553                                 }
1554
1555                                 rc = ll_hsm_release(f);
1556                                 iput(f);
1557                                 if (rc != 0)
1558                                         break;
1559                         }
1560                 } else {
1561                         rc = obd_iocontrol(cmd, ll_i2mdexp(inode), totalsize,
1562                                            hur, NULL);
1563                 }
1564
1565                 OBD_FREE_LARGE(hur, totalsize);
1566
1567                 RETURN(rc);
1568         }
1569         case LL_IOC_HSM_PROGRESS: {
1570                 struct hsm_progress_kernel      hpk;
1571                 struct hsm_progress             hp;
1572
1573                 if (copy_from_user(&hp, (void *)arg, sizeof(hp)))
1574                         RETURN(-EFAULT);
1575
1576                 hpk.hpk_fid = hp.hp_fid;
1577                 hpk.hpk_cookie = hp.hp_cookie;
1578                 hpk.hpk_extent = hp.hp_extent;
1579                 hpk.hpk_flags = hp.hp_flags;
1580                 hpk.hpk_errval = hp.hp_errval;
1581                 hpk.hpk_data_version = 0;
1582
1583                 /* File may not exist in Lustre; all progress
1584                  * reported to Lustre root */
1585                 rc = obd_iocontrol(cmd, sbi->ll_md_exp, sizeof(hpk), &hpk,
1586                                    NULL);
1587                 RETURN(rc);
1588         }
1589         case LL_IOC_HSM_CT_START:
1590                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1591                         RETURN(-EPERM);
1592
1593                 rc = copy_and_ioctl(cmd, sbi->ll_md_exp, (void *)arg,
1594                                     sizeof(struct lustre_kernelcomm));
1595                 RETURN(rc);
1596
1597         case LL_IOC_HSM_COPY_START: {
1598                 struct hsm_copy *copy;
1599                 int              rc;
1600
1601                 OBD_ALLOC_PTR(copy);
1602                 if (copy == NULL)
1603                         RETURN(-ENOMEM);
1604                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1605                         OBD_FREE_PTR(copy);
1606                         RETURN(-EFAULT);
1607                 }
1608
1609                 rc = ll_ioc_copy_start(inode->i_sb, copy);
1610                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1611                         rc = -EFAULT;
1612
1613                 OBD_FREE_PTR(copy);
1614                 RETURN(rc);
1615         }
1616         case LL_IOC_HSM_COPY_END: {
1617                 struct hsm_copy *copy;
1618                 int              rc;
1619
1620                 OBD_ALLOC_PTR(copy);
1621                 if (copy == NULL)
1622                         RETURN(-ENOMEM);
1623                 if (copy_from_user(copy, (char *)arg, sizeof(*copy))) {
1624                         OBD_FREE_PTR(copy);
1625                         RETURN(-EFAULT);
1626                 }
1627
1628                 rc = ll_ioc_copy_end(inode->i_sb, copy);
1629                 if (copy_to_user((char *)arg, copy, sizeof(*copy)))
1630                         rc = -EFAULT;
1631
1632                 OBD_FREE_PTR(copy);
1633                 RETURN(rc);
1634         }
1635         default:
1636                 RETURN(obd_iocontrol(cmd, sbi->ll_dt_exp, 0, NULL,
1637                                      (void *)arg));
1638         }
1639 }
1640
1641 static loff_t ll_dir_seek(struct file *file, loff_t offset, int origin)
1642 {
1643         struct inode *inode = file->f_mapping->host;
1644         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1645         struct ll_sb_info *sbi = ll_i2sbi(inode);
1646         int api32 = ll_need_32bit_api(sbi);
1647         loff_t ret = -EINVAL;
1648         ENTRY;
1649
1650         mutex_lock(&inode->i_mutex);
1651         switch (origin) {
1652                 case SEEK_SET:
1653                         break;
1654                 case SEEK_CUR:
1655                         offset += file->f_pos;
1656                         break;
1657                 case SEEK_END:
1658                         if (offset > 0)
1659                                 GOTO(out, ret);
1660                         if (api32)
1661                                 offset += LL_DIR_END_OFF_32BIT;
1662                         else
1663                                 offset += LL_DIR_END_OFF;
1664                         break;
1665                 default:
1666                         GOTO(out, ret);
1667         }
1668
1669         if (offset >= 0 &&
1670             ((api32 && offset <= LL_DIR_END_OFF_32BIT) ||
1671              (!api32 && offset <= LL_DIR_END_OFF))) {
1672                 if (offset != file->f_pos) {
1673                         if ((api32 && offset == LL_DIR_END_OFF_32BIT) ||
1674                             (!api32 && offset == LL_DIR_END_OFF))
1675                                 fd->lfd_pos = MDS_DIR_END_OFF;
1676                         else if (api32 && sbi->ll_flags & LL_SBI_64BIT_HASH)
1677                                 fd->lfd_pos = offset << 32;
1678                         else
1679                                 fd->lfd_pos = offset;
1680                         file->f_pos = offset;
1681                         file->f_version = 0;
1682                 }
1683                 ret = offset;
1684         }
1685         GOTO(out, ret);
1686
1687 out:
1688         mutex_unlock(&inode->i_mutex);
1689         return ret;
1690 }
1691
1692 int ll_dir_open(struct inode *inode, struct file *file)
1693 {
1694         ENTRY;
1695         RETURN(ll_file_open(inode, file));
1696 }
1697
1698 int ll_dir_release(struct inode *inode, struct file *file)
1699 {
1700         ENTRY;
1701         RETURN(ll_file_release(inode, file));
1702 }
1703
1704 struct file_operations ll_dir_operations = {
1705         .llseek   = ll_dir_seek,
1706         .open     = ll_dir_open,
1707         .release  = ll_dir_release,
1708         .read     = generic_read_dir,
1709         .readdir  = ll_readdir,
1710         .unlocked_ioctl   = ll_dir_ioctl,
1711         .fsync    = ll_fsync,
1712 };