Whamcloud - gitweb
Small fix for remote client.
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002-2005 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <lustre_lite.h>
32 #include <lustre_ha.h>
33 #include <lustre_dlm.h>
34 #include <lprocfs_status.h>
35 #include <lustre_disk.h>
36 #include <lustre_param.h>
37 #include <lustre_log.h>
38 #include "llite_internal.h"
39
40 kmem_cache_t *ll_file_data_slab;
41
42 LIST_HEAD(ll_super_blocks);
43 spinlock_t ll_sb_lock = SPIN_LOCK_UNLOCKED;
44
45 extern struct address_space_operations ll_aops;
46 extern struct address_space_operations ll_dir_aops;
47
48 #ifndef log2
49 #define log2(n) ffz(~(n))
50 #endif
51
52
53 static struct ll_sb_info *ll_init_sbi(void)
54 {
55         struct ll_sb_info *sbi = NULL;
56         class_uuid_t uuid;
57         int i;
58         ENTRY;
59
60         OBD_ALLOC(sbi, sizeof(*sbi));
61         if (!sbi)
62                 RETURN(NULL);
63
64         spin_lock_init(&sbi->ll_lock);
65         spin_lock_init(&sbi->ll_lco.lco_lock);
66         INIT_LIST_HEAD(&sbi->ll_pglist);
67         if (num_physpages >> (20 - PAGE_SHIFT) < 512)
68                 sbi->ll_async_page_max = num_physpages / 2;
69         else
70                 sbi->ll_async_page_max = (num_physpages / 4) * 3;
71         sbi->ll_ra_info.ra_max_pages = min(num_physpages / 8,
72                                            SBI_DEFAULT_READAHEAD_MAX);
73         sbi->ll_ra_info.ra_max_read_ahead_whole_pages =
74                                            SBI_DEFAULT_READAHEAD_WHOLE_MAX;
75
76         INIT_LIST_HEAD(&sbi->ll_conn_chain);
77         INIT_LIST_HEAD(&sbi->ll_orphan_dentry_list);
78
79         class_generate_random_uuid(uuid);
80         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
81         CDEBUG(D_HA, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
82
83         spin_lock(&ll_sb_lock);
84         list_add_tail(&sbi->ll_list, &ll_super_blocks);
85         spin_unlock(&ll_sb_lock);
86
87         INIT_LIST_HEAD(&sbi->ll_deathrow);
88         spin_lock_init(&sbi->ll_deathrow_lock);
89         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) { 
90                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_r_hist.oh_lock);
91                 spin_lock_init(&sbi->ll_rw_extents_info.pp_extents[i].pp_w_hist.oh_lock);
92         }
93
94         RETURN(sbi);
95 }
96
97 void ll_free_sbi(struct super_block *sb)
98 {
99         struct ll_sb_info *sbi = ll_s2sbi(sb);
100         ENTRY;
101
102         if (sbi != NULL) {
103                 spin_lock(&ll_sb_lock);
104                 list_del(&sbi->ll_list);
105                 spin_unlock(&ll_sb_lock);
106                 OBD_FREE(sbi, sizeof(*sbi));
107         }
108         EXIT;
109 }
110
111 static struct dentry_operations ll_d_root_ops = {
112 #ifdef LUSTRE_KERNEL_VERSION
113         .d_compare = ll_dcompare,
114 #endif
115 };
116
117 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
118  * us to make MDS RPCs with large enough reply buffers to hold the
119  * maximum-sized (= maximum striped) EA and cookie without having to
120  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
121 static int ll_init_ea_size(struct obd_export *md_exp, struct obd_export *dt_exp)
122 {
123         struct lov_stripe_md lsm = { .lsm_magic = LOV_MAGIC };
124         __u32 valsize = sizeof(struct lov_desc);
125         int rc, easize, def_easize, cookiesize;
126         struct lov_desc desc;
127         __u32 stripes;
128         ENTRY;
129
130         rc = obd_get_info(dt_exp, strlen(KEY_LOVDESC) + 1, KEY_LOVDESC,
131                           &valsize, &desc);
132         if (rc)
133                 RETURN(rc);
134
135         stripes = min(desc.ld_tgt_count, (__u32)LOV_MAX_STRIPE_COUNT);
136         lsm.lsm_stripe_count = stripes;
137         easize = obd_size_diskmd(dt_exp, &lsm);
138
139         lsm.lsm_stripe_count = desc.ld_default_stripe_count;
140         def_easize = obd_size_diskmd(dt_exp, &lsm);
141
142         cookiesize = stripes * sizeof(struct llog_cookie);
143
144         CDEBUG(D_HA, "updating max_mdsize/max_cookiesize: %d/%d\n",
145                easize, cookiesize);
146
147         rc = md_init_ea_size(md_exp, easize, def_easize, cookiesize);
148         RETURN(rc);
149 }
150
151 static int client_common_fill_super(struct super_block *sb, 
152                                     char *md, char *dt,
153                                     uid_t nllu, gid_t nllg)
154 {
155         struct inode *root = 0;
156         struct ll_sb_info *sbi = ll_s2sbi(sb);
157         struct obd_device *obd;
158         struct lu_fid rootfid;
159         struct obd_capa *oc = NULL;
160         struct obd_statfs osfs;
161         struct ptlrpc_request *request = NULL;
162         struct lustre_handle dt_conn = {0, };
163         struct lustre_handle md_conn = {0, };
164         struct obd_connect_data *data = NULL;
165         struct lustre_md lmd;
166         obd_valid valid;
167         int size, err;
168         ENTRY;
169
170         obd = class_name2obd(md);
171         if (!obd) {
172                 CERROR("MD %s: not setup or attached\n", md);
173                 RETURN(-EINVAL);
174         }
175
176         OBD_ALLOC_PTR(data);
177         if (data == NULL)
178                 RETURN(-ENOMEM);
179
180         if (proc_lustre_fs_root) {
181                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
182                                                   dt, md);
183                 if (err < 0)
184                         CERROR("could not register mount in /proc/lustre");
185         }
186
187         /* indicate the features supported by this client */
188         data->ocd_connect_flags = OBD_CONNECT_IBITS | OBD_CONNECT_NODEVOH |
189                                   OBD_CONNECT_ACL | OBD_CONNECT_JOIN |
190                                   OBD_CONNECT_ATTRFID | OBD_CONNECT_VERSION |
191                                   OBD_CONNECT_MDS_CAPA | OBD_CONNECT_OSS_CAPA;
192         data->ocd_ibits_known = MDS_INODELOCK_FULL;
193         data->ocd_version = LUSTRE_VERSION_CODE;
194
195         if (sb->s_flags & MS_RDONLY)
196                 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
197         if (sbi->ll_flags & LL_SBI_USER_XATTR)
198                 data->ocd_connect_flags |= OBD_CONNECT_XATTR;
199
200         if (sbi->ll_flags & LL_SBI_FLOCK)
201                 sbi->ll_fop = &ll_file_operations_flock;
202         else
203                 sbi->ll_fop = &ll_file_operations;
204
205         /* real client */
206         data->ocd_connect_flags |= OBD_CONNECT_REAL;
207         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
208                 data->ocd_connect_flags &= ~OBD_CONNECT_LCL_CLIENT;
209                 data->ocd_connect_flags |= OBD_CONNECT_RMT_CLIENT;
210                 data->ocd_nllu = nllu;
211                 data->ocd_nllg = nllg;
212         } else {
213                 data->ocd_connect_flags &= ~OBD_CONNECT_RMT_CLIENT;
214                 data->ocd_connect_flags |= OBD_CONNECT_LCL_CLIENT;
215         }
216
217         err = obd_connect(NULL, &md_conn, obd, &sbi->ll_sb_uuid, data);
218         if (err == -EBUSY) {
219                 LCONSOLE_ERROR("An MDT (md %s) is performing recovery, of "
220                                "which this client is not a part.  Please wait "
221                                "for recovery to complete, abort, or "
222                                "time out.\n", md);
223                 GOTO(out, err);
224         } else if (err) {
225                 CERROR("cannot connect to %s: rc = %d\n", md, err);
226                 GOTO(out, err);
227         }
228         sbi->ll_md_exp = class_conn2export(&md_conn);
229
230         err = obd_statfs(obd, &osfs, cfs_time_current_64() - HZ);
231         if (err)
232                 GOTO(out_md, err);
233
234         size = sizeof(*data);
235         err = obd_get_info(sbi->ll_md_exp, strlen(KEY_CONN_DATA), KEY_CONN_DATA,
236                            &size, data);
237         if (err) {
238                 CERROR("Get connect data failed: %d \n", err);
239                 GOTO(out_md, err);
240         }
241
242         LASSERT(osfs.os_bsize);
243         sb->s_blocksize = osfs.os_bsize;
244         sb->s_blocksize_bits = log2(osfs.os_bsize);
245         sb->s_magic = LL_SUPER_MAGIC;
246         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
247         sbi->ll_namelen = osfs.os_namelen;
248         sbi->ll_max_rw_chunk = LL_DEFAULT_MAX_RW_CHUNK;
249
250         if ((sbi->ll_flags & LL_SBI_USER_XATTR) &&
251             !(data->ocd_connect_flags & OBD_CONNECT_XATTR)) {
252                 LCONSOLE_INFO("Disabling user_xattr feature because "
253                               "it is not supported on the server\n");
254                 sbi->ll_flags &= ~LL_SBI_USER_XATTR;
255         }
256
257         if (data->ocd_connect_flags & OBD_CONNECT_ACL) {
258 #ifdef MS_POSIXACL
259                 sb->s_flags |= MS_POSIXACL;
260 #endif
261                 sbi->ll_flags |= LL_SBI_ACL;
262         } else if (sbi->ll_flags & LL_SBI_ACL) {
263                 LCONSOLE_INFO("client wants to enable acl, but mdt not!\n");
264                 sbi->ll_flags &= ~LL_SBI_ACL;
265         }
266
267         if (data->ocd_connect_flags & OBD_CONNECT_JOIN)
268                 sbi->ll_flags |= LL_SBI_JOIN;
269
270         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
271                 if (!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT)) {
272                         /* sometimes local client claims to be remote, but mdt
273                          * will disagree when client gss not applied. */
274                         LCONSOLE_INFO("client claims to be remote, but server "
275                                       "rejected, forced to be local.\n");
276                         sbi->ll_flags &= ~LL_SBI_RMT_CLIENT;
277                 }
278         } else {
279                 if (!(data->ocd_connect_flags & OBD_CONNECT_LCL_CLIENT)) {
280                         /* with gss applied, remote client can not claim to be
281                          * local, so mdt maybe force client to be remote. */
282                         LCONSOLE_INFO("client claims to be local, but server "
283                                       "rejected, forced to be remote.\n");
284                         sbi->ll_flags |= LL_SBI_RMT_CLIENT;
285                 }
286         }
287
288         if (data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) {
289                 LCONSOLE_INFO("client enabled MDS capability!\n");
290                 sbi->ll_flags |= LL_SBI_MDS_CAPA;
291         }
292
293         if (data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA) {
294                 LCONSOLE_INFO("client enabled OSS capability!\n");
295                 sbi->ll_flags |= LL_SBI_OSS_CAPA;
296         }
297
298 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
299         /* We set sb->s_dev equal on all lustre clients in order to support
300          * NFS export clustering.  NFSD requires that the FSID be the same
301          * on all clients. */
302         /* s_dev is also used in lt_compare() to compare two fs, but that is
303          * only a node-local comparison. */
304         
305         /* XXX: this will not work with LMV */
306         sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_target_uuid.uuid,
307                                  strlen(sbi2mdc(sbi)->cl_target_uuid.uuid));
308 #endif
309
310         /* init FIDs framework */
311         err = ll_fid_md_init(sbi);
312         if (err) {
313                 CERROR("can't init FIDs framework, rc %d\n", err);
314                 GOTO(out_md, err);
315         }
316
317         obd = class_name2obd(dt);
318         if (!obd) {
319                 CERROR("DT %s: not setup or attached\n", dt);
320                 GOTO(out_md_fid, err = -ENODEV);
321         }
322
323         data->ocd_connect_flags = OBD_CONNECT_GRANT | OBD_CONNECT_VERSION |
324                                   OBD_CONNECT_REQPORTAL | OBD_CONNECT_BRW_SIZE;
325         if (sbi->ll_flags & LL_SBI_OSS_CAPA)
326                 data->ocd_connect_flags |= OBD_CONNECT_OSS_CAPA;
327
328         CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
329                "ocd_grant: %d\n", data->ocd_connect_flags,
330                data->ocd_version, data->ocd_grant);
331
332         obd->obd_upcall.onu_owner = &sbi->ll_lco;
333         obd->obd_upcall.onu_upcall = ll_ocd_update;
334         data->ocd_brw_size = PTLRPC_MAX_BRW_PAGES << PAGE_SHIFT;
335
336         err = obd_connect(NULL, &dt_conn, obd, &sbi->ll_sb_uuid, data);
337         if (err == -EBUSY) {
338                 LCONSOLE_ERROR("An OST (dt %s) is performing recovery, of which this"
339                                " client is not a part.  Please wait for recovery to "
340                                "complete, abort, or time out.\n", dt);
341                 GOTO(out, err);
342         } else if (err) {
343                 CERROR("cannot connect to %s: rc = %d\n", dt, err);
344                 GOTO(out_md, err);
345         }
346
347         sbi->ll_dt_exp = class_conn2export(&dt_conn);
348
349         spin_lock(&sbi->ll_lco.lco_lock);
350         sbi->ll_lco.lco_flags = data->ocd_connect_flags;
351         spin_unlock(&sbi->ll_lco.lco_lock);
352
353         ll_init_ea_size(sbi->ll_md_exp, sbi->ll_dt_exp);
354
355         err = obd_prep_async_page(sbi->ll_dt_exp, NULL, NULL, NULL,
356                                   0, NULL, NULL, NULL);
357         if (err < 0) {
358                 LCONSOLE_ERROR("There are no OST's in this filesystem. "
359                                "There must be at least one active OST for "
360                                "a client to start.\n");
361                 GOTO(out_dt, err);
362         }
363
364         if (!ll_async_page_slab) {
365                 ll_async_page_slab_size =
366                         size_round(sizeof(struct ll_async_page)) + err;
367                 ll_async_page_slab = kmem_cache_create("ll_async_page",
368                                                        ll_async_page_slab_size,
369                                                        0, 0, NULL, NULL);
370                 if (!ll_async_page_slab)
371                         GOTO(out_dt, -ENOMEM);
372         }
373
374         /* init FIDs framework */
375         err = ll_fid_dt_init(sbi);
376         if (err) {
377                 CERROR("can't init FIDs framework, rc %d\n", err);
378                 GOTO(out_dt, err);
379         }
380
381         err = md_getstatus(sbi->ll_md_exp, &rootfid, &oc);
382         if (err) {
383                 CERROR("cannot mds_connect: rc = %d\n", err);
384                 GOTO(out_dt_fid, err);
385         }
386         CDEBUG(D_SUPER, "rootfid "DFID"\n", PFID(&rootfid));
387         sbi->ll_root_fid = rootfid;
388
389         sb->s_op = &lustre_super_operations;
390 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
391         sb->s_export_op = &lustre_export_operations;
392 #endif
393
394         /* make root inode
395          * XXX: move this to after cbd setup? */
396         valid = OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS | OBD_MD_FLMDSCAPA;
397         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
398                 valid |= OBD_MD_FLRMTPERM;
399         else if (sbi->ll_flags & LL_SBI_ACL)
400                 valid |= OBD_MD_FLACL;
401
402         err = md_getattr(sbi->ll_md_exp, &rootfid, oc, valid, 0, &request);
403         if (oc)
404                 free_capa(oc);
405         if (err) {
406                 CERROR("md_getattr failed for root: rc = %d\n", err);
407                 GOTO(out_dt, err);
408         }
409
410         err = md_get_lustre_md(sbi->ll_md_exp, request, 
411                                REPLY_REC_OFF, sbi->ll_dt_exp, sbi->ll_md_exp, 
412                                &lmd);
413         if (err) {
414                 CERROR("failed to understand root inode md: rc = %d\n", err);
415                 ptlrpc_req_finished (request);
416                 GOTO(out_dt, err);
417         }
418
419         if (lmd.mds_capa)
420                 obd_capa_set_root(lmd.mds_capa);
421         LASSERT(fid_is_sane(&sbi->ll_root_fid));
422         root = ll_iget(sb, ll_fid_build_ino(sbi, &sbi->ll_root_fid), &lmd);
423         ptlrpc_req_finished(request);
424
425         if (root == NULL || is_bad_inode(root)) {
426                 md_free_lustre_md(sbi->ll_dt_exp, &lmd);
427                 CERROR("lustre_lite: bad iget4 for root\n");
428                 GOTO(out_root, err = -EBADF);
429         }
430
431         err = ll_close_thread_start(&sbi->ll_lcq);
432         if (err) {
433                 CERROR("cannot start close thread: rc %d\n", err);
434                 GOTO(out_root, err);
435         }
436
437         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
438            backing dev info assigned to inode mapping is used for
439            determining maximal readahead. */
440 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
441     !defined(KERNEL_HAS_AS_MAX_READAHEAD)
442         /* bug 2805 - set VM readahead to zero */
443         vm_max_readahead = vm_min_readahead = 0;
444 #endif
445
446         sb->s_root = d_alloc_root(root);
447         if (data != NULL)
448                 OBD_FREE(data, sizeof(*data));
449         sb->s_root->d_op = &ll_d_root_ops;
450         RETURN(err);
451
452 out_root:
453         if (root)
454                 iput(root);
455 out_dt_fid:
456         obd_fid_fini(sbi->ll_dt_exp);
457 out_dt:
458         obd_disconnect(sbi->ll_dt_exp);
459         sbi->ll_dt_exp = NULL;
460 out_md_fid:
461         obd_fid_fini(sbi->ll_md_exp);
462 out_md:
463         obd_disconnect(sbi->ll_md_exp);
464         sbi->ll_md_exp = NULL;
465 out:
466         if (data != NULL)
467                 OBD_FREE_PTR(data);
468         lprocfs_unregister_mountpoint(sbi);
469         RETURN(err);
470 }
471
472 int ll_get_max_mdsize(struct ll_sb_info *sbi, int *lmmsize)
473 {
474         int size, rc;
475
476         *lmmsize = obd_size_diskmd(sbi->ll_dt_exp, NULL);
477         size = sizeof(int);
478         rc = obd_get_info(sbi->ll_md_exp, strlen("max_easize"), "max_easize",
479                           &size, lmmsize);
480         if (rc)
481                 CERROR("Get max mdsize error rc %d \n", rc);
482
483         RETURN(rc);
484 }
485
486 void ll_dump_inode(struct inode *inode)
487 {
488         struct list_head *tmp;
489         int dentry_count = 0;
490
491         LASSERT(inode != NULL);
492
493         list_for_each(tmp, &inode->i_dentry)
494                 dentry_count++;
495
496         CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
497                inode, ll_i2mdexp(inode)->exp_obd->obd_name, inode->i_ino,
498                inode->i_mode, atomic_read(&inode->i_count), dentry_count);
499 }
500
501 void lustre_dump_dentry(struct dentry *dentry, int recur)
502 {
503         struct list_head *tmp;
504         int subdirs = 0;
505
506         LASSERT(dentry != NULL);
507
508         list_for_each(tmp, &dentry->d_subdirs)
509                 subdirs++;
510
511         CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
512                " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
513                dentry->d_name.len, dentry->d_name.name,
514                dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
515                dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
516                dentry->d_flags, dentry->d_fsdata, subdirs);
517         if (dentry->d_inode != NULL)
518                 ll_dump_inode(dentry->d_inode);
519
520         if (recur == 0)
521                 return;
522
523         list_for_each(tmp, &dentry->d_subdirs) {
524                 struct dentry *d = list_entry(tmp, struct dentry, d_child);
525                 lustre_dump_dentry(d, recur - 1);
526         }
527 }
528
529 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
530 void lustre_throw_orphan_dentries(struct super_block *sb)
531 {
532         struct dentry *dentry, *next;
533         struct ll_sb_info *sbi = ll_s2sbi(sb);
534
535         /* Do this to get rid of orphaned dentries. That is not really trw. */
536         list_for_each_entry_safe(dentry, next, &sbi->ll_orphan_dentry_list,
537                                  d_hash) {
538                 CWARN("found orphan dentry %.*s (%p->%p) at unmount, dumping "
539                       "before and after shrink_dcache_parent\n",
540                       dentry->d_name.len, dentry->d_name.name, dentry, next);
541                 lustre_dump_dentry(dentry, 1);
542                 shrink_dcache_parent(dentry);
543                 lustre_dump_dentry(dentry, 1);
544         }
545 }
546 #else
547 #define lustre_throw_orphan_dentries(sb)
548 #endif
549
550 static void prune_dir_dentries(struct inode *inode)
551 {
552         struct dentry *dentry, *prev = NULL;
553
554         /* due to lustre specific logic, a directory
555          * can have few dentries - a bug from VFS POV */
556 restart:
557         spin_lock(&dcache_lock);
558         if (!list_empty(&inode->i_dentry)) {
559                 dentry = list_entry(inode->i_dentry.prev,
560                                     struct dentry, d_alias);
561                 /* in order to prevent infinite loops we
562                  * break if previous dentry is busy */
563                 if (dentry != prev) {
564                         prev = dentry;
565                         dget_locked(dentry);
566                         spin_unlock(&dcache_lock);
567
568                         /* try to kill all child dentries */
569                         lock_dentry(dentry);
570                         shrink_dcache_parent(dentry);
571                         unlock_dentry(dentry);
572                         dput(dentry);
573
574                         /* now try to get rid of current dentry */
575                         d_prune_aliases(inode);
576                         goto restart;
577                 }
578         }
579         spin_unlock(&dcache_lock);
580 }
581
582 static void prune_deathrow_one(struct ll_inode_info *lli)
583 {
584         struct inode *inode = ll_info2i(lli);
585
586         /* first, try to drop any dentries - they hold a ref on the inode */
587         if (S_ISDIR(inode->i_mode))
588                 prune_dir_dentries(inode);
589         else
590                 d_prune_aliases(inode);
591
592
593         /* if somebody still uses it, leave it */
594         LASSERT(atomic_read(&inode->i_count) > 0);
595         if (atomic_read(&inode->i_count) > 1)
596                 goto out;
597
598         CDEBUG(D_INODE, "inode %lu/%u(%d) looks a good candidate for prune\n",
599                inode->i_ino,inode->i_generation, atomic_read(&inode->i_count));
600
601         /* seems nobody uses it anymore */
602         inode->i_nlink = 0;
603
604 out:
605         iput(inode);
606         return;
607 }
608
609 static void prune_deathrow(struct ll_sb_info *sbi, int try)
610 {
611         struct ll_inode_info *lli;
612         int empty;
613
614         do {
615                 if (need_resched() && try)
616                         break;
617
618                 if (try) {
619                         if (!spin_trylock(&sbi->ll_deathrow_lock))
620                                 break;
621                 } else {
622                         spin_lock(&sbi->ll_deathrow_lock);
623                 }
624
625                 empty = 1;
626                 lli = NULL;
627                 if (!list_empty(&sbi->ll_deathrow)) {
628                         lli = list_entry(sbi->ll_deathrow.next,
629                                          struct ll_inode_info,
630                                          lli_dead_list);
631                         list_del_init(&lli->lli_dead_list);
632                         if (!list_empty(&sbi->ll_deathrow))
633                                 empty = 0;
634                 }
635                 spin_unlock(&sbi->ll_deathrow_lock);
636
637                 if (lli)
638                         prune_deathrow_one(lli);
639
640         } while (empty == 0);
641 }
642
643 void client_common_put_super(struct super_block *sb)
644 {
645         struct ll_sb_info *sbi = ll_s2sbi(sb);
646         ENTRY;
647
648         ll_close_thread_shutdown(sbi->ll_lcq);
649
650         /* destroy inodes in deathrow */
651         prune_deathrow(sbi, 0);
652
653         list_del(&sbi->ll_conn_chain);
654         ll_fid_dt_fini(sbi);
655         obd_disconnect(sbi->ll_dt_exp);
656         sbi->ll_dt_exp = NULL;
657
658         lprocfs_unregister_mountpoint(sbi);
659
660         ll_fid_md_fini(sbi);
661         obd_disconnect(sbi->ll_md_exp);
662         sbi->ll_md_exp = NULL;
663
664         lustre_throw_orphan_dentries(sb);
665         EXIT;
666 }
667
668 char *ll_read_opt(const char *opt, char *data)
669 {
670         char *value;
671         char *retval;
672         ENTRY;
673
674         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
675         if (strncmp(opt, data, strlen(opt)))
676                 RETURN(NULL);
677         if ((value = strchr(data, '=')) == NULL)
678                 RETURN(NULL);
679
680         value++;
681         OBD_ALLOC(retval, strlen(value) + 1);
682         if (!retval) {
683                 CERROR("out of memory!\n");
684                 RETURN(NULL);
685         }
686
687         memcpy(retval, value, strlen(value)+1);
688         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
689         RETURN(retval);
690 }
691
692 static inline int ll_set_opt(const char *opt, char *data, int fl)
693 {
694         if (strncmp(opt, data, strlen(opt)) != 0)
695                 return(0);
696         else
697                 return(fl);
698 }
699
700 /* non-client-specific mount options are parsed in lmd_parse */
701 static int ll_options(char *options, int *flags)
702 {
703         int tmp;
704         char *s1 = options, *s2;
705         ENTRY;
706
707         if (!options) 
708                 RETURN(0);
709
710         CDEBUG(D_CONFIG, "Parsing opts %s\n", options);
711
712         while (*s1) {
713                 CDEBUG(D_SUPER, "next opt=%s\n", s1);
714                 tmp = ll_set_opt("nolock", s1, LL_SBI_NOLCK);
715                 if (tmp) {
716                         *flags |= tmp;
717                         goto next;
718                 }
719                 tmp = ll_set_opt("flock", s1, LL_SBI_FLOCK);
720                 if (tmp) {
721                         *flags |= tmp;
722                         goto next;
723                 }
724                 tmp = ll_set_opt("noflock", s1, LL_SBI_FLOCK);
725                 if (tmp) {
726                         *flags &= ~tmp;
727                         goto next;
728                 }
729                 tmp = ll_set_opt("user_xattr", s1, LL_SBI_USER_XATTR);
730                 if (tmp) {
731                         *flags |= tmp;
732                         goto next;
733                 }
734                 tmp = ll_set_opt("nouser_xattr", s1, LL_SBI_USER_XATTR);
735                 if (tmp) {
736                         *flags &= ~tmp;
737                         goto next;
738                 }
739                 tmp = ll_set_opt("acl", s1, LL_SBI_ACL);
740                 if (tmp) {
741                         /* Ignore deprecated mount option.  The client will
742                          * always try to mount with ACL support, whether this
743                          * is used depends on whether server supports it. */
744                         goto next;
745                 }
746                 tmp = ll_set_opt("noacl", s1, LL_SBI_ACL);
747                 if (tmp) {
748                         goto next;
749                 }
750                 tmp = ll_set_opt("remote_client", s1, LL_SBI_RMT_CLIENT);
751                 if (tmp) {
752                         *flags |= tmp;
753                         goto next;
754                 }
755
756                 LCONSOLE_ERROR("Unknown option '%s', won't mount.\n", s1);
757                 RETURN(-EINVAL);
758
759 next:
760                 /* Find next opt */
761                 s2 = strchr(s1, ',');
762                 if (s2 == NULL)
763                         break;
764                 s1 = s2 + 1;
765         }
766         RETURN(0);
767 }
768
769 void ll_lli_init(struct ll_inode_info *lli)
770 {
771         sema_init(&lli->lli_open_sem, 1);
772         sema_init(&lli->lli_size_sem, 1);
773         sema_init(&lli->lli_write_sem, 1);
774         lli->lli_flags = 0;
775         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
776         spin_lock_init(&lli->lli_lock);
777         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
778         INIT_LIST_HEAD(&lli->lli_close_list);
779         lli->lli_inode_magic = LLI_INODE_MAGIC;
780         sema_init(&lli->lli_och_sem, 1);
781         lli->lli_mds_read_och = lli->lli_mds_write_och = NULL;
782         lli->lli_mds_exec_och = NULL;
783         lli->lli_open_fd_read_count = lli->lli_open_fd_write_count = 0;
784         lli->lli_open_fd_exec_count = 0;
785         INIT_LIST_HEAD(&lli->lli_dead_list);
786         INIT_LIST_HEAD(&lli->lli_oss_capas);
787         lli->lli_remote_perms = NULL;
788         sema_init(&lli->lli_rmtperm_sem, 1);
789 }
790
791 /* COMPAT_146 */
792 #define MDCDEV "mdc_dev"
793 static int old_lustre_process_log(struct super_block *sb, char *newprofile,
794                                   struct config_llog_instance *cfg)
795 {
796         struct lustre_sb_info *lsi = s2lsi(sb);
797         struct obd_device *obd;
798         struct lustre_handle mdc_conn = {0, };
799         struct obd_export *exp;
800         char *ptr, *mdt, *profile;
801         char niduuid[10] = "mdtnid0";
802         class_uuid_t uuid;
803         struct obd_uuid mdc_uuid;
804         struct llog_ctxt *ctxt;
805         struct obd_connect_data ocd = { 0 };
806         lnet_nid_t nid;
807         int i, rc = 0, recov_bk = 1, failnodes = 0;
808         ENTRY;
809
810         class_generate_random_uuid(uuid);
811         class_uuid_unparse(uuid, &mdc_uuid);
812         CDEBUG(D_HA, "generated uuid: %s\n", mdc_uuid.uuid);
813         
814         /* Figure out the old mdt and profile name from new-style profile
815            ("lustre" from "mds/lustre-client") */
816         mdt = newprofile;
817         profile = strchr(mdt, '/');
818         if (profile == NULL) {
819                 CDEBUG(D_CONFIG, "Can't find MDT name in %s\n", newprofile);
820                 GOTO(out, rc = -EINVAL);
821         }
822         *profile = '\0';
823         profile++;
824         ptr = strrchr(profile, '-');
825         if (ptr == NULL) {
826                 CDEBUG(D_CONFIG, "Can't find client name in %s\n", newprofile);
827                 GOTO(out, rc = -EINVAL);
828         }
829         *ptr = '\0';
830
831         LCONSOLE_WARN("This looks like an old mount command; I will try to "
832                       "contact MDT '%s' for profile '%s'\n", mdt, profile);
833
834         /* Use nids from mount line: uml1,1@elan:uml2,2@elan:/lustre */
835         i = 0;
836         ptr = lsi->lsi_lmd->lmd_dev;
837         while (class_parse_nid(ptr, &nid, &ptr) == 0) {
838                 rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid, 0,0,0);
839                 i++;
840                 /* Stop at the first failover nid */
841                 if (*ptr == ':') 
842                         break;
843         }
844         if (i == 0) {
845                 CERROR("No valid MDT nids found.\n");
846                 GOTO(out, rc = -EINVAL);
847         }
848         failnodes++;
849
850         rc = do_lcfg(MDCDEV, 0, LCFG_ATTACH, LUSTRE_MDC_NAME, mdc_uuid.uuid, 0, 0);
851         if (rc < 0)
852                 GOTO(out_del_uuid, rc);
853
854         rc = do_lcfg(MDCDEV, 0, LCFG_SETUP, mdt, niduuid, 0, 0);
855         if (rc < 0) {
856                 LCONSOLE_ERROR("I couldn't establish a connection with the MDT."
857                                " Check that the MDT host NID is correct and the"
858                                " networks are up.\n");
859                 GOTO(out_detach, rc);
860         }
861
862         obd = class_name2obd(MDCDEV);
863         if (obd == NULL)
864                 GOTO(out_cleanup, rc = -EINVAL);
865
866         /* Add any failover nids */
867         while (*ptr == ':') {
868                 /* New failover node */
869                 sprintf(niduuid, "mdtnid%d", failnodes);
870                 i = 0;
871                 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
872                         i++;
873                         rc = do_lcfg(MDCDEV, nid, LCFG_ADD_UUID, niduuid,0,0,0);
874                         if (rc)
875                                 CERROR("Add uuid for %s failed %d\n", 
876                                        libcfs_nid2str(nid), rc);
877                         if (*ptr == ':') 
878                                 break;
879                 }
880                 if (i > 0) {
881                         rc = do_lcfg(MDCDEV, 0, LCFG_ADD_CONN, niduuid, 0, 0,0);
882                         if (rc) 
883                                 CERROR("Add conn for %s failed %d\n", 
884                                        libcfs_nid2str(nid), rc);
885                         failnodes++;
886                 } else {
887                         /* at ":/fsname" */
888                         break;
889                 }
890         }
891
892         /* Try all connections, but only once. */
893         rc = obd_set_info_async(obd->obd_self_export,
894                                 strlen("init_recov_bk"), "init_recov_bk",
895                                 sizeof(recov_bk), &recov_bk, NULL);
896         if (rc)
897                 GOTO(out_cleanup, rc);
898
899         /* If we don't have this then an ACL MDS will refuse the connection */
900         ocd.ocd_connect_flags = OBD_CONNECT_ACL;
901
902         rc = obd_connect(NULL, &mdc_conn, obd, &mdc_uuid, &ocd);
903         if (rc) {
904                 CERROR("cannot connect to %s: rc = %d\n", mdt, rc);
905                 GOTO(out_cleanup, rc);
906         }
907
908         exp = class_conn2export(&mdc_conn);
909
910         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
911         
912         cfg->cfg_flags |= CFG_F_COMPAT146;
913
914 #if 1
915         rc = class_config_parse_llog(ctxt, profile, cfg);
916 #else
917         /*
918          * For debugging, it's useful to just dump the log
919          */
920         rc = class_config_dump_llog(ctxt, profile, cfg);
921 #endif
922         switch (rc) {
923         case 0: {
924                 /* Set the caller's profile name to the old-style */
925                 memcpy(newprofile, profile, strlen(profile) + 1);
926                 break;
927         }
928         case -EINVAL:
929                 LCONSOLE_ERROR("%s: The configuration '%s' could not be read "
930                                "from the MDT '%s'.  Make sure this client and "
931                                "the MDT are running compatible versions of "
932                                "Lustre.\n",
933                                obd->obd_name, profile, mdt);
934                 /* fall through */
935         default:
936                 LCONSOLE_ERROR("%s: The configuration '%s' could not be read "
937                                "from the MDT '%s'.  This may be the result of "
938                                "communication errors between the client and "
939                                "the MDT, or if the MDT is not running.\n",
940                                obd->obd_name, profile, mdt);
941                 break;
942         }
943
944         /* We don't so much care about errors in cleaning up the config llog
945          * connection, as we have already read the config by this point. */
946         obd_disconnect(exp);
947
948 out_cleanup:
949         do_lcfg(MDCDEV, 0, LCFG_CLEANUP, 0, 0, 0, 0);
950
951 out_detach:
952         do_lcfg(MDCDEV, 0, LCFG_DETACH, 0, 0, 0, 0);
953
954 out_del_uuid:
955         /* class_add_uuid adds a nid even if the same uuid exists; we might
956            delete any copy here.  So they all better match. */
957         for (i = 0; i < failnodes; i++) {
958                 sprintf(niduuid, "mdtnid%d", i);
959                 do_lcfg(MDCDEV, 0, LCFG_DEL_UUID, niduuid, 0, 0, 0);
960         }
961         /* class_import_put will get rid of the additional connections */
962 out:
963         RETURN(rc);
964 }
965 /* end COMPAT_146 */
966
967 int ll_fill_super(struct super_block *sb)
968 {
969         struct lustre_profile *lprof;
970         struct lustre_sb_info *lsi = s2lsi(sb);
971         struct ll_sb_info *sbi;
972         char  *dt = NULL, *md = NULL;
973         char  *profilenm = get_profile_name(sb);
974         struct config_llog_instance cfg;
975         char   ll_instance[sizeof(sb) * 2 + 1];
976         int    err;
977         ENTRY;
978
979         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
980
981         cfs_module_get();
982
983         /* client additional sb info */
984         lsi->lsi_llsbi = sbi = ll_init_sbi();
985         if (!sbi) {
986                 cfs_module_put();
987                 RETURN(-ENOMEM);
988         }
989
990         err = ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
991         if (err) 
992                 GOTO(out_free, err);
993
994         /* Generate a string unique to this super, in case some joker tries
995            to mount the same fs at two mount points.
996            Use the address of the super itself.*/
997         sprintf(ll_instance, "%p", sb);
998         cfg.cfg_instance = ll_instance;
999         cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
1000         cfg.cfg_last_idx = 0;
1001
1002         /* set up client obds */
1003         err = lustre_process_log(sb, profilenm, &cfg);
1004         /* COMPAT_146 */
1005         if (err < 0) {
1006                 char *oldname;
1007                 int rc, oldnamelen;
1008                 oldnamelen = strlen(profilenm) + 1;
1009                 /* Temp storage for 1.4.6 profile name */
1010                 OBD_ALLOC(oldname, oldnamelen);
1011                 if (oldname) {
1012                         memcpy(oldname, profilenm, oldnamelen); 
1013                         rc = old_lustre_process_log(sb, oldname, &cfg);
1014                         if (rc >= 0) {
1015                                 /* That worked - update the profile name 
1016                                    permanently */
1017                                 err = rc;
1018                                 OBD_FREE(lsi->lsi_lmd->lmd_profile, 
1019                                          strlen(lsi->lsi_lmd->lmd_profile) + 1);
1020                                 OBD_ALLOC(lsi->lsi_lmd->lmd_profile, 
1021                                          strlen(oldname) + 1);
1022                                 if (!lsi->lsi_lmd->lmd_profile) {
1023                                         OBD_FREE(oldname, oldnamelen);
1024                                         GOTO(out_free, err = -ENOMEM);
1025                                 }
1026                                 memcpy(lsi->lsi_lmd->lmd_profile, oldname,
1027                                        strlen(oldname) + 1); 
1028                                 profilenm = get_profile_name(sb);
1029                         }
1030                         OBD_FREE(oldname, oldnamelen);
1031                 }
1032         }
1033         /* end COMPAT_146 */
1034         if (err < 0) {
1035                 CERROR("Unable to process log: %d\n", err);
1036                 GOTO(out_free, err);
1037         }
1038
1039         lprof = class_get_profile(profilenm);
1040         if (lprof == NULL) {
1041                 LCONSOLE_ERROR("The client profile '%s' could not be read "
1042                                "from the MGS.  Does that filesystem exist?\n",
1043                                profilenm);
1044                 GOTO(out_free, err = -EINVAL);
1045         }
1046         CDEBUG(D_CONFIG, "Found profile %s: mdc=%s osc=%s\n", profilenm,
1047                lprof->lp_md, lprof->lp_dt);
1048
1049         OBD_ALLOC(dt, strlen(lprof->lp_dt) +
1050                   strlen(ll_instance) + 2);
1051         if (!dt)
1052                 GOTO(out_free, err = -ENOMEM);
1053         sprintf(dt, "%s-%s", lprof->lp_dt, ll_instance);
1054
1055         OBD_ALLOC(md, strlen(lprof->lp_md) +
1056                   strlen(ll_instance) + 2);
1057         if (!md)
1058                 GOTO(out_free, err = -ENOMEM);
1059         sprintf(md, "%s-%s", lprof->lp_md, ll_instance);
1060
1061         /* connections, registrations, sb setup */
1062         err = client_common_fill_super(sb, md, dt,
1063                                        lsi->lsi_lmd->lmd_nllu,
1064                                        lsi->lsi_lmd->lmd_nllg);
1065
1066 out_free:
1067         if (md)
1068                 OBD_FREE(md, strlen(md) + 1);
1069         if (dt)
1070                 OBD_FREE(dt, strlen(dt) + 1);
1071         if (err) 
1072                 ll_put_super(sb);
1073         RETURN(err);
1074 } /* ll_fill_super */
1075
1076
1077 void ll_put_super(struct super_block *sb)
1078 {
1079         struct config_llog_instance cfg;
1080         char   ll_instance[sizeof(sb) * 2 + 1];
1081         struct obd_device *obd;
1082         struct lustre_sb_info *lsi = s2lsi(sb);
1083         struct ll_sb_info *sbi = ll_s2sbi(sb);
1084         char *profilenm = get_profile_name(sb);
1085         int force = 1, next;
1086         ENTRY;
1087
1088         CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
1089
1090         sprintf(ll_instance, "%p", sb);
1091         cfg.cfg_instance = ll_instance;
1092         lustre_end_log(sb, NULL, &cfg);
1093         
1094         if (sbi->ll_md_exp) {
1095                 obd = class_exp2obd(sbi->ll_md_exp);
1096                 if (obd) 
1097                         force = obd->obd_no_recov;
1098         }
1099         
1100         /* We need to set force before the lov_disconnect in 
1101            lustre_common_put_super, since l_d cleans up osc's as well. */
1102         if (force) {
1103                 next = 0;
1104                 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid,
1105                                                      &next)) != NULL) {
1106                         obd->obd_force = force;
1107                 }
1108         }                       
1109
1110         if (sbi->ll_lcq) {
1111                 /* Only if client_common_fill_super succeeded */
1112                 client_common_put_super(sb);
1113         }
1114         next = 0;
1115         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
1116                 class_manual_cleanup(obd);
1117         }
1118
1119         if (profilenm)
1120                 class_del_profile(profilenm);
1121
1122         ll_free_sbi(sb);
1123         lsi->lsi_llsbi = NULL;
1124
1125         lustre_common_put_super(sb);
1126
1127         LCONSOLE_WARN("client %s umount complete\n", ll_instance);
1128         
1129         cfs_module_put();
1130
1131         EXIT;
1132 } /* client_put_super */
1133
1134 #ifdef HAVE_REGISTER_CACHE
1135 #include <linux/cache_def.h>
1136 #ifdef HAVE_CACHE_RETURN_INT
1137 static int
1138 #else
1139 static void
1140 #endif
1141 ll_shrink_cache(int priority, unsigned int gfp_mask)
1142 {
1143         struct ll_sb_info *sbi;
1144         int count = 0;
1145
1146         list_for_each_entry(sbi, &ll_super_blocks, ll_list)
1147                 count += llap_shrink_cache(sbi, priority);
1148
1149 #ifdef HAVE_CACHE_RETURN_INT
1150         return count;
1151 #endif
1152 }
1153
1154 struct cache_definition ll_cache_definition = {
1155         .name = "llap_cache",
1156         .shrink = ll_shrink_cache
1157 };
1158 #endif /* HAVE_REGISTER_CACHE */
1159
1160 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
1161 {
1162         struct inode *inode = NULL;
1163         /* NOTE: we depend on atomic igrab() -bzzz */
1164         lock_res_and_lock(lock);
1165         if (lock->l_ast_data) {
1166                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
1167                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
1168                         inode = igrab(lock->l_ast_data);
1169                 } else {
1170                         inode = lock->l_ast_data;
1171                         ldlm_lock_debug(NULL, inode->i_state & I_FREEING ?
1172                                                 D_INFO : D_WARNING,
1173                                         lock, __FILE__, __func__, __LINE__,
1174                                         "l_ast_data %p is bogus: magic %08x",
1175                                         lock->l_ast_data, lli->lli_inode_magic);
1176                         inode = NULL;
1177                 }
1178         }
1179         unlock_res_and_lock(lock);
1180         return inode;
1181 }
1182
1183 static int null_if_equal(struct ldlm_lock *lock, void *data)
1184 {
1185         if (data == lock->l_ast_data) {
1186                 lock->l_ast_data = NULL;
1187
1188                 if (lock->l_req_mode != lock->l_granted_mode)
1189                         LDLM_ERROR(lock,"clearing inode with ungranted lock");
1190         }
1191
1192         return LDLM_ITER_CONTINUE;
1193 }
1194
1195 void ll_clear_inode(struct inode *inode)
1196 {
1197         struct ll_inode_info *lli = ll_i2info(inode);
1198         struct ll_sb_info *sbi = ll_i2sbi(inode);
1199         ENTRY;
1200
1201         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1202                inode->i_generation, inode);
1203
1204         ll_i2info(inode)->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
1205         md_change_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
1206                          null_if_equal, inode);
1207
1208         LASSERT(!lli->lli_open_fd_write_count);
1209         LASSERT(!lli->lli_open_fd_read_count);
1210         LASSERT(!lli->lli_open_fd_exec_count);
1211
1212         if (lli->lli_mds_write_och)
1213                 ll_md_real_close(inode, FMODE_WRITE);
1214         if (lli->lli_mds_exec_och) {
1215                 if (!FMODE_EXEC)
1216                         CERROR("No FMODE exec, bug exec och is present for "
1217                                "inode %ld\n", inode->i_ino);
1218                 ll_md_real_close(inode, FMODE_EXEC);
1219         }
1220         if (lli->lli_mds_read_och)
1221                 ll_md_real_close(inode, FMODE_READ);
1222
1223         if (lli->lli_smd) {
1224                 obd_change_cbdata(sbi->ll_dt_exp, lli->lli_smd,
1225                                   null_if_equal, inode);
1226
1227                 obd_free_memmd(sbi->ll_dt_exp, &lli->lli_smd);
1228                 lli->lli_smd = NULL;
1229         }
1230
1231         if (lli->lli_symlink_name) {
1232                 OBD_FREE(lli->lli_symlink_name,
1233                          strlen(lli->lli_symlink_name) + 1);
1234                 lli->lli_symlink_name = NULL;
1235         }
1236
1237         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1238                 LASSERT(lli->lli_posix_acl == NULL);
1239                 if (lli->lli_remote_perms) {
1240                         free_rmtperm_hash(lli->lli_remote_perms);
1241                         lli->lli_remote_perms = NULL;
1242                 }
1243         }
1244 #ifdef CONFIG_FS_POSIX_ACL
1245         else if (lli->lli_posix_acl) {
1246                 LASSERT(atomic_read(&lli->lli_posix_acl->a_refcount) == 1);
1247                 LASSERT(lli->lli_remote_perms == NULL);
1248                 posix_acl_release(lli->lli_posix_acl);
1249                 lli->lli_posix_acl = NULL;
1250         }
1251 #endif
1252         lli->lli_inode_magic = LLI_INODE_DEAD;
1253
1254         spin_lock(&sbi->ll_deathrow_lock);
1255         list_del_init(&lli->lli_dead_list);
1256         spin_unlock(&sbi->ll_deathrow_lock);
1257
1258         ll_clear_inode_capas(inode);
1259
1260         EXIT;
1261 }
1262
1263 int ll_md_setattr(struct inode *inode, struct md_op_data *op_data)
1264 {
1265         struct lustre_md md;
1266         struct ll_sb_info *sbi = ll_i2sbi(inode);
1267         struct ptlrpc_request *request = NULL;
1268         int rc;
1269         ENTRY;
1270         
1271         op_data = ll_prep_md_op_data(op_data, inode, NULL, NULL, 0, 0);
1272         rc = md_setattr(sbi->ll_md_exp, op_data, NULL, 0, NULL, 0, &request);
1273         if (rc) {
1274                 ptlrpc_req_finished(request);
1275                 if (rc == -ENOENT) {
1276                         inode->i_nlink = 0;
1277                         /* Unlinked special device node? Or just a race?
1278                          * Pretend we done everything. */
1279                         if (!S_ISREG(inode->i_mode) &&
1280                             !S_ISDIR(inode->i_mode))
1281                                 rc = inode_setattr(inode, &op_data->attr);
1282                 } else if (rc != -EPERM && rc != -EACCES) {
1283                         CERROR("md_setattr fails: rc = %d\n", rc);
1284                 }
1285                 RETURN(rc);
1286         }
1287
1288         rc = md_get_lustre_md(sbi->ll_md_exp, request, REPLY_REC_OFF,
1289                               sbi->ll_dt_exp, sbi->ll_md_exp, &md);
1290         if (rc) {
1291                 ptlrpc_req_finished(request);
1292                 RETURN(rc);
1293         }
1294
1295         /* We call inode_setattr to adjust timestamps.
1296          * If there is at least some data in file, we cleared ATTR_SIZE
1297          * above to avoid invoking vmtruncate, otherwise it is important
1298          * to call vmtruncate in inode_setattr to update inode->i_size
1299          * (bug 6196) */
1300         rc = inode_setattr(inode, &op_data->attr);
1301
1302         /* Extract epoch data if obtained. */
1303         memcpy(&op_data->handle, &md.body->handle, sizeof(op_data->handle));
1304         op_data->ioepoch = md.body->ioepoch;
1305         
1306         ll_update_inode(inode, &md);
1307         ptlrpc_req_finished(request);
1308
1309         RETURN(rc);
1310 }
1311
1312 /* Close IO epoch and send Size-on-MDS attribute update. */
1313 static int ll_setattr_done_writing(struct inode *inode,
1314                                    struct md_op_data *op_data)
1315 {
1316         struct ll_inode_info *lli = ll_i2info(inode);
1317         int rc = 0;
1318         ENTRY;
1319         
1320         LASSERT(op_data != NULL);
1321         if (!S_ISREG(inode->i_mode))
1322                 RETURN(0);
1323
1324         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID" for truncate\n",
1325                op_data->ioepoch, PFID(&lli->lli_fid));
1326
1327         op_data->flags = MF_EPOCH_CLOSE | MF_SOM_CHANGE;
1328         /* XXX: pass och here for the recovery purpose. */
1329         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
1330         if (rc == -EAGAIN) {
1331                 /* MDS has instructed us to obtain Size-on-MDS attribute
1332                  * from OSTs and send setattr to back to MDS. */
1333                 rc = ll_sizeonmds_update(inode, &op_data->handle);
1334         } else if (rc) {
1335                 CERROR("inode %lu mdc truncate failed: rc = %d\n",
1336                        inode->i_ino, rc);
1337         }
1338         RETURN(rc);
1339 }
1340
1341 /* If this inode has objects allocated to it (lsm != NULL), then the OST
1342  * object(s) determine the file size and mtime.  Otherwise, the MDS will
1343  * keep these values until such a time that objects are allocated for it.
1344  * We do the MDS operations first, as it is checking permissions for us.
1345  * We don't to the MDS RPC if there is nothing that we want to store there,
1346  * otherwise there is no harm in updating mtime/atime on the MDS if we are
1347  * going to do an RPC anyways.
1348  *
1349  * If we are doing a truncate, we will send the mtime and ctime updates
1350  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
1351  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
1352  * at the same time.
1353  */
1354 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1355 {
1356         struct ll_inode_info *lli = ll_i2info(inode);
1357         struct lov_stripe_md *lsm = lli->lli_smd;
1358         struct ll_sb_info *sbi = ll_i2sbi(inode);
1359         struct md_op_data *op_data = NULL;
1360         int ia_valid = attr->ia_valid;
1361         int rc = 0, rc1 = 0;
1362         ENTRY;
1363
1364         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
1365                attr->ia_valid);
1366         lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_SETATTR);
1367
1368         if (ia_valid & ATTR_SIZE) {
1369                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1370                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1371                                attr->ia_size, ll_file_maxbytes(inode));
1372                         RETURN(-EFBIG);
1373                 }
1374
1375                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1376         }
1377
1378         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1379         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1380                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1381                         RETURN(-EPERM);
1382         }
1383
1384         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1385         if (attr->ia_valid & ATTR_CTIME) {
1386                 attr->ia_ctime = CURRENT_TIME;
1387                 attr->ia_valid |= ATTR_CTIME_SET;
1388         }
1389         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1390                 attr->ia_atime = CURRENT_TIME;
1391                 attr->ia_valid |= ATTR_ATIME_SET;
1392         }
1393         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1394                 attr->ia_mtime = CURRENT_TIME;
1395                 attr->ia_valid |= ATTR_MTIME_SET;
1396         }
1397         if ((attr->ia_valid & ATTR_CTIME) && !(attr->ia_valid & ATTR_MTIME)) {
1398                 /* To avoid stale mtime on mds, obtain it from ost and send 
1399                    to mds. */
1400                 rc = ll_glimpse_size(inode, 0);
1401                 if (rc) 
1402                         RETURN(rc);
1403                 
1404                 attr->ia_valid |= ATTR_MTIME_SET | ATTR_MTIME;
1405                 attr->ia_mtime = inode->i_mtime;
1406         }
1407
1408         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1409                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1410                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1411                        CURRENT_SECONDS);
1412
1413         /* NB: ATTR_SIZE will only be set after this point if the size
1414          * resides on the MDS, ie, this file has no objects. */
1415         if (lsm)
1416                 attr->ia_valid &= ~ATTR_SIZE;
1417
1418         /* If only OST attributes being set on objects, don't do MDS RPC.
1419          * In that case, we need to check permissions and update the local
1420          * inode ourselves so we can call obdo_from_inode() always. */
1421         if (ia_valid & (lsm ? ~(ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
1422                 OBD_ALLOC_PTR(op_data);
1423                 if (op_data == NULL)
1424                         RETURN(-ENOMEM);
1425
1426                 memcpy(&op_data->attr, attr, sizeof(*attr));
1427
1428                 /* Open epoch for truncate. */
1429                 if (ia_valid & ATTR_SIZE)
1430                         op_data->flags = MF_EPOCH_OPEN;
1431                 rc = ll_md_setattr(inode, op_data);
1432                 if (rc)
1433                         GOTO(out, rc);
1434
1435                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for truncate\n",
1436                        op_data->ioepoch, PFID(&lli->lli_fid));
1437
1438                 if (!lsm || !S_ISREG(inode->i_mode)) {
1439                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1440                         GOTO(out, rc = 0);
1441                 }
1442         } else {
1443                 /* The OST doesn't check permissions, but the alternative is
1444                  * a gratuitous RPC to the MDS.  We already rely on the client
1445                  * to do read/write/truncate permission checks, so is mtime OK?
1446                  */
1447                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1448                         /* from sys_utime() */
1449                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1450                                 if (current->fsuid != inode->i_uid &&
1451                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1452                                         RETURN(rc);
1453                         } else {
1454                                 /* from inode_change_ok() */
1455                                 if (current->fsuid != inode->i_uid &&
1456                                     !capable(CAP_FOWNER))
1457                                         RETURN(-EPERM);
1458                         }
1459                 }
1460
1461                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1462                 rc = inode_setattr(inode, attr);
1463         }
1464
1465         /* We really need to get our PW lock before we change inode->i_size.
1466          * If we don't we can race with other i_size updaters on our node, like
1467          * ll_file_read.  We can also race with i_size propogation to other
1468          * nodes through dirtying and writeback of final cached pages.  This
1469          * last one is especially bad for racing o_append users on other
1470          * nodes. */
1471         if (ia_valid & ATTR_SIZE) {
1472                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1473                                                            OBD_OBJECT_EOF } };
1474                 struct lustre_handle lockh = { 0 };
1475                 int err, ast_flags = 0;
1476                 /* XXX when we fix the AST intents to pass the discard-range
1477                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1478                  * XXX here. */
1479                 if (attr->ia_size == 0)
1480                         ast_flags = LDLM_AST_DISCARD_DATA;
1481
1482                 UNLOCK_INODE_MUTEX(inode);
1483                 UP_WRITE_I_ALLOC_SEM(inode);
1484                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1485                                     ast_flags);
1486 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1487                 DOWN_WRITE_I_ALLOC_SEM(inode);
1488                 LOCK_INODE_MUTEX(inode);
1489 #else
1490                 LOCK_INODE_MUTEX(inode);
1491                 DOWN_WRITE_I_ALLOC_SEM(inode);
1492 #endif
1493                 if (rc != 0)
1494                         GOTO(out, rc);
1495
1496                 /* Only ll_inode_size_lock is taken at this level.
1497                  * lov_stripe_lock() is grabbed by ll_truncate() only over
1498                  * call to obd_adjust_kms().  If vmtruncate returns 0, then
1499                  * ll_truncate dropped ll_inode_size_lock() */
1500                 ll_inode_size_lock(inode, 0);
1501                 rc = vmtruncate(inode, attr->ia_size);
1502                 if (rc != 0) {
1503                         LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
1504                         ll_inode_size_unlock(inode, 0);
1505                 }
1506
1507                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1508                 if (err) {
1509                         CERROR("ll_extent_unlock failed: %d\n", err);
1510                         if (!rc)
1511                                 rc = err;
1512                 }
1513         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1514                 obd_flag flags;
1515                 struct obd_info oinfo = { { { 0 } } };
1516                 struct obdo *oa = obdo_alloc();
1517
1518                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1519                        inode->i_ino, LTIME_S(attr->ia_mtime));
1520
1521                 if (oa) {
1522                         oa->o_id = lsm->lsm_object_id;
1523                         oa->o_gr = lsm->lsm_object_gr;
1524                         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1525
1526                         flags = OBD_MD_FLTYPE | OBD_MD_FLATIME |
1527                                 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1528                                 OBD_MD_FLFID | OBD_MD_FLGENER | 
1529                                 OBD_MD_FLGROUP;
1530
1531                         obdo_from_inode(oa, inode, flags);
1532
1533                         oinfo.oi_oa = oa;
1534                         oinfo.oi_md = lsm;
1535
1536                         /* XXX: this looks unnecessary now. */
1537                         rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
1538                         if (rc)
1539                                 CERROR("obd_setattr_async fails: rc=%d\n", rc);
1540                         obdo_free(oa);
1541                 } else {
1542                         rc = -ENOMEM;
1543                 }
1544         }
1545         EXIT;
1546 out:
1547         if (op_data) {
1548                 if (op_data->ioepoch) {
1549                         rc1 = ll_setattr_done_writing(inode, op_data);
1550                 }
1551                 ll_finish_md_op_data(op_data);
1552         }
1553         return rc ? rc : rc1;
1554 }
1555
1556 int ll_setattr(struct dentry *de, struct iattr *attr)
1557 {
1558         if ((attr->ia_valid & (ATTR_CTIME|ATTR_SIZE|ATTR_MODE)) ==
1559             (ATTR_CTIME|ATTR_SIZE|ATTR_MODE))
1560                 attr->ia_valid |= MDS_OPEN_OWNEROVERRIDE;
1561
1562         return ll_setattr_raw(de->d_inode, attr);
1563 }
1564
1565 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1566                        __u64 max_age)
1567 {
1568         struct ll_sb_info *sbi = ll_s2sbi(sb);
1569         struct obd_statfs obd_osfs;
1570         int rc;
1571         ENTRY;
1572
1573         rc = obd_statfs(class_exp2obd(sbi->ll_md_exp), osfs, max_age);
1574         if (rc) {
1575                 CERROR("md_statfs fails: rc = %d\n", rc);
1576                 RETURN(rc);
1577         }
1578
1579         osfs->os_type = sb->s_magic;
1580
1581         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1582                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1583
1584         rc = obd_statfs_rqset(class_exp2obd(sbi->ll_dt_exp),
1585                               &obd_osfs, max_age);
1586         if (rc) {
1587                 CERROR("obd_statfs fails: rc = %d\n", rc);
1588                 RETURN(rc);
1589         }
1590
1591         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1592                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1593                obd_osfs.os_files);
1594
1595         osfs->os_blocks = obd_osfs.os_blocks;
1596         osfs->os_bfree = obd_osfs.os_bfree;
1597         osfs->os_bavail = obd_osfs.os_bavail;
1598
1599         /* If we don't have as many objects free on the OST as inodes
1600          * on the MDS, we reduce the total number of inodes to
1601          * compensate, so that the "inodes in use" number is correct.
1602          */
1603         if (obd_osfs.os_ffree < osfs->os_ffree) {
1604                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1605                         obd_osfs.os_ffree;
1606                 osfs->os_ffree = obd_osfs.os_ffree;
1607         }
1608
1609         RETURN(rc);
1610 }
1611
1612 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1613 {
1614         struct obd_statfs osfs;
1615         int rc;
1616
1617         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1618         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1619
1620         /* For now we will always get up-to-date statfs values, but in the
1621          * future we may allow some amount of caching on the client (e.g.
1622          * from QOS or lprocfs updates). */
1623         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - 1);
1624         if (rc)
1625                 return rc;
1626
1627         statfs_unpack(sfs, &osfs);
1628
1629         /* We need to downshift for all 32-bit kernels, because we can't
1630          * tell if the kernel is being called via sys_statfs64() or not.
1631          * Stop before overflowing f_bsize - in which case it is better
1632          * to just risk EOVERFLOW if caller is using old sys_statfs(). */
1633         if (sizeof(long) < 8) {
1634                 while (osfs.os_blocks > ~0UL && sfs->f_bsize < 0x40000000) {
1635                         sfs->f_bsize <<= 1;
1636
1637                         osfs.os_blocks >>= 1;
1638                         osfs.os_bfree >>= 1;
1639                         osfs.os_bavail >>= 1;
1640                 }
1641         }
1642
1643         sfs->f_blocks = osfs.os_blocks;
1644         sfs->f_bfree = osfs.os_bfree;
1645         sfs->f_bavail = osfs.os_bavail;
1646
1647         return 0;
1648 }
1649
1650 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
1651 {
1652         struct ll_inode_info *lli;
1653         struct lov_stripe_md *lsm;
1654
1655         lli = ll_i2info(inode);
1656         LASSERT(lli->lli_size_sem_owner != current);
1657         down(&lli->lli_size_sem);
1658         LASSERT(lli->lli_size_sem_owner == NULL);
1659         lli->lli_size_sem_owner = current;
1660         lsm = lli->lli_smd;
1661         LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
1662                  lsm, lock_lsm);
1663         if (lock_lsm)
1664                 lov_stripe_lock(lsm);
1665 }
1666
1667 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1668 {
1669         struct ll_inode_info *lli;
1670         struct lov_stripe_md *lsm;
1671
1672         lli = ll_i2info(inode);
1673         lsm = lli->lli_smd;
1674         LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1675                  lsm, unlock_lsm);
1676         if (unlock_lsm)
1677                 lov_stripe_unlock(lsm);
1678         LASSERT(lli->lli_size_sem_owner == current);
1679         lli->lli_size_sem_owner = NULL;
1680         up(&lli->lli_size_sem);
1681 }
1682
1683 static void ll_replace_lsm(struct inode *inode, struct lov_stripe_md *lsm)
1684 {
1685         struct ll_inode_info *lli = ll_i2info(inode);
1686
1687         dump_lsm(D_INODE, lsm);
1688         dump_lsm(D_INODE, lli->lli_smd);
1689         LASSERTF(lsm->lsm_magic == LOV_MAGIC_JOIN,
1690                  "lsm must be joined lsm %p\n", lsm);
1691         obd_free_memmd(ll_i2dtexp(inode), &lli->lli_smd);
1692         CDEBUG(D_INODE, "replace lsm %p to lli_smd %p for inode %lu%u(%p)\n",
1693                lsm, lli->lli_smd, inode->i_ino, inode->i_generation, inode);
1694         lli->lli_smd = lsm;
1695         lli->lli_maxbytes = lsm->lsm_maxbytes;
1696         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1697                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1698 }
1699
1700 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1701 {
1702         struct ll_inode_info *lli = ll_i2info(inode);
1703         struct mdt_body *body = md->body;
1704         struct lov_stripe_md *lsm = md->lsm;
1705         struct ll_sb_info *sbi = ll_i2sbi(inode);
1706
1707         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1708         if (lsm != NULL) {
1709                 if (lli->lli_smd == NULL) {
1710                         if (lsm->lsm_magic != LOV_MAGIC &&
1711                             lsm->lsm_magic != LOV_MAGIC_JOIN) {
1712                                 dump_lsm(D_ERROR, lsm);
1713                                 LBUG();
1714                         }
1715                         CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1716                                lsm, inode->i_ino, inode->i_generation, inode);
1717                         /* ll_inode_size_lock() requires it is only called
1718                          * with lli_smd != NULL or lock_lsm == 0 or we can
1719                          * race between lock/unlock.  bug 9547 */
1720                         lli->lli_smd = lsm;
1721                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1722                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1723                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1724                 } else {
1725                         if (lli->lli_smd->lsm_magic == lsm->lsm_magic &&
1726                              lli->lli_smd->lsm_stripe_count ==
1727                                         lsm->lsm_stripe_count) {
1728                                 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1729                                         CERROR("lsm mismatch for inode %ld\n",
1730                                                 inode->i_ino);
1731                                         CERROR("lli_smd:\n");
1732                                         dump_lsm(D_ERROR, lli->lli_smd);
1733                                         CERROR("lsm:\n");
1734                                         dump_lsm(D_ERROR, lsm);
1735                                         LBUG();
1736                                 }
1737                         } else
1738                                 ll_replace_lsm(inode, lsm);
1739                 }
1740                 if (lli->lli_smd != lsm)
1741                         obd_free_memmd(ll_i2dtexp(inode), &lsm);
1742         }
1743
1744         if (sbi->ll_flags & LL_SBI_RMT_CLIENT) {
1745                 if (body->valid & OBD_MD_FLRMTPERM)
1746                         ll_update_remote_perm(inode, md->remote_perm);
1747         }
1748 #ifdef CONFIG_FS_POSIX_ACL
1749         else if (body->valid & OBD_MD_FLACL) {
1750                 spin_lock(&lli->lli_lock);
1751                 if (lli->lli_posix_acl)
1752                         posix_acl_release(lli->lli_posix_acl);
1753                 lli->lli_posix_acl = md->posix_acl;
1754                 spin_unlock(&lli->lli_lock);
1755         }
1756 #endif
1757         if (body->valid & OBD_MD_FLATIME &&
1758             body->atime > LTIME_S(inode->i_atime))
1759                 LTIME_S(inode->i_atime) = body->atime;
1760         
1761         /* mtime is always updated with ctime, but can be set in past.
1762            As write and utime(2) may happen within 1 second, and utime's
1763            mtime has a priority over write's one, so take mtime from mds 
1764            for the same ctimes. */
1765         if (body->valid & OBD_MD_FLCTIME &&
1766             body->ctime >= LTIME_S(inode->i_ctime)) {
1767                 LTIME_S(inode->i_ctime) = body->ctime;
1768                 if (body->valid & OBD_MD_FLMTIME) {
1769                         CDEBUG(D_INODE, "setting ino %lu mtime "
1770                                "from %lu to "LPU64"\n", inode->i_ino, 
1771                                LTIME_S(inode->i_mtime), body->mtime);
1772                         LTIME_S(inode->i_mtime) = body->mtime;
1773                 }
1774         }
1775         if (body->valid & OBD_MD_FLMODE)
1776                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1777         if (body->valid & OBD_MD_FLTYPE)
1778                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1779         if (S_ISREG(inode->i_mode))
1780                 inode->i_blksize = min(2UL*PTLRPC_MAX_BRW_SIZE, LL_MAX_BLKSIZE);
1781         else
1782                 inode->i_blksize = inode->i_sb->s_blocksize;
1783         if (body->valid & OBD_MD_FLUID)
1784                 inode->i_uid = body->uid;
1785         if (body->valid & OBD_MD_FLGID)
1786                 inode->i_gid = body->gid;
1787         if (body->valid & OBD_MD_FLFLAGS)
1788                 inode->i_flags = ll_ext_to_inode_flags(body->flags);
1789         if (body->valid & OBD_MD_FLNLINK)
1790                 inode->i_nlink = body->nlink;
1791         if (body->valid & OBD_MD_FLRDEV)
1792 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1793                 inode->i_rdev = body->rdev;
1794 #else
1795                 inode->i_rdev = old_decode_dev(body->rdev);
1796 #endif
1797         if (body->valid & OBD_MD_FLSIZE) {
1798                 inode->i_size = body->size;
1799
1800                 if (body->valid & OBD_MD_FLBLOCKS)
1801                         inode->i_blocks = body->blocks;
1802
1803                 lli->lli_flags |= LLIF_MDS_SIZE_LOCK;
1804         }
1805
1806         if (body->valid & OBD_MD_FLID) {
1807                 /* FID shouldn't be changed! */
1808                 if (fid_is_sane(&lli->lli_fid)) {
1809                         LASSERTF(lu_fid_eq(&lli->lli_fid, &body->fid1),
1810                                  "Trying to change FID "DFID
1811                                  " to the "DFID", inode %lu/%u(%p)\n",
1812                                  PFID(&lli->lli_fid), PFID(&body->fid1),
1813                                  inode->i_ino, inode->i_generation, inode);
1814                 } else 
1815                         lli->lli_fid = body->fid1;
1816         }
1817
1818         LASSERT(fid_seq(&lli->lli_fid) != 0);
1819
1820         if (body->valid & OBD_MD_FLMDSCAPA) {
1821                 LASSERT(md->mds_capa);
1822                 ll_add_capa(inode, md->mds_capa);
1823         }
1824         if (body->valid & OBD_MD_FLOSSCAPA) {
1825                 LASSERT(md->oss_capa);
1826                 ll_add_capa(inode, md->oss_capa);
1827         }
1828 }
1829
1830 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1831 static struct backing_dev_info ll_backing_dev_info = {
1832         .ra_pages       = 0,    /* No readahead */
1833 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1834         .capabilities   = 0,    /* Does contribute to dirty memory */
1835 #else
1836         .memory_backed  = 0,    /* Does contribute to dirty memory */
1837 #endif
1838 };
1839 #endif
1840
1841 void ll_read_inode2(struct inode *inode, void *opaque)
1842 {
1843         struct lustre_md *md = opaque;
1844         struct ll_inode_info *lli = ll_i2info(inode);
1845         ENTRY;
1846
1847         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n",
1848                inode->i_ino, inode->i_generation, inode);
1849
1850         ll_lli_init(lli);
1851
1852         LASSERT(!lli->lli_smd);
1853
1854         /* Core attributes from the MDS first.  This is a new inode, and
1855          * the VFS doesn't zero times in the core inode so we have to do
1856          * it ourselves.  They will be overwritten by either MDS or OST
1857          * attributes - we just need to make sure they aren't newer. */
1858         LTIME_S(inode->i_mtime) = 0;
1859         LTIME_S(inode->i_atime) = 0;
1860         LTIME_S(inode->i_ctime) = 0;
1861         inode->i_rdev = 0;
1862         ll_update_inode(inode, md);
1863
1864         /* OIDEBUG(inode); */
1865
1866         if (S_ISREG(inode->i_mode)) {
1867                 struct ll_sb_info *sbi = ll_i2sbi(inode);
1868                 inode->i_op = &ll_file_inode_operations;
1869                 inode->i_fop = sbi->ll_fop;
1870                 inode->i_mapping->a_ops = &ll_aops;
1871                 EXIT;
1872         } else if (S_ISDIR(inode->i_mode)) {
1873                 inode->i_op = &ll_dir_inode_operations;
1874                 inode->i_fop = &ll_dir_operations;
1875                 inode->i_mapping->a_ops = &ll_dir_aops;
1876                 EXIT;
1877         } else if (S_ISLNK(inode->i_mode)) {
1878                 inode->i_op = &ll_fast_symlink_inode_operations;
1879                 EXIT;
1880         } else {
1881                 inode->i_op = &ll_special_inode_operations;
1882
1883 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1884                 init_special_inode(inode, inode->i_mode,
1885                                    kdev_t_to_nr(inode->i_rdev));
1886
1887                 /* initializing backing dev info. */
1888                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1889 #else
1890                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1891 #endif
1892                 EXIT;
1893         }
1894 }
1895
1896 void ll_delete_inode(struct inode *inode)
1897 {
1898         struct ll_sb_info *sbi = ll_i2sbi(inode);
1899         int rc;
1900         ENTRY;
1901
1902         rc = obd_fid_delete(sbi->ll_md_exp, ll_inode2fid(inode));
1903         if (rc) {
1904                 CERROR("fid_delete() failed, rc %d\n", rc);
1905         }
1906         clear_inode(inode);
1907
1908         EXIT;
1909 }
1910
1911 int ll_iocontrol(struct inode *inode, struct file *file,
1912                  unsigned int cmd, unsigned long arg)
1913 {
1914         struct ll_sb_info *sbi = ll_i2sbi(inode);
1915         struct ptlrpc_request *req = NULL;
1916         int rc, flags = 0;
1917         ENTRY;
1918
1919         switch(cmd) {
1920         case EXT3_IOC_GETFLAGS: {
1921                 struct mdt_body *body;
1922                 struct obd_capa *oc;
1923
1924                 oc = ll_i2mdscapa(inode);
1925                 rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
1926                                 OBD_MD_FLFLAGS, 0, &req);
1927                 capa_put(oc);
1928                 if (rc) {
1929                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1930                         RETURN(-abs(rc));
1931                 }
1932
1933                 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1934                                       sizeof(*body));
1935
1936                 /*Now the ext3 will be packed directly back to client,
1937                  *no need convert here*/
1938                 flags = body->flags;
1939
1940                 ptlrpc_req_finished (req);
1941
1942                 RETURN(put_user(flags, (int *)arg));
1943         }
1944         case EXT3_IOC_SETFLAGS: {
1945                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1946                 struct obd_info oinfo = { { { 0 } } };
1947                 struct md_op_data *op_data;
1948
1949                 if (get_user(flags, (int *)arg))
1950                         RETURN(-EFAULT);
1951
1952                 oinfo.oi_md = lsm;
1953                 oinfo.oi_oa = obdo_alloc();
1954                 if (!oinfo.oi_oa)
1955                         RETURN(-ENOMEM);
1956
1957                 op_data = ll_prep_md_op_data(NULL, inode, NULL, NULL, 0, 0);
1958                 if (op_data == NULL)
1959                         RETURN(-ENOMEM);
1960                 
1961                 ((struct ll_iattr *)&op_data->attr)->ia_attr_flags = flags;
1962                 op_data->attr.ia_valid |= ATTR_ATTR_FLAG;
1963                 rc = md_setattr(sbi->ll_md_exp, op_data,
1964                                 NULL, 0, NULL, 0, &req);
1965                 ll_finish_md_op_data(op_data);
1966                 ptlrpc_req_finished(req);
1967                 if (rc || lsm == NULL) {
1968                         obdo_free(oinfo.oi_oa);
1969                         RETURN(rc);
1970                 }
1971
1972                 oinfo.oi_oa->o_id = lsm->lsm_object_id;
1973                 oinfo.oi_oa->o_gr = lsm->lsm_object_gr;
1974                 oinfo.oi_oa->o_flags = flags;
1975                 oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | 
1976                                        OBD_MD_FLGROUP;
1977
1978                 obdo_from_inode(oinfo.oi_oa, inode,
1979                                 OBD_MD_FLFID | OBD_MD_FLGENER);
1980                 rc = obd_setattr_rqset(sbi->ll_dt_exp, &oinfo, NULL);
1981                 obdo_free(oinfo.oi_oa);
1982                 if (rc) {
1983                         if (rc != -EPERM && rc != -EACCES)
1984                                 CERROR("md_setattr_async fails: rc = %d\n", rc);
1985                         RETURN(rc);
1986                 }
1987
1988                 inode->i_flags = ll_ext_to_inode_flags(flags |
1989                                                        MDS_BFLAG_EXT_FLAGS);
1990                 RETURN(0);
1991         }
1992         default:
1993                 RETURN(-ENOSYS);
1994         }
1995
1996         RETURN(0);
1997 }
1998
1999 int ll_flush_ctx(struct inode *inode)
2000 {
2001         struct ll_sb_info  *sbi = ll_i2sbi(inode);
2002
2003         CDEBUG(D_SEC, "flush context for user %d\n", current->uid);
2004
2005         obd_set_info_async(sbi->ll_md_exp,
2006                            sizeof(KEY_FLUSH_CTX) - 1, KEY_FLUSH_CTX,
2007                            0, NULL, NULL);
2008         obd_set_info_async(sbi->ll_dt_exp,
2009                            sizeof(KEY_FLUSH_CTX) - 1, KEY_FLUSH_CTX,
2010                            0, NULL, NULL);
2011         return 0;
2012 }
2013
2014 /* umount -f client means force down, don't save state */
2015 void ll_umount_begin(struct super_block *sb)
2016 {
2017         struct lustre_sb_info *lsi = s2lsi(sb);
2018         struct ll_sb_info *sbi = ll_s2sbi(sb);
2019         struct obd_device *obd;
2020         struct obd_ioctl_data ioc_data = { 0 };
2021         ENTRY;
2022
2023         /* Tell the MGC we got umount -f */
2024         lsi->lsi_flags |= LSI_UMOUNT_FORCE;
2025
2026         CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
2027                sb->s_count, atomic_read(&sb->s_active));
2028
2029         obd = class_exp2obd(sbi->ll_md_exp);
2030         if (obd == NULL) {
2031                 CERROR("Invalid MDC connection handle "LPX64"\n",
2032                        sbi->ll_md_exp->exp_handle.h_cookie);
2033                 EXIT;
2034                 return;
2035         }
2036         obd->obd_no_recov = 1;
2037         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_md_exp, sizeof ioc_data,
2038                       &ioc_data, NULL);
2039
2040         obd = class_exp2obd(sbi->ll_dt_exp);
2041         if (obd == NULL) {
2042                 CERROR("Invalid LOV connection handle "LPX64"\n",
2043                        sbi->ll_dt_exp->exp_handle.h_cookie);
2044                 EXIT;
2045                 return;
2046         }
2047
2048         obd->obd_no_recov = 1;
2049         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_dt_exp, sizeof ioc_data,
2050                       &ioc_data, NULL);
2051
2052         /* Really, we'd like to wait until there are no requests outstanding,
2053          * and then continue.  For now, we just invalidate the requests,
2054          * schedule, and hope.
2055          */
2056         schedule();
2057
2058         EXIT;
2059 }
2060
2061 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
2062 {
2063         struct ll_sb_info *sbi = ll_s2sbi(sb);
2064         int err;
2065         __u32 read_only;
2066
2067         if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
2068                 read_only = *flags & MS_RDONLY;
2069                 err = obd_set_info_async(sbi->ll_md_exp, strlen("read-only"),
2070                                          "read-only", sizeof(read_only),
2071                                          &read_only, NULL);
2072                 if (err) {
2073                         CERROR("Failed to change the read-only flag during "
2074                                "remount: %d\n", err);
2075                         return err;
2076                 }
2077
2078                 if (read_only)
2079                         sb->s_flags |= MS_RDONLY;
2080                 else
2081                         sb->s_flags &= ~MS_RDONLY;
2082         }
2083         return 0;
2084 }
2085
2086 int ll_prep_inode(struct inode **inode, struct ptlrpc_request *req,
2087                   int offset, struct super_block *sb)
2088 {
2089         struct ll_sb_info *sbi = NULL;
2090         struct lustre_md md;
2091         int rc = 0;
2092         ENTRY;
2093
2094         LASSERT(*inode || sb);
2095         sbi = sb ? ll_s2sbi(sb) : ll_i2sbi(*inode);
2096         prune_deathrow(sbi, 1);
2097
2098         rc = md_get_lustre_md(sbi->ll_md_exp, req, offset,
2099                               sbi->ll_dt_exp, sbi->ll_md_exp, &md);
2100         if (rc)
2101                 RETURN(rc);
2102
2103         if (*inode) {
2104                 ll_update_inode(*inode, &md);
2105         } else {
2106                 LASSERT(sb != NULL);
2107
2108                 /* at this point server answers to client's RPC with same fid as
2109                  * client generated for creating some inode. So using ->fid1 is
2110                  * okay here. */
2111                 LASSERT(fid_is_sane(&md.body->fid1));
2112
2113                 *inode = ll_iget(sb, ll_fid_build_ino(sbi, &md.body->fid1), &md);
2114                 if (*inode == NULL || is_bad_inode(*inode)) {
2115                         md_free_lustre_md(sbi->ll_dt_exp, &md);
2116                         rc = -ENOMEM;
2117                         CERROR("new_inode -fatal: rc %d\n", rc);
2118                         GOTO(out, rc);
2119                 }
2120         }
2121
2122         rc = obd_checkmd(sbi->ll_dt_exp, sbi->ll_md_exp,
2123                          ll_i2info(*inode)->lli_smd);
2124 out:
2125         RETURN(rc);
2126 }
2127
2128 char *llap_origins[] = {
2129         [LLAP_ORIGIN_UNKNOWN] = "--",
2130         [LLAP_ORIGIN_READPAGE] = "rp",
2131         [LLAP_ORIGIN_READAHEAD] = "ra",
2132         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
2133         [LLAP_ORIGIN_WRITEPAGE] = "wp",
2134 };
2135
2136 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
2137                                              struct list_head *list)
2138 {
2139         struct ll_async_page *llap;
2140         struct list_head *pos;
2141
2142         list_for_each(pos, list) {
2143                 if (pos == &sbi->ll_pglist)
2144                         return NULL;
2145                 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
2146                 if (llap->llap_page == NULL)
2147                         continue;
2148                 return llap;
2149         }
2150         LBUG();
2151         return NULL;
2152 }
2153
2154 int ll_obd_statfs(struct inode *inode, void *arg)
2155 {
2156         struct ll_sb_info *sbi = NULL;
2157         struct obd_device *client_obd = NULL, *lov_obd = NULL;
2158         struct lov_obd *lov = NULL;
2159         struct obd_statfs stat_buf = {0};
2160         char *buf = NULL;
2161         struct obd_ioctl_data *data = NULL;
2162         __u32 type, index;
2163         int len, rc;
2164
2165         if (!inode || !(sbi = ll_i2sbi(inode)))
2166                 GOTO(out_statfs, rc = -EINVAL);
2167
2168         rc = obd_ioctl_getdata(&buf, &len, arg);
2169         if (rc)
2170                 GOTO(out_statfs, rc);
2171
2172         data = (void*)buf;
2173         if (!data->ioc_inlbuf1 || !data->ioc_inlbuf2 ||
2174             !data->ioc_pbuf1 || !data->ioc_pbuf2)
2175                 GOTO(out_statfs, rc = -EINVAL);
2176
2177         memcpy(&type, data->ioc_inlbuf1, sizeof(__u32));
2178         memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
2179
2180         if (type == LL_STATFS_MDC) {
2181                 if (index > 0)
2182                         GOTO(out_statfs, rc = -ENODEV);
2183                 client_obd = class_exp2obd(sbi->ll_md_exp);
2184         } else if (type == LL_STATFS_LOV) {
2185                 lov_obd = class_exp2obd(sbi->ll_dt_exp);
2186                 lov = &lov_obd->u.lov;
2187
2188                 if ((index >= lov->desc.ld_tgt_count) ||
2189                     !lov->lov_tgts[index])
2190                         GOTO(out_statfs, rc = -ENODEV);
2191
2192                 client_obd = class_exp2obd(lov->lov_tgts[index]->ltd_exp);
2193                 if (!lov->lov_tgts[index]->ltd_active)
2194                         GOTO(out_uuid, rc = -ENODATA);
2195         }
2196
2197         if (!client_obd)
2198                 GOTO(out_statfs, rc = -EINVAL);
2199
2200         rc = obd_statfs(client_obd, &stat_buf, cfs_time_current_64() - 1);
2201         if (rc)
2202                 GOTO(out_statfs, rc);
2203
2204         if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
2205                 GOTO(out_statfs, rc = -EFAULT);
2206
2207 out_uuid:
2208         if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(client_obd),
2209                          data->ioc_plen2))
2210                 rc = -EFAULT;
2211
2212 out_statfs:
2213         if (buf)
2214                 obd_ioctl_freedata(buf, len);
2215         return rc;
2216 }
2217
2218 int ll_process_config(struct lustre_cfg *lcfg)
2219 {
2220         char *ptr;
2221         void *sb;
2222         struct lprocfs_static_vars lvars;
2223         unsigned long x; 
2224         int rc = 0;
2225
2226         lprocfs_init_vars(llite, &lvars);
2227
2228         /* The instance name contains the sb: lustre-client-aacfe000 */
2229         ptr = strrchr(lustre_cfg_string(lcfg, 0), '-');
2230         if (!ptr || !*(++ptr)) 
2231                 return -EINVAL;
2232         if (sscanf(ptr, "%lx", &x) != 1)
2233                 return -EINVAL;
2234         sb = (void *)x;
2235         /* This better be a real Lustre superblock! */
2236         LASSERT(s2lsi((struct super_block *)sb)->lsi_lmd->lmd_magic == LMD_MAGIC);
2237
2238         /* Note we have not called client_common_fill_super yet, so 
2239            proc fns must be able to handle that! */
2240         rc = class_process_proc_param(PARAM_LLITE, lvars.obd_vars,
2241                                       lcfg, sb);
2242         return(rc);
2243 }
2244
2245 /* this function prepares md_op_data hint for passing ot down to MD stack. */
2246 struct md_op_data *
2247 ll_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
2248                    struct inode *i2, const char *name, int namelen, int mode)
2249 {
2250         LASSERT(i1 != NULL);
2251
2252         if (!op_data)
2253                 OBD_ALLOC_PTR(op_data);
2254         if (!op_data)
2255                 return NULL;
2256
2257         ll_i2gids(op_data->suppgids, i1, i2);
2258         op_data->fid1 = ll_i2info(i1)->lli_fid;
2259         op_data->mod_capa1 = ll_i2mdscapa(i1);
2260
2261         /* @i2 may be NULL. In this case caller itself has to initialize ->fid2
2262          * if needed. */
2263         if (i2) {
2264                 op_data->fid2 = *ll_inode2fid(i2);
2265                 op_data->mod_capa2 = ll_i2mdscapa(i2);
2266         }
2267
2268         op_data->name = name;
2269         op_data->namelen = namelen;
2270         op_data->mode = mode;
2271         op_data->mod_time = CURRENT_SECONDS;
2272         op_data->fsuid = current->fsuid;
2273         op_data->fsgid = current->fsgid;
2274         op_data->cap = current->cap_effective;
2275
2276         return op_data;
2277 }
2278
2279 void ll_finish_md_op_data(struct md_op_data *op_data)
2280 {
2281         capa_put(op_data->mod_capa1);
2282         capa_put(op_data->mod_capa2);
2283         OBD_FREE_PTR(op_data);
2284 }
2285
2286 int ll_ioctl_getfacl(struct inode *inode, struct rmtacl_ioctl_data *ioc)
2287 {
2288         struct ptlrpc_request *req = NULL;
2289         struct mdt_body *body;
2290         char *cmd, *buf;
2291         struct obd_capa *oc;
2292         int rc, buflen;
2293         ENTRY;
2294
2295         LASSERT(ioc->cmd && ioc->cmd_len && ioc->res && ioc->res_len);
2296
2297         OBD_ALLOC(cmd, ioc->cmd_len);
2298         if (!cmd)
2299                 RETURN(-ENOMEM);
2300         if (copy_from_user(cmd, ioc->cmd, ioc->cmd_len))
2301                 GOTO(out, rc = -EFAULT);
2302
2303         oc = ll_i2mdscapa(inode);
2304         rc = md_getxattr(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
2305                          OBD_MD_FLXATTR, XATTR_NAME_LUSTRE_ACL, cmd,
2306                          ioc->cmd_len, ioc->res_len, 0, &req);
2307         capa_put(oc);
2308         if (rc < 0) {
2309                 CERROR("mdc_getxattr %s [%s] failed: %d\n",
2310                        XATTR_NAME_LUSTRE_ACL, cmd, rc);
2311                 GOTO(out, rc);
2312         }
2313
2314         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
2315         LASSERT(body);
2316
2317         buflen = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF);
2318         LASSERT(buflen <= ioc->res_len);
2319         buf = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF + 1, ioc->res_len);
2320         LASSERT(buf);
2321         if (copy_to_user(ioc->res, buf, buflen))
2322                 GOTO(out, rc = -EFAULT);
2323         EXIT;
2324 out:
2325         if (req)
2326                 ptlrpc_req_finished(req);
2327         OBD_FREE(cmd, ioc->cmd_len);
2328         return rc;
2329 }
2330
2331 int ll_ioctl_setfacl(struct inode *inode, struct rmtacl_ioctl_data *ioc)
2332 {
2333         struct ptlrpc_request *req = NULL;
2334         char *cmd, *buf;
2335         struct obd_capa *oc;
2336         int buflen, rc;
2337         ENTRY;
2338
2339         LASSERT(ioc->cmd && ioc->cmd_len && ioc->res && ioc->res_len);
2340
2341         OBD_ALLOC(cmd, ioc->cmd_len);
2342         if (!cmd)
2343                 RETURN(-ENOMEM);
2344         if (copy_from_user(cmd, ioc->cmd, ioc->cmd_len))
2345                 GOTO(out, rc = -EFAULT);
2346
2347         oc = ll_i2mdscapa(inode);
2348         rc = md_setxattr(ll_i2sbi(inode)->ll_md_exp, ll_inode2fid(inode), oc,
2349                          OBD_MD_FLXATTR, XATTR_NAME_LUSTRE_ACL, cmd,
2350                          ioc->cmd_len, ioc->res_len, 0, &req);
2351         capa_put(oc);
2352         if (rc) {
2353                 CERROR("mdc_setxattr %s [%s] failed: %d\n",
2354                        XATTR_NAME_LUSTRE_ACL, cmd, rc);
2355                 GOTO(out, rc);
2356         }
2357
2358         buflen = lustre_msg_buflen(req->rq_repmsg, REPLY_REC_OFF);
2359         LASSERT(buflen <= ioc->res_len);
2360         buf = lustre_msg_string(req->rq_repmsg, REPLY_REC_OFF, ioc->res_len);
2361         LASSERT(buf);
2362         if (copy_to_user(ioc->res, buf, buflen))
2363                 GOTO(out, rc = -EFAULT);
2364         EXIT;
2365 out:
2366         if (req)
2367                 ptlrpc_req_finished(req);
2368         OBD_FREE(cmd, ioc->cmd_len);
2369         return rc;
2370 }