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