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