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