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