inode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * inode.c
  5. *
  6. * vfs' aops, fops, dops and iops
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/pagemap.h>
  30. #include <asm/byteorder.h>
  31. #define MLOG_MASK_PREFIX ML_INODE
  32. #include <cluster/masklog.h>
  33. #include "ocfs2.h"
  34. #include "alloc.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "file.h"
  38. #include "heartbeat.h"
  39. #include "inode.h"
  40. #include "journal.h"
  41. #include "namei.h"
  42. #include "suballoc.h"
  43. #include "super.h"
  44. #include "symlink.h"
  45. #include "sysfile.h"
  46. #include "uptodate.h"
  47. #include "xattr.h"
  48. #include "buffer_head_io.h"
  49. struct ocfs2_find_inode_args
  50. {
  51. u64 fi_blkno;
  52. unsigned long fi_ino;
  53. unsigned int fi_flags;
  54. unsigned int fi_sysfile_type;
  55. };
  56. static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
  57. static int ocfs2_read_locked_inode(struct inode *inode,
  58. struct ocfs2_find_inode_args *args);
  59. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  60. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  61. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  62. struct inode *inode,
  63. struct buffer_head *fe_bh);
  64. void ocfs2_set_inode_flags(struct inode *inode)
  65. {
  66. unsigned int flags = OCFS2_I(inode)->ip_attr;
  67. inode->i_flags &= ~(S_IMMUTABLE |
  68. S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  69. if (flags & OCFS2_IMMUTABLE_FL)
  70. inode->i_flags |= S_IMMUTABLE;
  71. if (flags & OCFS2_SYNC_FL)
  72. inode->i_flags |= S_SYNC;
  73. if (flags & OCFS2_APPEND_FL)
  74. inode->i_flags |= S_APPEND;
  75. if (flags & OCFS2_NOATIME_FL)
  76. inode->i_flags |= S_NOATIME;
  77. if (flags & OCFS2_DIRSYNC_FL)
  78. inode->i_flags |= S_DIRSYNC;
  79. }
  80. /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  81. void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  82. {
  83. unsigned int flags = oi->vfs_inode.i_flags;
  84. oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  85. OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  86. if (flags & S_SYNC)
  87. oi->ip_attr |= OCFS2_SYNC_FL;
  88. if (flags & S_APPEND)
  89. oi->ip_attr |= OCFS2_APPEND_FL;
  90. if (flags & S_IMMUTABLE)
  91. oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  92. if (flags & S_NOATIME)
  93. oi->ip_attr |= OCFS2_NOATIME_FL;
  94. if (flags & S_DIRSYNC)
  95. oi->ip_attr |= OCFS2_DIRSYNC_FL;
  96. }
  97. struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
  98. int sysfile_type)
  99. {
  100. struct inode *inode = NULL;
  101. struct super_block *sb = osb->sb;
  102. struct ocfs2_find_inode_args args;
  103. mlog_entry("(blkno = %llu)\n", (unsigned long long)blkno);
  104. /* Ok. By now we've either got the offsets passed to us by the
  105. * caller, or we just pulled them off the bh. Lets do some
  106. * sanity checks to make sure they're OK. */
  107. if (blkno == 0) {
  108. inode = ERR_PTR(-EINVAL);
  109. mlog_errno(PTR_ERR(inode));
  110. goto bail;
  111. }
  112. args.fi_blkno = blkno;
  113. args.fi_flags = flags;
  114. args.fi_ino = ino_from_blkno(sb, blkno);
  115. args.fi_sysfile_type = sysfile_type;
  116. inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  117. ocfs2_init_locked_inode, &args);
  118. /* inode was *not* in the inode cache. 2.6.x requires
  119. * us to do our own read_inode call and unlock it
  120. * afterwards. */
  121. if (inode && inode->i_state & I_NEW) {
  122. mlog(0, "Inode was not in inode cache, reading it.\n");
  123. ocfs2_read_locked_inode(inode, &args);
  124. unlock_new_inode(inode);
  125. }
  126. if (inode == NULL) {
  127. inode = ERR_PTR(-ENOMEM);
  128. mlog_errno(PTR_ERR(inode));
  129. goto bail;
  130. }
  131. if (is_bad_inode(inode)) {
  132. iput(inode);
  133. inode = ERR_PTR(-ESTALE);
  134. goto bail;
  135. }
  136. bail:
  137. if (!IS_ERR(inode)) {
  138. mlog(0, "returning inode with number %llu\n",
  139. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  140. mlog_exit_ptr(inode);
  141. }
  142. return inode;
  143. }
  144. /*
  145. * here's how inodes get read from disk:
  146. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  147. * found? : return the in-memory inode
  148. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  149. */
  150. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  151. {
  152. struct ocfs2_find_inode_args *args = NULL;
  153. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  154. int ret = 0;
  155. mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
  156. args = opaque;
  157. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  158. if (oi->ip_blkno != args->fi_blkno)
  159. goto bail;
  160. ret = 1;
  161. bail:
  162. mlog_exit(ret);
  163. return ret;
  164. }
  165. /*
  166. * initialize the new inode, but don't do anything that would cause
  167. * us to sleep.
  168. * return 0 on success, 1 on failure
  169. */
  170. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  171. {
  172. struct ocfs2_find_inode_args *args = opaque;
  173. mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
  174. inode->i_ino = args->fi_ino;
  175. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  176. if (args->fi_sysfile_type != 0)
  177. lockdep_set_class(&inode->i_mutex,
  178. &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
  179. mlog_exit(0);
  180. return 0;
  181. }
  182. void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  183. int create_ino)
  184. {
  185. struct super_block *sb;
  186. struct ocfs2_super *osb;
  187. int use_plocks = 1;
  188. mlog_entry("(0x%p, size:%llu)\n", inode,
  189. (unsigned long long)le64_to_cpu(fe->i_size));
  190. sb = inode->i_sb;
  191. osb = OCFS2_SB(sb);
  192. if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  193. ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
  194. use_plocks = 0;
  195. /*
  196. * These have all been checked by ocfs2_read_inode_block() or set
  197. * by ocfs2_mknod_locked(), so a failure is a code bug.
  198. */
  199. BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode
  200. cannot create a superblock
  201. inode today. change if
  202. that is needed. */
  203. BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
  204. BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
  205. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  206. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  207. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  208. inode->i_version = 1;
  209. inode->i_generation = le32_to_cpu(fe->i_generation);
  210. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  211. inode->i_mode = le16_to_cpu(fe->i_mode);
  212. inode->i_uid = le32_to_cpu(fe->i_uid);
  213. inode->i_gid = le32_to_cpu(fe->i_gid);
  214. /* Fast symlinks will have i_size but no allocated clusters. */
  215. if (S_ISLNK(inode->i_mode) && !fe->i_clusters)
  216. inode->i_blocks = 0;
  217. else
  218. inode->i_blocks = ocfs2_inode_sector_count(inode);
  219. inode->i_mapping->a_ops = &ocfs2_aops;
  220. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  221. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  222. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  223. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  224. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  225. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  226. if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  227. mlog(ML_ERROR,
  228. "ip_blkno %llu != i_blkno %llu!\n",
  229. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  230. (unsigned long long)le64_to_cpu(fe->i_blkno));
  231. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  232. if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL))
  233. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
  234. if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  235. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  236. mlog(0, "local alloc inode: i_ino=%lu\n", inode->i_ino);
  237. } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  238. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  239. } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
  240. mlog(0, "superblock inode: i_ino=%lu\n", inode->i_ino);
  241. /* we can't actually hit this as read_inode can't
  242. * handle superblocks today ;-) */
  243. BUG();
  244. }
  245. switch (inode->i_mode & S_IFMT) {
  246. case S_IFREG:
  247. if (use_plocks)
  248. inode->i_fop = &ocfs2_fops;
  249. else
  250. inode->i_fop = &ocfs2_fops_no_plocks;
  251. inode->i_op = &ocfs2_file_iops;
  252. i_size_write(inode, le64_to_cpu(fe->i_size));
  253. break;
  254. case S_IFDIR:
  255. inode->i_op = &ocfs2_dir_iops;
  256. if (use_plocks)
  257. inode->i_fop = &ocfs2_dops;
  258. else
  259. inode->i_fop = &ocfs2_dops_no_plocks;
  260. i_size_write(inode, le64_to_cpu(fe->i_size));
  261. break;
  262. case S_IFLNK:
  263. if (ocfs2_inode_is_fast_symlink(inode))
  264. inode->i_op = &ocfs2_fast_symlink_inode_operations;
  265. else
  266. inode->i_op = &ocfs2_symlink_inode_operations;
  267. i_size_write(inode, le64_to_cpu(fe->i_size));
  268. break;
  269. default:
  270. inode->i_op = &ocfs2_special_file_iops;
  271. init_special_inode(inode, inode->i_mode,
  272. inode->i_rdev);
  273. break;
  274. }
  275. if (create_ino) {
  276. inode->i_ino = ino_from_blkno(inode->i_sb,
  277. le64_to_cpu(fe->i_blkno));
  278. /*
  279. * If we ever want to create system files from kernel,
  280. * the generation argument to
  281. * ocfs2_inode_lock_res_init() will have to change.
  282. */
  283. BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
  284. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  285. OCFS2_LOCK_TYPE_META, 0, inode);
  286. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  287. OCFS2_LOCK_TYPE_OPEN, 0, inode);
  288. }
  289. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
  290. OCFS2_LOCK_TYPE_RW, inode->i_generation,
  291. inode);
  292. ocfs2_set_inode_flags(inode);
  293. mlog_exit_void();
  294. }
  295. static int ocfs2_read_locked_inode(struct inode *inode,
  296. struct ocfs2_find_inode_args *args)
  297. {
  298. struct super_block *sb;
  299. struct ocfs2_super *osb;
  300. struct ocfs2_dinode *fe;
  301. struct buffer_head *bh = NULL;
  302. int status, can_lock;
  303. u32 generation = 0;
  304. mlog_entry("(0x%p, 0x%p)\n", inode, args);
  305. status = -EINVAL;
  306. if (inode == NULL || inode->i_sb == NULL) {
  307. mlog(ML_ERROR, "bad inode\n");
  308. return status;
  309. }
  310. sb = inode->i_sb;
  311. osb = OCFS2_SB(sb);
  312. if (!args) {
  313. mlog(ML_ERROR, "bad inode args\n");
  314. make_bad_inode(inode);
  315. return status;
  316. }
  317. /*
  318. * To improve performance of cold-cache inode stats, we take
  319. * the cluster lock here if possible.
  320. *
  321. * Generally, OCFS2 never trusts the contents of an inode
  322. * unless it's holding a cluster lock, so taking it here isn't
  323. * a correctness issue as much as it is a performance
  324. * improvement.
  325. *
  326. * There are three times when taking the lock is not a good idea:
  327. *
  328. * 1) During startup, before we have initialized the DLM.
  329. *
  330. * 2) If we are reading certain system files which never get
  331. * cluster locks (local alloc, truncate log).
  332. *
  333. * 3) If the process doing the iget() is responsible for
  334. * orphan dir recovery. We're holding the orphan dir lock and
  335. * can get into a deadlock with another process on another
  336. * node in ->delete_inode().
  337. *
  338. * #1 and #2 can be simply solved by never taking the lock
  339. * here for system files (which are the only type we read
  340. * during mount). It's a heavier approach, but our main
  341. * concern is user-accesible files anyway.
  342. *
  343. * #3 works itself out because we'll eventually take the
  344. * cluster lock before trusting anything anyway.
  345. */
  346. can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  347. && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
  348. && !ocfs2_mount_local(osb);
  349. /*
  350. * To maintain backwards compatibility with older versions of
  351. * ocfs2-tools, we still store the generation value for system
  352. * files. The only ones that actually matter to userspace are
  353. * the journals, but it's easier and inexpensive to just flag
  354. * all system files similarly.
  355. */
  356. if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  357. generation = osb->fs_generation;
  358. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  359. OCFS2_LOCK_TYPE_META,
  360. generation, inode);
  361. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  362. OCFS2_LOCK_TYPE_OPEN,
  363. 0, inode);
  364. if (can_lock) {
  365. status = ocfs2_open_lock(inode);
  366. if (status) {
  367. make_bad_inode(inode);
  368. mlog_errno(status);
  369. return status;
  370. }
  371. status = ocfs2_inode_lock(inode, NULL, 0);
  372. if (status) {
  373. make_bad_inode(inode);
  374. mlog_errno(status);
  375. return status;
  376. }
  377. }
  378. if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
  379. status = ocfs2_try_open_lock(inode, 0);
  380. if (status) {
  381. make_bad_inode(inode);
  382. return status;
  383. }
  384. }
  385. if (can_lock) {
  386. status = ocfs2_read_inode_block_full(inode, &bh,
  387. OCFS2_BH_IGNORE_CACHE);
  388. } else {
  389. status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
  390. if (!status)
  391. status = ocfs2_validate_inode_block(osb->sb, bh);
  392. }
  393. if (status < 0) {
  394. mlog_errno(status);
  395. goto bail;
  396. }
  397. status = -EINVAL;
  398. fe = (struct ocfs2_dinode *) bh->b_data;
  399. /*
  400. * This is a code bug. Right now the caller needs to
  401. * understand whether it is asking for a system file inode or
  402. * not so the proper lock names can be built.
  403. */
  404. mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
  405. !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
  406. "Inode %llu: system file state is ambigous\n",
  407. (unsigned long long)args->fi_blkno);
  408. if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  409. S_ISBLK(le16_to_cpu(fe->i_mode)))
  410. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  411. ocfs2_populate_inode(inode, fe, 0);
  412. BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
  413. status = 0;
  414. bail:
  415. if (can_lock)
  416. ocfs2_inode_unlock(inode, 0);
  417. if (status < 0)
  418. make_bad_inode(inode);
  419. if (args && bh)
  420. brelse(bh);
  421. mlog_exit(status);
  422. return status;
  423. }
  424. void ocfs2_sync_blockdev(struct super_block *sb)
  425. {
  426. sync_blockdev(sb->s_bdev);
  427. }
  428. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  429. struct inode *inode,
  430. struct buffer_head *fe_bh)
  431. {
  432. int status = 0;
  433. struct ocfs2_truncate_context *tc = NULL;
  434. struct ocfs2_dinode *fe;
  435. handle_t *handle = NULL;
  436. mlog_entry_void();
  437. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  438. /*
  439. * This check will also skip truncate of inodes with inline
  440. * data and fast symlinks.
  441. */
  442. if (fe->i_clusters) {
  443. if (ocfs2_should_order_data(inode))
  444. ocfs2_begin_ordered_truncate(inode, 0);
  445. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  446. if (IS_ERR(handle)) {
  447. status = PTR_ERR(handle);
  448. mlog_errno(status);
  449. goto out;
  450. }
  451. status = ocfs2_journal_access(handle, inode, fe_bh,
  452. OCFS2_JOURNAL_ACCESS_WRITE);
  453. if (status < 0) {
  454. mlog_errno(status);
  455. goto out;
  456. }
  457. i_size_write(inode, 0);
  458. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  459. if (status < 0) {
  460. mlog_errno(status);
  461. goto out;
  462. }
  463. ocfs2_commit_trans(osb, handle);
  464. handle = NULL;
  465. status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
  466. if (status < 0) {
  467. mlog_errno(status);
  468. goto out;
  469. }
  470. status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
  471. if (status < 0) {
  472. mlog_errno(status);
  473. goto out;
  474. }
  475. }
  476. out:
  477. if (handle)
  478. ocfs2_commit_trans(osb, handle);
  479. mlog_exit(status);
  480. return status;
  481. }
  482. static int ocfs2_remove_inode(struct inode *inode,
  483. struct buffer_head *di_bh,
  484. struct inode *orphan_dir_inode,
  485. struct buffer_head *orphan_dir_bh)
  486. {
  487. int status;
  488. struct inode *inode_alloc_inode = NULL;
  489. struct buffer_head *inode_alloc_bh = NULL;
  490. handle_t *handle;
  491. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  492. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  493. inode_alloc_inode =
  494. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  495. le16_to_cpu(di->i_suballoc_slot));
  496. if (!inode_alloc_inode) {
  497. status = -EEXIST;
  498. mlog_errno(status);
  499. goto bail;
  500. }
  501. mutex_lock(&inode_alloc_inode->i_mutex);
  502. status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  503. if (status < 0) {
  504. mutex_unlock(&inode_alloc_inode->i_mutex);
  505. mlog_errno(status);
  506. goto bail;
  507. }
  508. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS);
  509. if (IS_ERR(handle)) {
  510. status = PTR_ERR(handle);
  511. mlog_errno(status);
  512. goto bail_unlock;
  513. }
  514. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  515. orphan_dir_bh);
  516. if (status < 0) {
  517. mlog_errno(status);
  518. goto bail_commit;
  519. }
  520. /* set the inodes dtime */
  521. status = ocfs2_journal_access(handle, inode, di_bh,
  522. OCFS2_JOURNAL_ACCESS_WRITE);
  523. if (status < 0) {
  524. mlog_errno(status);
  525. goto bail_commit;
  526. }
  527. di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
  528. di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  529. status = ocfs2_journal_dirty(handle, di_bh);
  530. if (status < 0) {
  531. mlog_errno(status);
  532. goto bail_commit;
  533. }
  534. ocfs2_remove_from_cache(inode, di_bh);
  535. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  536. inode_alloc_bh, di);
  537. if (status < 0)
  538. mlog_errno(status);
  539. bail_commit:
  540. ocfs2_commit_trans(osb, handle);
  541. bail_unlock:
  542. ocfs2_inode_unlock(inode_alloc_inode, 1);
  543. mutex_unlock(&inode_alloc_inode->i_mutex);
  544. brelse(inode_alloc_bh);
  545. bail:
  546. iput(inode_alloc_inode);
  547. return status;
  548. }
  549. /*
  550. * Serialize with orphan dir recovery. If the process doing
  551. * recovery on this orphan dir does an iget() with the dir
  552. * i_mutex held, we'll deadlock here. Instead we detect this
  553. * and exit early - recovery will wipe this inode for us.
  554. */
  555. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  556. int slot)
  557. {
  558. int ret = 0;
  559. spin_lock(&osb->osb_lock);
  560. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  561. mlog(0, "Recovery is happening on orphan dir %d, will skip "
  562. "this inode\n", slot);
  563. ret = -EDEADLK;
  564. goto out;
  565. }
  566. /* This signals to the orphan recovery process that it should
  567. * wait for us to handle the wipe. */
  568. osb->osb_orphan_wipes[slot]++;
  569. out:
  570. spin_unlock(&osb->osb_lock);
  571. return ret;
  572. }
  573. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  574. int slot)
  575. {
  576. spin_lock(&osb->osb_lock);
  577. osb->osb_orphan_wipes[slot]--;
  578. spin_unlock(&osb->osb_lock);
  579. wake_up(&osb->osb_wipe_event);
  580. }
  581. static int ocfs2_wipe_inode(struct inode *inode,
  582. struct buffer_head *di_bh)
  583. {
  584. int status, orphaned_slot;
  585. struct inode *orphan_dir_inode = NULL;
  586. struct buffer_head *orphan_dir_bh = NULL;
  587. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  588. struct ocfs2_dinode *di;
  589. di = (struct ocfs2_dinode *) di_bh->b_data;
  590. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  591. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  592. if (status)
  593. return status;
  594. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  595. ORPHAN_DIR_SYSTEM_INODE,
  596. orphaned_slot);
  597. if (!orphan_dir_inode) {
  598. status = -EEXIST;
  599. mlog_errno(status);
  600. goto bail;
  601. }
  602. /* Lock the orphan dir. The lock will be held for the entire
  603. * delete_inode operation. We do this now to avoid races with
  604. * recovery completion on other nodes. */
  605. mutex_lock(&orphan_dir_inode->i_mutex);
  606. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  607. if (status < 0) {
  608. mutex_unlock(&orphan_dir_inode->i_mutex);
  609. mlog_errno(status);
  610. goto bail;
  611. }
  612. /* we do this while holding the orphan dir lock because we
  613. * don't want recovery being run from another node to try an
  614. * inode delete underneath us -- this will result in two nodes
  615. * truncating the same file! */
  616. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  617. if (status < 0) {
  618. mlog_errno(status);
  619. goto bail_unlock_dir;
  620. }
  621. /*Free extended attribute resources associated with this inode.*/
  622. status = ocfs2_xattr_remove(inode, di_bh);
  623. if (status < 0) {
  624. mlog_errno(status);
  625. goto bail_unlock_dir;
  626. }
  627. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  628. orphan_dir_bh);
  629. if (status < 0)
  630. mlog_errno(status);
  631. bail_unlock_dir:
  632. ocfs2_inode_unlock(orphan_dir_inode, 1);
  633. mutex_unlock(&orphan_dir_inode->i_mutex);
  634. brelse(orphan_dir_bh);
  635. bail:
  636. iput(orphan_dir_inode);
  637. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  638. return status;
  639. }
  640. /* There is a series of simple checks that should be done before a
  641. * trylock is even considered. Encapsulate those in this function. */
  642. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  643. {
  644. int ret = 0;
  645. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  646. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  647. /* We shouldn't be getting here for the root directory
  648. * inode.. */
  649. if (inode == osb->root_inode) {
  650. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  651. goto bail;
  652. }
  653. /* If we're coming from downconvert_thread we can't go into our own
  654. * voting [hello, deadlock city!], so unforuntately we just
  655. * have to skip deleting this guy. That's OK though because
  656. * the node who's doing the actual deleting should handle it
  657. * anyway. */
  658. if (current == osb->dc_task) {
  659. mlog(0, "Skipping delete of %lu because we're currently "
  660. "in downconvert\n", inode->i_ino);
  661. goto bail;
  662. }
  663. spin_lock(&oi->ip_lock);
  664. /* OCFS2 *never* deletes system files. This should technically
  665. * never get here as system file inodes should always have a
  666. * positive link count. */
  667. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  668. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  669. (unsigned long long)oi->ip_blkno);
  670. goto bail_unlock;
  671. }
  672. /* If we have allowd wipe of this inode for another node, it
  673. * will be marked here so we can safely skip it. Recovery will
  674. * cleanup any inodes we might inadvertantly skip here. */
  675. if (oi->ip_flags & OCFS2_INODE_SKIP_DELETE) {
  676. mlog(0, "Skipping delete of %lu because another node "
  677. "has done this for us.\n", inode->i_ino);
  678. goto bail_unlock;
  679. }
  680. ret = 1;
  681. bail_unlock:
  682. spin_unlock(&oi->ip_lock);
  683. bail:
  684. return ret;
  685. }
  686. /* Query the cluster to determine whether we should wipe an inode from
  687. * disk or not.
  688. *
  689. * Requires the inode to have the cluster lock. */
  690. static int ocfs2_query_inode_wipe(struct inode *inode,
  691. struct buffer_head *di_bh,
  692. int *wipe)
  693. {
  694. int status = 0;
  695. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  696. struct ocfs2_dinode *di;
  697. *wipe = 0;
  698. /* While we were waiting for the cluster lock in
  699. * ocfs2_delete_inode, another node might have asked to delete
  700. * the inode. Recheck our flags to catch this. */
  701. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  702. mlog(0, "Skipping delete of %llu because flags changed\n",
  703. (unsigned long long)oi->ip_blkno);
  704. goto bail;
  705. }
  706. /* Now that we have an up to date inode, we can double check
  707. * the link count. */
  708. if (inode->i_nlink) {
  709. mlog(0, "Skipping delete of %llu because nlink = %u\n",
  710. (unsigned long long)oi->ip_blkno, inode->i_nlink);
  711. goto bail;
  712. }
  713. /* Do some basic inode verification... */
  714. di = (struct ocfs2_dinode *) di_bh->b_data;
  715. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL))) {
  716. /* for lack of a better error? */
  717. status = -EEXIST;
  718. mlog(ML_ERROR,
  719. "Inode %llu (on-disk %llu) not orphaned! "
  720. "Disk flags 0x%x, inode flags 0x%x\n",
  721. (unsigned long long)oi->ip_blkno,
  722. (unsigned long long)le64_to_cpu(di->i_blkno),
  723. le32_to_cpu(di->i_flags), oi->ip_flags);
  724. goto bail;
  725. }
  726. /* has someone already deleted us?! baaad... */
  727. if (di->i_dtime) {
  728. status = -EEXIST;
  729. mlog_errno(status);
  730. goto bail;
  731. }
  732. /*
  733. * This is how ocfs2 determines whether an inode is still live
  734. * within the cluster. Every node takes a shared read lock on
  735. * the inode open lock in ocfs2_read_locked_inode(). When we
  736. * get to ->delete_inode(), each node tries to convert it's
  737. * lock to an exclusive. Trylocks are serialized by the inode
  738. * meta data lock. If the upconvert suceeds, we know the inode
  739. * is no longer live and can be deleted.
  740. *
  741. * Though we call this with the meta data lock held, the
  742. * trylock keeps us from ABBA deadlock.
  743. */
  744. status = ocfs2_try_open_lock(inode, 1);
  745. if (status == -EAGAIN) {
  746. status = 0;
  747. mlog(0, "Skipping delete of %llu because it is in use on "
  748. "other nodes\n", (unsigned long long)oi->ip_blkno);
  749. goto bail;
  750. }
  751. if (status < 0) {
  752. mlog_errno(status);
  753. goto bail;
  754. }
  755. *wipe = 1;
  756. mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
  757. (unsigned long long)oi->ip_blkno,
  758. le16_to_cpu(di->i_orphaned_slot));
  759. bail:
  760. return status;
  761. }
  762. /* Support function for ocfs2_delete_inode. Will help us keep the
  763. * inode data in a consistent state for clear_inode. Always truncates
  764. * pages, optionally sync's them first. */
  765. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  766. int sync_data)
  767. {
  768. mlog(0, "Cleanup inode %llu, sync = %d\n",
  769. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  770. if (sync_data)
  771. write_inode_now(inode, 1);
  772. truncate_inode_pages(&inode->i_data, 0);
  773. }
  774. void ocfs2_delete_inode(struct inode *inode)
  775. {
  776. int wipe, status;
  777. sigset_t blocked, oldset;
  778. struct buffer_head *di_bh = NULL;
  779. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  780. if (is_bad_inode(inode)) {
  781. mlog(0, "Skipping delete of bad inode\n");
  782. goto bail;
  783. }
  784. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  785. /* It's probably not necessary to truncate_inode_pages
  786. * here but we do it for safety anyway (it will most
  787. * likely be a no-op anyway) */
  788. ocfs2_cleanup_delete_inode(inode, 0);
  789. goto bail;
  790. }
  791. /* We want to block signals in delete_inode as the lock and
  792. * messaging paths may return us -ERESTARTSYS. Which would
  793. * cause us to exit early, resulting in inodes being orphaned
  794. * forever. */
  795. sigfillset(&blocked);
  796. status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
  797. if (status < 0) {
  798. mlog_errno(status);
  799. ocfs2_cleanup_delete_inode(inode, 1);
  800. goto bail;
  801. }
  802. /* Lock down the inode. This gives us an up to date view of
  803. * it's metadata (for verification), and allows us to
  804. * serialize delete_inode on multiple nodes.
  805. *
  806. * Even though we might be doing a truncate, we don't take the
  807. * allocation lock here as it won't be needed - nobody will
  808. * have the file open.
  809. */
  810. status = ocfs2_inode_lock(inode, &di_bh, 1);
  811. if (status < 0) {
  812. if (status != -ENOENT)
  813. mlog_errno(status);
  814. ocfs2_cleanup_delete_inode(inode, 0);
  815. goto bail_unblock;
  816. }
  817. /* Query the cluster. This will be the final decision made
  818. * before we go ahead and wipe the inode. */
  819. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  820. if (!wipe || status < 0) {
  821. /* Error and remote inode busy both mean we won't be
  822. * removing the inode, so they take almost the same
  823. * path. */
  824. if (status < 0)
  825. mlog_errno(status);
  826. /* Someone in the cluster has disallowed a wipe of
  827. * this inode, or it was never completely
  828. * orphaned. Write out the pages and exit now. */
  829. ocfs2_cleanup_delete_inode(inode, 1);
  830. goto bail_unlock_inode;
  831. }
  832. ocfs2_cleanup_delete_inode(inode, 0);
  833. status = ocfs2_wipe_inode(inode, di_bh);
  834. if (status < 0) {
  835. if (status != -EDEADLK)
  836. mlog_errno(status);
  837. goto bail_unlock_inode;
  838. }
  839. /*
  840. * Mark the inode as successfully deleted.
  841. *
  842. * This is important for ocfs2_clear_inode() as it will check
  843. * this flag and skip any checkpointing work
  844. *
  845. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  846. * the LVB for other nodes.
  847. */
  848. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  849. bail_unlock_inode:
  850. ocfs2_inode_unlock(inode, 1);
  851. brelse(di_bh);
  852. bail_unblock:
  853. status = sigprocmask(SIG_SETMASK, &oldset, NULL);
  854. if (status < 0)
  855. mlog_errno(status);
  856. bail:
  857. clear_inode(inode);
  858. mlog_exit_void();
  859. }
  860. void ocfs2_clear_inode(struct inode *inode)
  861. {
  862. int status;
  863. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  864. mlog_entry_void();
  865. if (!inode)
  866. goto bail;
  867. mlog(0, "Clearing inode: %llu, nlink = %u\n",
  868. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
  869. mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
  870. "Inode=%lu\n", inode->i_ino);
  871. /* To preven remote deletes we hold open lock before, now it
  872. * is time to unlock PR and EX open locks. */
  873. ocfs2_open_unlock(inode);
  874. /* Do these before all the other work so that we don't bounce
  875. * the downconvert thread while waiting to destroy the locks. */
  876. ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
  877. ocfs2_mark_lockres_freeing(&oi->ip_inode_lockres);
  878. ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
  879. /* We very well may get a clear_inode before all an inodes
  880. * metadata has hit disk. Of course, we can't drop any cluster
  881. * locks until the journal has finished with it. The only
  882. * exception here are successfully wiped inodes - their
  883. * metadata can now be considered to be part of the system
  884. * inodes from which it came. */
  885. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
  886. ocfs2_checkpoint_inode(inode);
  887. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  888. "Clear inode of %llu, inode has io markers\n",
  889. (unsigned long long)oi->ip_blkno);
  890. ocfs2_extent_map_trunc(inode, 0);
  891. status = ocfs2_drop_inode_locks(inode);
  892. if (status < 0)
  893. mlog_errno(status);
  894. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  895. ocfs2_lock_res_free(&oi->ip_inode_lockres);
  896. ocfs2_lock_res_free(&oi->ip_open_lockres);
  897. ocfs2_metadata_cache_purge(inode);
  898. mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
  899. "Clear inode of %llu, inode has %u cache items\n",
  900. (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
  901. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  902. "Clear inode of %llu, inode has a bad flag\n",
  903. (unsigned long long)oi->ip_blkno);
  904. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  905. "Clear inode of %llu, inode is locked\n",
  906. (unsigned long long)oi->ip_blkno);
  907. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  908. "Clear inode of %llu, io_mutex is locked\n",
  909. (unsigned long long)oi->ip_blkno);
  910. mutex_unlock(&oi->ip_io_mutex);
  911. /*
  912. * down_trylock() returns 0, down_write_trylock() returns 1
  913. * kernel 1, world 0
  914. */
  915. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  916. "Clear inode of %llu, alloc_sem is locked\n",
  917. (unsigned long long)oi->ip_blkno);
  918. up_write(&oi->ip_alloc_sem);
  919. mlog_bug_on_msg(oi->ip_open_count,
  920. "Clear inode of %llu has open count %d\n",
  921. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  922. /* Clear all other flags. */
  923. oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
  924. oi->ip_created_trans = 0;
  925. oi->ip_last_trans = 0;
  926. oi->ip_dir_start_lookup = 0;
  927. oi->ip_blkno = 0ULL;
  928. /*
  929. * ip_jinode is used to track txns against this inode. We ensure that
  930. * the journal is flushed before journal shutdown. Thus it is safe to
  931. * have inodes get cleaned up after journal shutdown.
  932. */
  933. jbd2_journal_release_jbd_inode(OCFS2_SB(inode->i_sb)->journal->j_journal,
  934. &oi->ip_jinode);
  935. bail:
  936. mlog_exit_void();
  937. }
  938. /* Called under inode_lock, with no more references on the
  939. * struct inode, so it's safe here to check the flags field
  940. * and to manipulate i_nlink without any other locks. */
  941. void ocfs2_drop_inode(struct inode *inode)
  942. {
  943. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  944. mlog_entry_void();
  945. mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
  946. (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
  947. if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
  948. generic_delete_inode(inode);
  949. else
  950. generic_drop_inode(inode);
  951. mlog_exit_void();
  952. }
  953. /*
  954. * This is called from our getattr.
  955. */
  956. int ocfs2_inode_revalidate(struct dentry *dentry)
  957. {
  958. struct inode *inode = dentry->d_inode;
  959. int status = 0;
  960. mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
  961. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
  962. if (!inode) {
  963. mlog(0, "eep, no inode!\n");
  964. status = -ENOENT;
  965. goto bail;
  966. }
  967. spin_lock(&OCFS2_I(inode)->ip_lock);
  968. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  969. spin_unlock(&OCFS2_I(inode)->ip_lock);
  970. mlog(0, "inode deleted!\n");
  971. status = -ENOENT;
  972. goto bail;
  973. }
  974. spin_unlock(&OCFS2_I(inode)->ip_lock);
  975. /* Let ocfs2_inode_lock do the work of updating our struct
  976. * inode for us. */
  977. status = ocfs2_inode_lock(inode, NULL, 0);
  978. if (status < 0) {
  979. if (status != -ENOENT)
  980. mlog_errno(status);
  981. goto bail;
  982. }
  983. ocfs2_inode_unlock(inode, 0);
  984. bail:
  985. mlog_exit(status);
  986. return status;
  987. }
  988. /*
  989. * Updates a disk inode from a
  990. * struct inode.
  991. * Only takes ip_lock.
  992. */
  993. int ocfs2_mark_inode_dirty(handle_t *handle,
  994. struct inode *inode,
  995. struct buffer_head *bh)
  996. {
  997. int status;
  998. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  999. mlog_entry("(inode %llu)\n",
  1000. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1001. status = ocfs2_journal_access(handle, inode, bh,
  1002. OCFS2_JOURNAL_ACCESS_WRITE);
  1003. if (status < 0) {
  1004. mlog_errno(status);
  1005. goto leave;
  1006. }
  1007. spin_lock(&OCFS2_I(inode)->ip_lock);
  1008. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1009. ocfs2_get_inode_flags(OCFS2_I(inode));
  1010. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1011. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1012. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1013. fe->i_size = cpu_to_le64(i_size_read(inode));
  1014. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  1015. fe->i_uid = cpu_to_le32(inode->i_uid);
  1016. fe->i_gid = cpu_to_le32(inode->i_gid);
  1017. fe->i_mode = cpu_to_le16(inode->i_mode);
  1018. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  1019. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  1020. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1021. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1022. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  1023. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1024. status = ocfs2_journal_dirty(handle, bh);
  1025. if (status < 0)
  1026. mlog_errno(status);
  1027. status = 0;
  1028. leave:
  1029. mlog_exit(status);
  1030. return status;
  1031. }
  1032. /*
  1033. *
  1034. * Updates a struct inode from a disk inode.
  1035. * does no i/o, only takes ip_lock.
  1036. */
  1037. void ocfs2_refresh_inode(struct inode *inode,
  1038. struct ocfs2_dinode *fe)
  1039. {
  1040. spin_lock(&OCFS2_I(inode)->ip_lock);
  1041. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1042. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1043. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1044. ocfs2_set_inode_flags(inode);
  1045. i_size_write(inode, le64_to_cpu(fe->i_size));
  1046. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  1047. inode->i_uid = le32_to_cpu(fe->i_uid);
  1048. inode->i_gid = le32_to_cpu(fe->i_gid);
  1049. inode->i_mode = le16_to_cpu(fe->i_mode);
  1050. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1051. inode->i_blocks = 0;
  1052. else
  1053. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1054. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  1055. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  1056. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  1057. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  1058. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  1059. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  1060. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1061. }
  1062. int ocfs2_validate_inode_block(struct super_block *sb,
  1063. struct buffer_head *bh)
  1064. {
  1065. int rc = -EINVAL;
  1066. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1067. BUG_ON(!buffer_uptodate(bh));
  1068. if (!OCFS2_IS_VALID_DINODE(di)) {
  1069. ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
  1070. (unsigned long long)bh->b_blocknr, 7,
  1071. di->i_signature);
  1072. goto bail;
  1073. }
  1074. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1075. ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
  1076. (unsigned long long)bh->b_blocknr,
  1077. (unsigned long long)le64_to_cpu(di->i_blkno));
  1078. goto bail;
  1079. }
  1080. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1081. ocfs2_error(sb,
  1082. "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
  1083. (unsigned long long)bh->b_blocknr);
  1084. goto bail;
  1085. }
  1086. if (le32_to_cpu(di->i_fs_generation) !=
  1087. OCFS2_SB(sb)->fs_generation) {
  1088. ocfs2_error(sb,
  1089. "Invalid dinode #%llu: fs_generation is %u\n",
  1090. (unsigned long long)bh->b_blocknr,
  1091. le32_to_cpu(di->i_fs_generation));
  1092. goto bail;
  1093. }
  1094. rc = 0;
  1095. bail:
  1096. return rc;
  1097. }
  1098. int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
  1099. int flags)
  1100. {
  1101. int rc;
  1102. struct buffer_head *tmp = *bh;
  1103. rc = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, &tmp,
  1104. flags);
  1105. if (rc)
  1106. goto out;
  1107. if (!(flags & OCFS2_BH_READAHEAD)) {
  1108. rc = ocfs2_validate_inode_block(inode->i_sb, tmp);
  1109. if (rc) {
  1110. brelse(tmp);
  1111. goto out;
  1112. }
  1113. }
  1114. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1115. if (!*bh)
  1116. *bh = tmp;
  1117. out:
  1118. return rc;
  1119. }
  1120. int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
  1121. {
  1122. return ocfs2_read_inode_block_full(inode, bh, 0);
  1123. }