inode.c 34 KB

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