inode.c 34 KB

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