Whamcloud - gitweb
d8ae8e8716c78387b12d346bbdb07e465b5e9c3f
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30
31 #include <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_dlm.h>
34 #include <linux/lprocfs_status.h>
35 #include <linux/lustre_snap.h>
36 #include "llite_internal.h"
37
38 kmem_cache_t *ll_file_data_slab;
39
40 extern struct address_space_operations ll_aops;
41 extern struct address_space_operations ll_dir_aops;
42
43 #ifndef log2
44 #define log2(n) ffz(~(n))
45 #endif
46
47 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
48 {
49         struct ll_sb_info *sbi = NULL;
50         class_uuid_t uuid;
51         ENTRY;
52
53         OBD_ALLOC(sbi, sizeof(*sbi));
54         if (!sbi)
55                 RETURN(NULL);
56
57         spin_lock_init(&sbi->ll_lock);
58         INIT_LIST_HEAD(&sbi->ll_pglist);
59         sbi->ll_pglist_gen = 0;
60         sbi->ll_max_read_ahead_pages = SBI_DEFAULT_RA_MAX;
61         INIT_LIST_HEAD(&sbi->ll_conn_chain);
62         INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
63         INIT_LIST_HEAD(&sbi->ll_mnt_list);
64         sema_init(&sbi->ll_gns_sem, 1);
65         init_completion(&sbi->ll_gns_completion);
66         sbi->ll_gns_state = LL_GNS_STATE_IDLE;
67         sbi->ll_gns_timer.data = (unsigned long)sbi;
68         sbi->ll_gns_timer.function = ll_gns_timer_callback;
69         init_timer(&sbi->ll_gns_timer);
70         INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
71
72         ll_set_sbi(sb, sbi);
73
74         generate_random_uuid(uuid);
75         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
76         RETURN(sbi);
77 }
78
79 void lustre_free_sbi(struct super_block *sb)
80 {
81         struct ll_sb_info *sbi = ll_s2sbi(sb);
82         ENTRY;
83
84         if (sbi != NULL) {
85                 list_del(&sbi->ll_gns_sbi_head);
86                 del_timer(&sbi->ll_gns_timer);
87                 OBD_FREE(sbi, sizeof(*sbi));
88         }
89         ll_set_sbi(sb, NULL);
90         EXIT;
91 }
92
93 int lustre_init_ea_size(struct ll_sb_info *sbi)
94 {
95         struct lov_desc desc;
96         int rc, valsize;
97         
98         /* OSC may have larger ea size */
99         valsize = sizeof(desc);
100         rc = obd_get_info(sbi->ll_osc_exp, strlen("lovdesc") + 1, "lovdesc", 
101                           &valsize, &desc);
102         if (rc)
103                 RETURN(rc);
104         obd_init_ea_size(sbi->ll_mdc_exp, obd_size_diskmd(sbi->ll_osc_exp, NULL),
105                          desc.ld_tgt_count*sizeof(struct llog_cookie));
106
107         RETURN(rc);
108 }
109 #if CONFIG_SNAPFS
110 int lustre_set_clone_info(struct super_block *sb, int clone_index)
111 {
112         struct ll_sb_info *sbi = ll_s2sbi(sb);
113         
114         ENTRY;
115
116         CDEBUG(D_INFO, "set clone index %d\n", clone_index);
117         if (!clone_index)
118                 RETURN(0); 
119         
120         if (sbi->ll_mdc_exp) {
121                 struct obd_device *obd = class_exp2obd(sbi->ll_mdc_exp);
122                 struct client_obd *cl_obd = &obd->u.cli;
123
124                 OBD_ALLOC(cl_obd->cl_clone_info, sizeof(struct clonefs_info));
125                 if (!cl_obd->cl_clone_info) 
126                         RETURN(-ENOMEM);
127                 SET_CLONE_INDEX(cl_obd->cl_clone_info, clone_index);
128                 SET_CLONE_FLAGS(cl_obd->cl_clone_info, SM_CLONE_FS);
129                 cl_obd->cl_clone_info->cl_clone_info->clone_magic = 
130                                                   cpu_to_le32(CLONE_INFO_MAGI);
131         }
132         
133         if (sbi->ll_osc_exp) {
134                 struct obd_device *obd = class_exp2obd(sbi->ll_osc_exp);
135                 struct client_obd *cl_obd = &obd->u.cli;
136
137                 OBD_ALLOC(cl_obd->cl_clone_info, sizeof(struct clonefs_info));
138                 if (!cl_obd->cl_clone_info) 
139                         RETURN(-ENOMEM);
140                 SET_CLONE_INDEX(cl_obd->cl_clone_info, clone_index);
141                 SET_CLONE_FLAGS(cl_obd->cl_clone_info, SM_CLONE_FS);
142                 cl_obd->cl_clone_info->cl_clone_info->clone_magic = 
143                                                   cpu_to_le32(CLONE_INFO_MAGI);
144         }
145         RETURN(0); 
146 }
147
148 int lustre_cleanup_clone_info(struct super_block *sb)
149 {
150         struct ll_sb_info *sbi = ll_s2sbi(sb);
151        
152         ENTRY; 
153
154         if (sbi->ll_mdc_exp) {
155                 struct obd_device *obd = class_exp2obd(sbi->ll_mdc_exp);
156                 struct client_obd *cl_obd = &obd->u.cli;
157
158                 if (!cl_obd->cl_clone_info) 
159                         RETURN(0); 
160                 CDEBUG(D_INFO, "clean clone info %p\n", cl_obd->cl_clone_info);
161                 OBD_FREE(cl_obd->cl_clone_info, sizeof(struct clonefs_info));
162         }
163         
164         if (sbi->ll_osc_exp) {
165                 struct obd_device *obd = class_exp2obd(sbi->ll_osc_exp);
166                 struct client_obd *cl_obd = &obd->u.cli;
167
168                 if (!cl_obd->cl_clone_info) 
169                         RETURN(0); 
170                 CDEBUG(D_INFO, "clean clone info %p\n", cl_obd->cl_clone_info);
171                 OBD_FREE(cl_obd->cl_clone_info, sizeof(struct clonefs_info));
172         }
173         RETURN(0); 
174 }
175 #endif
176
177
178 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
179 {
180         struct inode *root = 0;
181         struct ll_sb_info *sbi = ll_s2sbi(sb);
182         struct obd_device *obd;
183         struct ll_fid rootfid;
184         struct obd_statfs osfs;
185         struct ptlrpc_request *request = NULL;
186         struct lustre_handle osc_conn = {0, };
187         struct lustre_handle mdc_conn = {0, };
188         struct lustre_md md;
189         kdev_t devno;
190         int err;
191
192         obd = class_name2obd(mdc);
193         if (!obd) {
194                 CERROR("MDC %s: not setup or attached\n", mdc);
195                 RETURN(-EINVAL);
196         }
197
198         if (proc_lustre_fs_root) {
199                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
200                                                   osc, mdc);
201                 if (err < 0)
202                         CERROR("could not register mount in /proc/lustre");
203         }
204
205         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, 0);
206         if (err == -EBUSY) {
207                 CERROR("An MDS (mdc %s) is performing recovery, of which this"
208                        " client is not a part.  Please wait for recovery to "
209                        "complete, abort, or time out.\n", mdc);
210                 GOTO(out, err);
211         } else if (err) {
212                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
213                 GOTO(out, err);
214         }
215         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
216
217         err = obd_statfs(obd, &osfs, jiffies - HZ);
218         if (err)
219                 GOTO(out_mdc, err);
220
221         LASSERT(osfs.os_bsize);
222         sb->s_blocksize = osfs.os_bsize;
223         sb->s_blocksize_bits = log2(osfs.os_bsize);
224         sb->s_magic = LL_SUPER_MAGIC;
225         sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
226        
227         devno = get_uuid2int(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid, 
228                              strlen(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid));
229
230         sb->s_dev = devno;
231
232         obd = class_name2obd(osc);
233         if (!obd) {
234                 CERROR("OSC %s: not setup or attached\n", osc);
235                 GOTO(out_mdc, err);
236         }
237
238         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, 0);
239         if (err == -EBUSY) {
240                 CERROR("An OST (osc %s) is performing recovery, of which this"
241                        " client is not a part.  Please wait for recovery to "
242                        "complete, abort, or time out.\n", osc);
243                 GOTO(out, err);
244         } else if (err) {
245                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
246                 GOTO(out_mdc, err);
247         }
248         sbi->ll_osc_exp = class_conn2export(&osc_conn);
249
250         lustre_init_ea_size(sbi);
251
252         err = md_getstatus(sbi->ll_mdc_exp, &rootfid);
253         if (err) {
254                 CERROR("cannot mds_connect: rc = %d\n", err);
255                 GOTO(out_osc, err);
256         }
257         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
258         sbi->ll_rootino = rootfid.id;
259
260         sb->s_op = &lustre_super_operations;
261
262         /* make root inode
263          * XXX: move this to after cbd setup? */
264         err = md_getattr(sbi->ll_mdc_exp, &rootfid,
265                          OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS, 0, &request);
266         if (err) {
267                 CERROR("md_getattr failed for root: rc = %d\n", err);
268                 GOTO(out_osc, err);
269         }
270
271         err = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
272                                 sbi->ll_osc_exp, &md);
273         if (err) {
274                 CERROR("failed to understand root inode md: rc = %d\n",err);
275                 ptlrpc_req_finished (request);
276                 GOTO(out_osc, err);
277         }
278
279         LASSERT(sbi->ll_rootino != 0);
280         root = ll_iget(sb, sbi->ll_rootino, &md);
281
282         ptlrpc_req_finished(request);
283
284         if (root == NULL || is_bad_inode(root)) {
285                 /* XXX might need iput() for bad inode */
286                 CERROR("lustre_lite: bad iget4 for root\n");
287                 GOTO(out_root, err = -EBADF);
288         }
289
290         err = ll_close_thread_start(&sbi->ll_lcq);
291         if (err) {
292                 CERROR("cannot start close thread: rc %d\n", err);
293                 GOTO(out_root, err);
294         }
295
296         ll_gns_add_timer(sbi);
297
298         /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
299            backing dev info assigned to inode mapping is used for
300            determining maximal readahead. */
301 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
302         /* bug 2805 - set VM readahead to zero */
303         vm_max_readahead = vm_min_readahead = 0;
304 #endif
305
306         sb->s_root = d_alloc_root(root);
307
308 #ifdef S_PDIROPS
309         CWARN("Enabling PDIROPS\n");
310         sb->s_flags |= S_PDIROPS;
311 #endif
312
313         RETURN(err);
314
315 out_root:
316         if (root)
317                 iput(root);
318 out_osc:
319         obd_disconnect(sbi->ll_osc_exp, 0);
320 out_mdc:
321         obd_disconnect(sbi->ll_mdc_exp, 0);
322 out:
323         lprocfs_unregister_mountpoint(sbi);
324         RETURN(err);
325 }
326
327 void lustre_common_put_super(struct super_block *sb)
328 {
329         struct ll_sb_info *sbi = ll_s2sbi(sb);
330         struct hlist_node *tmp, *next;
331         ENTRY;
332
333         ll_gns_del_timer(sbi);
334
335         ll_close_thread_shutdown(sbi->ll_lcq);
336
337         list_del(&sbi->ll_conn_chain);
338         obd_disconnect(sbi->ll_osc_exp, 0);
339
340         lprocfs_unregister_mountpoint(sbi);
341         if (sbi->ll_proc_root) {
342                 lprocfs_remove(sbi->ll_proc_root);
343                 sbi->ll_proc_root = NULL;
344         }
345
346         obd_disconnect(sbi->ll_mdc_exp, 0);
347
348         // We do this to get rid of orphaned dentries. That is not really trw.
349         spin_lock(&dcache_lock);
350         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
351                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
352                 CWARN("orphan dentry %*s (%p) at unmount\n",
353                       dentry->d_name.len, dentry->d_name.name, dentry);
354                 shrink_dcache_parent(dentry);
355         }
356         spin_unlock(&dcache_lock);
357         EXIT;
358 }
359
360
361 char *ll_read_opt(const char *opt, char *data)
362 {
363         char *value;
364         char *retval;
365         ENTRY;
366
367         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
368         if (strncmp(opt, data, strlen(opt)))
369                 RETURN(NULL);
370         if ((value = strchr(data, '=')) == NULL)
371                 RETURN(NULL);
372
373         value++;
374         OBD_ALLOC(retval, strlen(value) + 1);
375         if (!retval) {
376                 CERROR("out of memory!\n");
377                 RETURN(NULL);
378         }
379
380         memcpy(retval, value, strlen(value)+1);
381         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
382         RETURN(retval);
383 }
384
385 int ll_set_opt(const char *opt, char *data, int fl)
386 {
387         ENTRY;
388
389         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
390         if (strncmp(opt, data, strlen(opt)))
391                 RETURN(0);
392         else
393                 RETURN(fl);
394 }
395
396 void ll_options(char *options, char **ost, char **mdc, int *flags, 
397                 char **clone_opts)
398 {
399         char *this_char;
400 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
401         char *opt_ptr = options;
402 #endif
403         ENTRY;
404
405         if (!options) {
406                 EXIT;
407                 return;
408         }
409
410 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
411         for (this_char = strtok (options, ",");
412              this_char != NULL;
413              this_char = strtok (NULL, ",")) {
414 #else
415         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
416 #endif
417                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
418                 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
419                         continue;
420                 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
421                         continue;
422                 if (!*clone_opts && (*clone_opts = ll_read_opt("clone", 
423                                                                 this_char))) 
424                         continue; 
425                 if (!(*flags & LL_SBI_NOLCK) &&
426                     ((*flags) = (*flags) |
427                                 ll_set_opt("nolock", this_char,
428                                            LL_SBI_NOLCK)))
429                         continue;
430         }
431         
432         EXIT;
433 }
434
435 void ll_lli_init(struct ll_inode_info *lli)
436 {
437         sema_init(&lli->lli_open_sem, 1);
438         lli->lli_flags = 0;
439         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
440         spin_lock_init(&lli->lli_lock);
441         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
442         lli->lli_inode_magic = LLI_INODE_MAGIC;
443 }
444
445 int ll_fill_super(struct super_block *sb, void *data, int silent)
446 {
447         struct ll_sb_info *sbi;
448         char *osc = NULL;
449         char *mdc = NULL;
450         char *clone_opts = NULL;
451         int err;
452         ENTRY;
453
454         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
455
456         sbi = lustre_init_sbi(sb);
457         if (!sbi)
458                 RETURN(-ENOMEM);
459
460         sbi->ll_flags |= LL_SBI_READAHEAD;
461         ll_options(data, &osc, &mdc, &sbi->ll_flags, &clone_opts);
462
463         if (!osc) {
464                 CERROR("no osc\n");
465                 GOTO(out, err = -EINVAL);
466         }
467
468         if (!mdc) {
469                 CERROR("no mdc\n");
470                 GOTO(out, err = -EINVAL);
471         }
472
473         err = lustre_common_fill_super(sb, mdc, osc);
474 #if CONFIG_SNAPFS        
475         if (clone_opts) {
476                 int clone_index = 0;
477                 char *endp;
478
479                 /*set clone info to the super block*/
480                 clone_index = simple_strtoul(clone_opts, &endp, 0);
481                 err = lustre_set_clone_info(sb, clone_index);                
482         }
483 #endif         
484 out:
485         if (err)
486                 lustre_free_sbi(sb);
487
488         if (mdc)
489                 OBD_FREE(mdc, strlen(mdc) + 1);
490         if (osc)
491                 OBD_FREE(osc, strlen(osc) + 1);
492         if (clone_opts)
493                 OBD_FREE(clone_opts, strlen(clone_opts) + 1);
494
495         RETURN(err);
496 } /* ll_read_super */
497
498 static int lustre_process_log(struct lustre_mount_data *lmd, char *profile,
499                               struct config_llog_instance *cfg, int allow_recov)
500 {
501         struct lustre_cfg lcfg;
502         struct portals_cfg pcfg;
503         char * peer = "MDS_PEER_UUID";
504         struct obd_device *obd;
505         struct lustre_handle mdc_conn = {0, };
506         struct obd_export *exp;
507         char * name = "mdc_dev";
508         class_uuid_t uuid;
509         struct obd_uuid mdc_uuid;
510         struct llog_ctxt *ctxt;
511         int rc = 0;
512         int err;
513         ENTRY;
514
515         if (lmd_bad_magic(lmd))
516                 RETURN(-EINVAL);
517
518         generate_random_uuid(uuid);
519         class_uuid_unparse(uuid, &mdc_uuid);
520
521         if (lmd->lmd_local_nid) {
522                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
523                 pcfg.pcfg_nal = lmd->lmd_nal;
524                 pcfg.pcfg_nid = lmd->lmd_local_nid;
525                 err = libcfs_nal_cmd(&pcfg);
526                 if (err <0)
527                         GOTO(out, err);
528         }
529
530         if (lmd->lmd_nal == SOCKNAL) {
531                 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
532                 pcfg.pcfg_nal     = lmd->lmd_nal;
533                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
534                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
535                 pcfg.pcfg_misc    = lmd->lmd_port;
536                 pcfg.pcfg_size    = 8388608;
537                 pcfg.pcfg_flags   = 0x4; /*share*/
538                 err = libcfs_nal_cmd(&pcfg);
539                 if (err <0)
540                         GOTO(out, err);
541         }
542
543         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
544         lcfg.lcfg_nid = lmd->lmd_server_nid;
545         lcfg.lcfg_inllen1 = strlen(peer) + 1;
546         lcfg.lcfg_inlbuf1 = peer;
547         lcfg.lcfg_nal = lmd->lmd_nal;
548         err = class_process_config(&lcfg);
549         if (err < 0)
550                 GOTO(out_del_conn, err);
551
552         LCFG_INIT(lcfg, LCFG_ATTACH, name);
553         lcfg.lcfg_inlbuf1 = "mdc";
554         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
555         lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
556         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
557         err = class_process_config(&lcfg);
558         if (err < 0)
559                 GOTO(out_del_uuid, err);
560
561         LCFG_INIT(lcfg, LCFG_SETUP, name);
562         lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
563         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
564         lcfg.lcfg_inlbuf2 = peer;
565         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
566         err = class_process_config(&lcfg);
567         if (err < 0)
568                 GOTO(out_detach, err);
569
570         obd = class_name2obd(name);
571         if (obd == NULL)
572                 GOTO(out_cleanup, err = -EINVAL);
573
574         /* Disable initial recovery on this import */
575         err = obd_set_info(obd->obd_self_export,
576                            strlen("initial_recov"), "initial_recov",
577                            sizeof(allow_recov), &allow_recov);
578         if (err)
579                 GOTO(out_cleanup, err);
580
581         err = obd_connect(&mdc_conn, obd, &mdc_uuid, 0);
582         if (err) {
583                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
584                 GOTO(out_cleanup, err);
585         }
586
587         exp = class_conn2export(&mdc_conn);
588
589         ctxt = llog_get_context(&exp->exp_obd->obd_llogs,LLOG_CONFIG_REPL_CTXT);
590         rc = class_config_process_llog(ctxt, profile, cfg);
591         if (rc)
592                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
593
594         err = obd_disconnect(exp, 0);
595
596 out_cleanup:
597         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
598         err = class_process_config(&lcfg);
599         if (err < 0)
600                 GOTO(out, err);
601
602 out_detach:
603         LCFG_INIT(lcfg, LCFG_DETACH, name);
604         err = class_process_config(&lcfg);
605         if (err < 0)
606                 GOTO(out, err);
607
608 out_del_uuid:
609         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
610         lcfg.lcfg_inllen1 = strlen(peer) + 1;
611         lcfg.lcfg_inlbuf1 = peer;
612         err = class_process_config(&lcfg);
613
614 out_del_conn:
615         if (lmd->lmd_nal == SOCKNAL) {
616                 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
617                 pcfg.pcfg_nal     = lmd->lmd_nal;
618                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
619                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
620                 pcfg.pcfg_flags   = 1; /*share*/
621                 err = libcfs_nal_cmd(&pcfg);
622                 if (err <0)
623                         GOTO(out, err);
624         }
625 out:
626         if (rc == 0)
627                 rc = err;
628
629         RETURN(rc);
630 }
631
632 int lustre_fill_super(struct super_block *sb, void *data, int silent)
633 {
634         struct lustre_mount_data * lmd = data;
635         struct ll_sb_info *sbi;
636         char *osc = NULL;
637         char *mdc = NULL;
638         int err;
639         ENTRY;
640
641         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
642         if (lmd_bad_magic(lmd))
643                 RETURN(-EINVAL);
644
645         sbi = lustre_init_sbi(sb);
646         if (!sbi)
647                 RETURN(-ENOMEM);
648
649         sbi->ll_flags |= LL_SBI_READAHEAD;
650
651         if (lmd->lmd_profile) {
652                 struct lustre_profile *lprof;
653                 struct config_llog_instance cfg;
654                 int len;
655
656                 if (lmd->lmd_mds[0] == '\0') {
657                         CERROR("no mds name\n");
658                         GOTO(out_free, err = -EINVAL);
659                 }
660
661                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
662                 if (sbi->ll_lmd == NULL)
663                         GOTO(out_free, err = -ENOMEM);
664                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
665
666                 /* generate a string unique to this super, let's try
667                  the address of the super itself.*/
668                 len = (sizeof(sb) * 2) + 1;
669                 OBD_ALLOC(sbi->ll_instance, len);
670                 if (sbi->ll_instance == NULL)
671                         GOTO(out_free, err = -ENOMEM);
672                 sprintf(sbi->ll_instance, "%p", sb);
673
674                 cfg.cfg_instance = sbi->ll_instance;
675                 cfg.cfg_uuid = sbi->ll_sb_uuid;
676                 cfg.cfg_local_nid = lmd->lmd_local_nid;
677                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
678                 if (err < 0) {
679                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
680                         GOTO(out_free, err);
681                 }
682
683                 lprof = class_get_profile(lmd->lmd_profile);
684                 if (lprof == NULL) {
685                         CERROR("No profile found: %s\n", lmd->lmd_profile);
686                         GOTO(out_free, err = -EINVAL);
687                 }
688                 if (osc)
689                         OBD_FREE(osc, strlen(osc) + 1);
690                 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
691                           strlen(sbi->ll_instance) + 2);
692                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
693
694                 if (mdc)
695                         OBD_FREE(mdc, strlen(mdc) + 1);
696                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
697                           strlen(sbi->ll_instance) + 2);
698                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
699         }
700
701         if (!osc) {
702                 CERROR("no osc\n");
703                 GOTO(out_free, err = -EINVAL);
704         }
705
706         if (!mdc) {
707                 CERROR("no mdc\n");
708                 GOTO(out_free, err = -EINVAL);
709         }
710
711         err = lustre_common_fill_super(sb, mdc, osc);
712
713         if (err)
714                 GOTO(out_free, err);
715
716 #if CONFIG_SNAPFS
717         if (lmd->lmd_clone_index) {
718                 err = lustre_set_clone_info(sb, lmd->lmd_clone_index);                
719         }
720         
721         if (err)
722                 GOTO(out_free, err);
723 #endif
724         
725 out_dev:
726         if (mdc)
727                 OBD_FREE(mdc, strlen(mdc) + 1);
728         if (osc)
729                 OBD_FREE(osc, strlen(osc) + 1);
730
731         RETURN(err);
732
733 out_free:
734         if (sbi->ll_lmd) {
735                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
736                 int err;
737
738                 if (sbi->ll_instance != NULL) {
739                         struct lustre_mount_data *lmd = sbi->ll_lmd;
740                         char * cln_prof;
741                         struct config_llog_instance cfg;
742
743                         cfg.cfg_instance = sbi->ll_instance;
744                         cfg.cfg_uuid = sbi->ll_sb_uuid;
745
746                         OBD_ALLOC(cln_prof, len);
747                         sprintf(cln_prof, "%s-clean", lmd->lmd_profile);
748
749                         err = lustre_process_log(lmd, cln_prof, &cfg, 0);
750                         if (err < 0)
751                                 CERROR("Unable to process log: %s\n", cln_prof);
752                         OBD_FREE(cln_prof, len);
753                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
754                 }
755                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
756         }
757 #if CONFIG_SNAPFS
758         lustre_cleanup_clone_info(sb);
759 #endif
760         lustre_free_sbi(sb);
761
762         goto out_dev;
763 } /* lustre_fill_super */
764
765 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
766 {
767         struct lustre_cfg lcfg;
768         struct obd_device *obd;
769         int next = 0;
770
771         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
772         {
773                 int err;
774
775                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
776                 err = class_process_config(&lcfg);
777                 if (err) {
778                         CERROR("cleanup failed: %s\n", obd->obd_name);
779                         //continue;
780                 }
781
782                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
783                 err = class_process_config(&lcfg);
784                 if (err) {
785                         CERROR("detach failed: %s\n", obd->obd_name);
786                         //continue;
787                 }
788         }
789
790         if (sbi->ll_lmd != NULL)
791                 class_del_profile(sbi->ll_lmd->lmd_profile);
792 }
793
794 void lustre_put_super(struct super_block *sb)
795 {
796         struct obd_device *obd;
797         struct ll_sb_info *sbi = ll_s2sbi(sb);
798         int force_umount = 0;
799         ENTRY;
800
801         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
802         obd = class_exp2obd(sbi->ll_mdc_exp);
803         if (obd)
804                 force_umount = obd->obd_no_recov;
805         obd = NULL;
806
807 #if CONFIG_SNAPFS
808         lustre_cleanup_clone_info(sb);
809 #endif
810
811         lustre_common_put_super(sb);
812         if (sbi->ll_lmd != NULL) {
813                 char * cln_prof;
814                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
815                 int err;
816                 struct config_llog_instance cfg;
817
818                 if (force_umount) {
819                         CERROR("force umount, doing manual cleanup\n");
820                         lustre_manual_cleanup(sbi);
821                         GOTO(free_lmd, 0);
822                 }
823
824                 cfg.cfg_instance = sbi->ll_instance;
825                 cfg.cfg_uuid = sbi->ll_sb_uuid;
826
827                 OBD_ALLOC(cln_prof, len);
828                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
829
830                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
831                 if (err < 0) {
832                         CERROR("Unable to process log: %s, doing manual cleanup"
833                                "\n", cln_prof);
834                         lustre_manual_cleanup(sbi);
835                 }
836
837                 OBD_FREE(cln_prof, len);
838         free_lmd:
839                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
840                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
841         }
842
843         lustre_free_sbi(sb);
844
845         EXIT;
846 } /* lustre_put_super */
847
848 int ll_process_config_update(struct ll_sb_info *sbi, int clean)
849 {
850         struct obd_export *mdc_exp = sbi->ll_mdc_exp;
851         struct lustre_mount_data *lmd = sbi->ll_lmd;
852         struct llog_ctxt *ctxt;
853         struct config_llog_instance cfg;
854         char *profile = lmd->lmd_profile, *name = NULL;
855         int rc, namelen =  0, version;
856         ENTRY;
857
858         if (profile == NULL)
859                 RETURN(0);
860         if (lmd == NULL) {
861                 CERROR("Client not mounted with zero-conf; cannot process "
862                        "update log.\n");
863                 RETURN(0);
864         }
865
866         rc = ldlm_cli_cancel_unused(mdc_exp->exp_obd->obd_namespace, NULL,
867                                     LDLM_FL_CONFIG_CHANGE, NULL);
868         if (rc != 0)
869                 CWARN("ldlm_cli_cancel_unused(mdc): %d\n", rc);
870
871         rc = obd_cancel_unused(sbi->ll_osc_exp, NULL, LDLM_FL_CONFIG_CHANGE,
872                                NULL);
873         if (rc != 0)
874                 CWARN("obd_cancel_unused(lov): %d\n", rc);
875
876         cfg.cfg_instance = sbi->ll_instance;
877         cfg.cfg_uuid = sbi->ll_sb_uuid;
878         cfg.cfg_local_nid = lmd->lmd_local_nid;
879
880         namelen = strlen(profile) + 20; /* -clean-######### */
881         OBD_ALLOC(name, namelen);
882         if (name == NULL)
883                 RETURN(-ENOMEM);
884
885         if (clean) {
886                 version = sbi->ll_config_version - 1;
887                 sprintf(name, "%s-clean-%d", profile, version);
888         } else {
889                 version = sbi->ll_config_version + 1;
890                 sprintf(name, "%s-%d", profile, version);
891         }
892
893         CWARN("Applying configuration log %s\n", name);
894
895         ctxt = llog_get_context(&mdc_exp->exp_obd->obd_llogs,
896                                 LLOG_CONFIG_REPL_CTXT);
897         rc = class_config_process_llog(ctxt, name, &cfg);
898         if (rc == 0)
899                 sbi->ll_config_version = version;
900         CWARN("Finished applying configuration log %s: %d\n", name, rc);
901
902         if (rc == 0 && clean == 0) {
903                 struct lov_desc desc;
904                 int rc, valsize;
905                 valsize = sizeof(desc);
906                 rc = obd_get_info(sbi->ll_osc_exp, strlen("lovdesc") + 1,
907                                   "lovdesc", &valsize, &desc);
908
909                 rc = obd_init_ea_size(mdc_exp,
910                                       obd_size_diskmd(sbi->ll_osc_exp, NULL),
911                                       (desc.ld_tgt_count *
912                                        sizeof(struct llog_cookie)));
913         }
914         OBD_FREE(name, namelen);
915         RETURN(rc);
916 }
917
918 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
919 {
920         struct inode *inode = NULL;
921         l_lock(&lock->l_resource->lr_namespace->ns_lock);
922         if (lock->l_ast_data) {
923                 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
924                 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
925                         inode = igrab(lock->l_ast_data);
926                 } else {
927                         CERROR("DEBUG: l_ast_data %p is bogus: magic %x\n",
928                                lock->l_ast_data, lli->lli_inode_magic);
929                 }
930         }
931         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
932         return inode;
933 }
934
935 int null_if_equal(struct ldlm_lock *lock, void *data)
936 {
937         if (data == lock->l_ast_data)
938                 lock->l_ast_data = NULL;
939
940         if (lock->l_req_mode != lock->l_granted_mode)
941                 return LDLM_ITER_STOP;
942
943         return LDLM_ITER_CONTINUE;
944 }
945
946 void ll_clear_inode(struct inode *inode)
947 {
948         struct ll_fid fid;
949         struct ll_inode_info *lli = ll_i2info(inode);
950         struct ll_sb_info *sbi = ll_i2sbi(inode);
951         ENTRY;
952
953         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
954                inode->i_generation, inode);
955
956         lli->lli_inode_magic = LLI_INODE_DEAD;
957         ll_inode2fid(&fid, inode);
958         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
959         md_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
960
961         if (lli->lli_smd)
962                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
963                                   null_if_equal, inode);
964
965         if (lli->lli_smd) {
966                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
967                 lli->lli_smd = NULL;
968         }
969
970         if (lli->lli_mea) {
971                 /* FIXME: change cbdata for mea here */
972                 obd_free_memmd(sbi->ll_mdc_exp,
973                                (struct lov_stripe_md **) &lli->lli_mea);
974                 lli->lli_mea = NULL;
975         }
976
977         if (lli->lli_symlink_name) {
978                 OBD_FREE(lli->lli_symlink_name,
979                          strlen(lli->lli_symlink_name) + 1);
980                 lli->lli_symlink_name = NULL;
981         }
982
983         EXIT;
984 }
985
986 /* If this inode has objects allocated to it (lsm != NULL), then the OST
987  * object(s) determine the file size and mtime.  Otherwise, the MDS will
988  * keep these values until such a time that objects are allocated for it.
989  * We do the MDS operations first, as it is checking permissions for us.
990  * We don't to the MDS RPC if there is nothing that we want to store there,
991  * otherwise there is no harm in updating mtime/atime on the MDS if we are
992  * going to do an RPC anyways.
993  *
994  * If we are doing a truncate, we will send the mtime and ctime updates
995  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
996  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
997  * at the same time.
998  */
999 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
1000 {
1001         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1002         struct ll_sb_info *sbi = ll_i2sbi(inode);
1003         struct ptlrpc_request *request = NULL;
1004         struct mdc_op_data op_data;
1005         int ia_valid = attr->ia_valid;
1006         int rc = 0;
1007         ENTRY;
1008
1009         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1010         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
1011
1012         if (ia_valid & ATTR_SIZE) {
1013                 if (attr->ia_size > ll_file_maxbytes(inode)) {
1014                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
1015                                attr->ia_size, ll_file_maxbytes(inode));
1016                         RETURN(-EFBIG);
1017                 }
1018
1019                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1020         }
1021
1022         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
1023         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
1024                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1025                         RETURN(-EPERM);
1026         }
1027
1028         /* We mark all of the fields "set" so MDS/OST does not re-set them */
1029         if (attr->ia_valid & ATTR_CTIME) {
1030                 attr->ia_ctime = CURRENT_TIME;
1031                 attr->ia_valid |= ATTR_CTIME_SET;
1032         }
1033         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
1034                 attr->ia_atime = CURRENT_TIME;
1035                 attr->ia_valid |= ATTR_ATIME_SET;
1036         }
1037         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
1038                 attr->ia_mtime = CURRENT_TIME;
1039                 attr->ia_valid |= ATTR_MTIME_SET;
1040         }
1041
1042         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
1043                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
1044                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
1045                        LTIME_S(CURRENT_TIME));
1046         if (lsm)
1047                 attr->ia_valid &= ~ATTR_SIZE;
1048
1049         /* If only OST attributes being set on objects, don't do MDS RPC.
1050          * In that case, we need to check permissions and update the local
1051          * inode ourselves so we can call obdo_from_inode() always. */
1052         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
1053                 struct lustre_md md;
1054                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1055
1056                 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1057                                 attr, NULL, 0, NULL, 0, &request);
1058                 if (rc) {
1059                         ptlrpc_req_finished(request);
1060                         if (rc != -EPERM && rc != -EACCES)
1061                                 CERROR("md_setattr fails: rc = %d\n", rc);
1062                         RETURN(rc);
1063                 }
1064
1065                 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0, 
1066                                        sbi->ll_osc_exp, &md);
1067                 if (rc) {
1068                         ptlrpc_req_finished(request);
1069                         RETURN(rc);
1070                 }
1071
1072                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
1073                  * but needed to set timestamps backwards on utime. */
1074                 inode_setattr(inode, attr);
1075                 ll_update_inode(inode, &md);
1076                 ptlrpc_req_finished(request);
1077
1078                 if (!lsm || !S_ISREG(inode->i_mode)) {
1079                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
1080                         RETURN(0);
1081                 }
1082         } else {
1083                 /* The OST doesn't check permissions, but the alternative is
1084                  * a gratuitous RPC to the MDS.  We already rely on the client
1085                  * to do read/write/truncate permission checks, so is mtime OK?
1086                  */
1087                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
1088                         /* from sys_utime() */
1089                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
1090                                 if (current->fsuid != inode->i_uid &&
1091                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
1092                                         RETURN(rc);
1093                         } else {
1094                                 /* from inode_change_ok() */
1095                                 if (current->fsuid != inode->i_uid &&
1096                                     !capable(CAP_FOWNER))
1097                                         RETURN(-EPERM);
1098                         }
1099                 }
1100
1101                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
1102                 inode_setattr(inode, attr);
1103         }
1104
1105         /* We really need to get our PW lock before we change inode->i_size.
1106          * If we don't we can race with other i_size updaters on our node, like
1107          * ll_file_read.  We can also race with i_size propogation to other
1108          * nodes through dirtying and writeback of final cached pages.  This
1109          * last one is especially bad for racing o_append users on other
1110          * nodes. */
1111         if (ia_valid & ATTR_SIZE) {
1112                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
1113                                                            OBD_OBJECT_EOF } };
1114                 struct lustre_handle lockh = { 0 };
1115                 int err, ast_flags = 0;
1116                 /* XXX when we fix the AST intents to pass the discard-range
1117                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
1118                  * XXX here. */
1119                 if (attr->ia_size == 0)
1120                         ast_flags = LDLM_AST_DISCARD_DATA;
1121
1122                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
1123                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
1124                 up(&inode->i_sem);
1125                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
1126                                     ast_flags);
1127                 down(&inode->i_sem);
1128                 if (rc != 0)
1129                         RETURN(rc);
1130
1131                 rc = vmtruncate(inode, attr->ia_size);
1132
1133                 /* We need to drop the semaphore here, because this unlock may
1134                  * result in a cancellation, which will need the i_sem */
1135                 up(&inode->i_sem);
1136                 /* unlock now as we don't mind others file lockers racing with
1137                  * the mds updates below? */
1138                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
1139                 down(&inode->i_sem);
1140                 if (err) {
1141                         CERROR("ll_extent_unlock failed: %d\n", err);
1142                         if (!rc)
1143                                 rc = err;
1144                 }
1145         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
1146                 struct obdo oa;
1147
1148                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
1149                        inode->i_ino, LTIME_S(attr->ia_mtime));
1150                 oa.o_id = lsm->lsm_object_id;
1151                 oa.o_gr = lsm->lsm_object_gr;
1152                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1153                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1154                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1155                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
1156                 if (rc)
1157                         CERROR("obd_setattr fails: rc=%d\n", rc);
1158         }
1159         RETURN(rc);
1160 }
1161
1162 int ll_setattr(struct dentry *de, struct iattr *attr)
1163 {
1164         LBUG(); /* code is unused, but leave this in case of VFS changes */
1165         RETURN(-ENOSYS);
1166 }
1167
1168 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1169                        unsigned long max_age)
1170 {
1171         struct ll_sb_info *sbi = ll_s2sbi(sb);
1172         struct obd_statfs obd_osfs;
1173         int rc;
1174         ENTRY;
1175
1176         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1177         if (rc) {
1178                 CERROR("mdc_statfs fails: rc = %d\n", rc);
1179                 RETURN(rc);
1180         }
1181
1182         osfs->os_type = sb->s_magic;
1183
1184         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1185                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1186
1187         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1188         if (rc) {
1189                 CERROR("obd_statfs fails: rc = %d\n", rc);
1190                 RETURN(rc);
1191         }
1192
1193         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1194                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1195                obd_osfs.os_files);
1196
1197         osfs->os_blocks = obd_osfs.os_blocks;
1198         osfs->os_bfree = obd_osfs.os_bfree;
1199         osfs->os_bavail = obd_osfs.os_bavail;
1200
1201         /* If we don't have as many objects free on the OST as inodes
1202          * on the MDS, we reduce the total number of inodes to
1203          * compensate, so that the "inodes in use" number is correct.
1204          */
1205         if (obd_osfs.os_ffree < osfs->os_ffree) {
1206                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1207                         obd_osfs.os_ffree;
1208                 osfs->os_ffree = obd_osfs.os_ffree;
1209         }
1210
1211         RETURN(rc);
1212 }
1213
1214 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1215 {
1216         struct obd_statfs osfs;
1217         int rc;
1218
1219         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1220         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1221
1222         /* For now we will always get up-to-date statfs values, but in the
1223          * future we may allow some amount of caching on the client (e.g.
1224          * from QOS or lprocfs updates). */
1225         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1226         if (rc)
1227                 return rc;
1228
1229         statfs_unpack(sfs, &osfs);
1230
1231         if (sizeof(sfs->f_blocks) == 4) {
1232                 while (osfs.os_blocks > ~0UL) {
1233                         sfs->f_bsize <<= 1;
1234
1235                         osfs.os_blocks >>= 1;
1236                         osfs.os_bfree >>= 1;
1237                         osfs.os_bavail >>= 1;
1238                 }
1239         }
1240
1241         sfs->f_blocks = osfs.os_blocks;
1242         sfs->f_bfree = osfs.os_bfree;
1243         sfs->f_bavail = osfs.os_bavail;
1244
1245         return 0;
1246 }
1247
1248 void dump_lsm(int level, struct lov_stripe_md *lsm)
1249 {
1250         CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1251                "stripe_size %u, stripe_count %u\n",
1252                lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1253                lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1254 }
1255
1256 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1257 {
1258         struct ll_inode_info *lli = ll_i2info(inode);
1259         struct mds_body *body = md->body;
1260         struct lov_stripe_md *lsm = md->lsm;
1261         struct mea *mea = md->mea;
1262         ENTRY;
1263
1264         LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1265         LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1266         if (lsm != NULL) {
1267                 LASSERT(lsm->lsm_object_gr > 0);
1268                 if (lli->lli_smd == NULL) {
1269                         lli->lli_smd = lsm;
1270                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1271                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1272                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1273                 } else {
1274                         int i;
1275                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1276                                 CERROR("lsm mismatch for inode %ld\n",
1277                                        inode->i_ino);
1278                                 CERROR("lli_smd:\n");
1279                                 dump_lsm(D_ERROR, lli->lli_smd);
1280                                 CERROR("lsm:\n");
1281                                 dump_lsm(D_ERROR, lsm);
1282                                 LBUG();
1283                         }
1284                         /* XXX FIXME -- We should decide on a safer (atomic) and
1285                          * more elegant way to update the lsm */
1286                         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1287                                 lli->lli_smd->lsm_oinfo[i].loi_id =
1288                                         lsm->lsm_oinfo[i].loi_id;
1289                                 lli->lli_smd->lsm_oinfo[i].loi_gr =
1290                                         lsm->lsm_oinfo[i].loi_gr;
1291                                 lli->lli_smd->lsm_oinfo[i].loi_ost_idx =
1292                                         lsm->lsm_oinfo[i].loi_ost_idx;
1293                                 lli->lli_smd->lsm_oinfo[i].loi_ost_gen =
1294                                         lsm->lsm_oinfo[i].loi_ost_gen;
1295                         }
1296                 }
1297                 /* bug 2844 - limit i_blksize for broken user-space apps */
1298                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1299                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1300                 if (lli->lli_smd != lsm)
1301                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1302         }
1303
1304         if (mea != NULL) {
1305                 if (lli->lli_mea == NULL) {
1306                         lli->lli_mea = mea;
1307                 } else {
1308                         if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1309                                 CERROR("mea mismatch for inode %lu\n",
1310                                         inode->i_ino);
1311                                 LBUG();
1312                         }
1313                 }
1314                 if (lli->lli_mea != mea)
1315                         obd_free_memmd(ll_i2mdcexp(inode),
1316                                         (struct lov_stripe_md **) &mea);
1317                         
1318         }
1319
1320         if (body->valid & OBD_MD_FLID)
1321                 inode->i_ino = body->ino;
1322         if (body->valid & OBD_MD_FLATIME)
1323                 LTIME_S(inode->i_atime) = body->atime;
1324         if (body->valid & OBD_MD_FLMTIME &&
1325             body->mtime > LTIME_S(inode->i_mtime)) {
1326                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1327                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1328                 LTIME_S(inode->i_mtime) = body->mtime;
1329         }
1330         if (body->valid & OBD_MD_FLCTIME &&
1331             body->ctime > LTIME_S(inode->i_ctime))
1332                 LTIME_S(inode->i_ctime) = body->ctime;
1333         if (body->valid & OBD_MD_FLMODE)
1334                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1335         if (body->valid & OBD_MD_FLTYPE)
1336                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1337         if (body->valid & OBD_MD_FLUID)
1338                 inode->i_uid = body->uid;
1339         if (body->valid & OBD_MD_FLGID)
1340                 inode->i_gid = body->gid;
1341         if (body->valid & OBD_MD_FLFLAGS)
1342                 inode->i_flags = body->flags;
1343         if (body->valid & OBD_MD_FLNLINK)
1344                 inode->i_nlink = body->nlink;
1345         if (body->valid & OBD_MD_FLGENER)
1346                 inode->i_generation = body->generation;
1347         if (body->valid & OBD_MD_FLRDEV)
1348 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1349                 inode->i_rdev = body->rdev;
1350 #else
1351                 inode->i_rdev = old_decode_dev(body->rdev);
1352 #endif
1353         if (body->valid & OBD_MD_FLSIZE)
1354                 inode->i_size = body->size;
1355         if (body->valid & OBD_MD_FLBLOCKS)
1356                 inode->i_blocks = body->blocks;
1357
1358         if (body->valid & OBD_MD_FLSIZE)
1359                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1360
1361         lli->lli_mds = body->mds;
1362         inode->i_dev = (kdev_t) body->mds;
1363         LASSERT(body->mds < 1000);
1364 }
1365
1366 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1367 static struct backing_dev_info ll_backing_dev_info = {
1368         .ra_pages       = 0,    /* No readahead */
1369         .memory_backed  = 0,    /* Does contribute to dirty memory */
1370 };
1371 #endif
1372
1373 void ll_read_inode2(struct inode *inode, void *opaque)
1374 {
1375         struct lustre_md *md = opaque;
1376         struct ll_inode_info *lli = ll_i2info(inode);
1377         ENTRY;
1378
1379         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1380                inode->i_generation, inode);
1381
1382         ll_lli_init(lli);
1383
1384         LASSERT(!lli->lli_smd);
1385
1386         /* Core attributes from the MDS first.  This is a new inode, and
1387          * the VFS doesn't zero times in the core inode so we have to do
1388          * it ourselves.  They will be overwritten by either MDS or OST
1389          * attributes - we just need to make sure they aren't newer. */
1390         LTIME_S(inode->i_mtime) = 0;
1391         LTIME_S(inode->i_atime) = 0;
1392         LTIME_S(inode->i_ctime) = 0;
1393
1394         inode->i_rdev = 0;
1395         ll_update_inode(inode, md);
1396
1397         /* OIDEBUG(inode); */
1398
1399         if (S_ISREG(inode->i_mode)) {
1400                 inode->i_op = &ll_file_inode_operations;
1401                 inode->i_fop = &ll_file_operations;
1402                 inode->i_mapping->a_ops = &ll_aops;
1403                 EXIT;
1404         } else if (S_ISDIR(inode->i_mode)) {
1405                 inode->i_op = &ll_dir_inode_operations;
1406                 inode->i_fop = &ll_dir_operations;
1407                 inode->i_mapping->a_ops = &ll_dir_aops;
1408                 EXIT;
1409         } else if (S_ISLNK(inode->i_mode)) {
1410                 inode->i_op = &ll_fast_symlink_inode_operations;
1411                 EXIT;
1412         } else {
1413                 inode->i_op = &ll_special_inode_operations;
1414
1415 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1416                 init_special_inode(inode, inode->i_mode,
1417                                    kdev_t_to_nr(inode->i_rdev));
1418
1419                 /* initializing backing dev info. */
1420                 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1421 #else
1422                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1423 #endif
1424                 lli->ll_save_ifop = inode->i_fop;
1425
1426                 if (S_ISCHR(inode->i_mode))
1427                         inode->i_fop = &ll_special_chr_inode_fops;
1428                 else if (S_ISBLK(inode->i_mode))
1429                         inode->i_fop = &ll_special_blk_inode_fops;
1430                 else if (S_ISFIFO(inode->i_mode))
1431                         inode->i_fop = &ll_special_fifo_inode_fops;
1432                 else if (S_ISSOCK(inode->i_mode))
1433                         inode->i_fop = &ll_special_sock_inode_fops;
1434
1435                 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1436                       inode->i_fop);
1437
1438                 if (lli->ll_save_ifop->owner) {
1439                         CWARN("%p has owner %p\n", lli->ll_save_ifop,
1440                               lli->ll_save_ifop->owner);
1441                 }
1442                 EXIT;
1443         }
1444 }
1445
1446 void ll_delete_inode(struct inode *inode)
1447 {
1448         int rc;
1449         struct ll_fid fid;
1450         struct ll_sb_info *sbi = ll_i2sbi(inode);
1451         ENTRY;
1452         
1453         ll_inode2fid(&fid, inode);
1454
1455         rc = md_delete_object(sbi->ll_mdc_exp, &fid);
1456         if (rc) {
1457                 CERROR("md_delete_object() failed, error %d.\n",
1458                        rc);
1459         }
1460
1461         clear_inode(inode);
1462         EXIT;
1463 }
1464
1465 int ll_iocontrol(struct inode *inode, struct file *file,
1466                  unsigned int cmd, unsigned long arg)
1467 {
1468         struct ll_sb_info *sbi = ll_i2sbi(inode);
1469         struct ptlrpc_request *req = NULL;
1470         int rc, flags = 0;
1471         ENTRY;
1472
1473         switch(cmd) {
1474         case EXT3_IOC_GETFLAGS: {
1475                 struct ll_fid fid;
1476                 unsigned long valid = OBD_MD_FLFLAGS;
1477                 struct mds_body *body;
1478
1479                 ll_inode2fid(&fid, inode);
1480                 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1481                 if (rc) {
1482                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1483                         RETURN(-abs(rc));
1484                 }
1485
1486                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1487
1488                 if (body->flags & S_APPEND)
1489                         flags |= EXT3_APPEND_FL;
1490                 if (body->flags & S_IMMUTABLE)
1491                         flags |= EXT3_IMMUTABLE_FL;
1492                 if (body->flags & S_NOATIME)
1493                         flags |= EXT3_NOATIME_FL;
1494
1495                 ptlrpc_req_finished (req);
1496
1497                 RETURN(put_user(flags, (int *)arg));
1498         }
1499         case EXT3_IOC_SETFLAGS: {
1500                 struct mdc_op_data op_data;
1501                 struct iattr attr;
1502                 struct obdo *oa;
1503                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1504
1505                 if (get_user(flags, (int *)arg))
1506                         RETURN(-EFAULT);
1507
1508                 oa = obdo_alloc();
1509                 if (!oa)
1510                         RETURN(-ENOMEM);
1511
1512                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1513
1514                 memset(&attr, 0x0, sizeof(attr));
1515                 attr.ia_attr_flags = flags;
1516                 attr.ia_valid |= ATTR_ATTR_FLAG;
1517
1518                 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1519                                 &attr, NULL, 0, NULL, 0, &req);
1520                 if (rc) {
1521                         ptlrpc_req_finished(req);
1522                         if (rc != -EPERM && rc != -EACCES)
1523                                 CERROR("md_setattr fails: rc = %d\n", rc);
1524                         obdo_free(oa);
1525                         RETURN(rc);
1526                 }
1527                 ptlrpc_req_finished(req);
1528
1529                 oa->o_id = lsm->lsm_object_id;
1530                 oa->o_gr = lsm->lsm_object_gr;
1531                 oa->o_flags = flags;
1532                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1533
1534                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1535                 obdo_free(oa);
1536                 if (rc) {
1537                         if (rc != -EPERM && rc != -EACCES)
1538                                 CERROR("md_setattr fails: rc = %d\n", rc);
1539                         RETURN(rc);
1540                 }
1541
1542                 if (flags & EXT3_APPEND_FL)
1543                         inode->i_flags |= S_APPEND;
1544                 else
1545                         inode->i_flags &= ~S_APPEND;
1546                 if (flags & EXT3_IMMUTABLE_FL)
1547                         inode->i_flags |= S_IMMUTABLE;
1548                 else
1549                         inode->i_flags &= ~S_IMMUTABLE;
1550                 if (flags & EXT3_NOATIME_FL)
1551                         inode->i_flags |= S_NOATIME;
1552                 else
1553                         inode->i_flags &= ~S_NOATIME;
1554
1555                 RETURN(0);
1556         }
1557         default:
1558                 RETURN(-ENOSYS);
1559         }
1560
1561         RETURN(0);
1562 }
1563
1564 void ll_umount_begin(struct super_block *sb)
1565 {
1566         struct ll_sb_info *sbi = ll_s2sbi(sb);
1567         struct obd_device *obd;
1568         struct obd_ioctl_data ioc_data = { 0 };
1569         ENTRY;
1570         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1571
1572         obd = class_exp2obd(sbi->ll_mdc_exp);
1573         if (obd == NULL) {
1574                 CERROR("Invalid MDC connection handle "LPX64"\n",
1575                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1576                 EXIT;
1577                 return;
1578         }
1579         obd->obd_no_recov = 1;
1580         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1581                       &ioc_data, NULL);
1582
1583         obd = class_exp2obd(sbi->ll_osc_exp);
1584         if (obd == NULL) {
1585                 CERROR("Invalid LOV connection handle "LPX64"\n",
1586                        sbi->ll_osc_exp->exp_handle.h_cookie);
1587                 EXIT;
1588                 return;
1589         }
1590
1591         obd->obd_no_recov = 1;
1592         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1593                       &ioc_data, NULL);
1594
1595         /* Really, we'd like to wait until there are no requests outstanding,
1596          * and then continue.  For now, we just invalidate the requests,
1597          * schedule, and hope.
1598          */
1599         schedule();
1600
1601         EXIT;
1602 }
1603
1604 int ll_prep_inode(struct obd_export *osc_exp, struct obd_export *mdc_exp,
1605                   struct inode **inode, struct ptlrpc_request *req,
1606                   int offset, struct super_block *sb)
1607 {
1608         struct lustre_md md;
1609         int rc = 0;
1610
1611         rc = mdc_req2lustre_md(mdc_exp, req, offset, osc_exp, &md);
1612         if (rc)
1613                 RETURN(rc);
1614
1615         if (*inode) {
1616                 ll_update_inode(*inode, &md);
1617         } else {
1618                 LASSERT(sb);
1619                 *inode = ll_iget(sb, md.body->ino, &md);
1620                 if (*inode == NULL || is_bad_inode(*inode)) {
1621                         /* free the lsm if we allocated one above */
1622                         if (md.lsm != NULL)
1623                                 obd_free_memmd(osc_exp, &md.lsm);
1624                         if (md.mea != NULL)
1625                                 obd_free_memmd(mdc_exp,
1626                                                (struct lov_stripe_md**)&md.mea);
1627                         rc = -ENOMEM;
1628                         CERROR("new_inode -fatal: rc %d\n", rc);
1629                 }
1630         }
1631
1632         RETURN(rc);
1633 }