inode.c 35 KB

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