inode.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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 "buffer_head_io.h"
  48. struct ocfs2_find_inode_args
  49. {
  50. u64 fi_blkno;
  51. unsigned long fi_ino;
  52. unsigned int fi_flags;
  53. };
  54. static int ocfs2_read_locked_inode(struct inode *inode,
  55. struct ocfs2_find_inode_args *args);
  56. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  57. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  58. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  59. struct inode *inode,
  60. struct buffer_head *fe_bh);
  61. void ocfs2_set_inode_flags(struct inode *inode)
  62. {
  63. unsigned int flags = OCFS2_I(inode)->ip_attr;
  64. inode->i_flags &= ~(S_IMMUTABLE |
  65. S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  66. if (flags & OCFS2_IMMUTABLE_FL)
  67. inode->i_flags |= S_IMMUTABLE;
  68. if (flags & OCFS2_SYNC_FL)
  69. inode->i_flags |= S_SYNC;
  70. if (flags & OCFS2_APPEND_FL)
  71. inode->i_flags |= S_APPEND;
  72. if (flags & OCFS2_NOATIME_FL)
  73. inode->i_flags |= S_NOATIME;
  74. if (flags & OCFS2_DIRSYNC_FL)
  75. inode->i_flags |= S_DIRSYNC;
  76. }
  77. /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  78. void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  79. {
  80. unsigned int flags = oi->vfs_inode.i_flags;
  81. oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  82. OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  83. if (flags & S_SYNC)
  84. oi->ip_attr |= OCFS2_SYNC_FL;
  85. if (flags & S_APPEND)
  86. oi->ip_attr |= OCFS2_APPEND_FL;
  87. if (flags & S_IMMUTABLE)
  88. oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  89. if (flags & S_NOATIME)
  90. oi->ip_attr |= OCFS2_NOATIME_FL;
  91. if (flags & S_DIRSYNC)
  92. oi->ip_attr |= OCFS2_DIRSYNC_FL;
  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. goto bail;
  130. }
  131. bail:
  132. if (!IS_ERR(inode)) {
  133. mlog(0, "returning inode with number %llu\n",
  134. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  135. mlog_exit_ptr(inode);
  136. }
  137. return inode;
  138. }
  139. /*
  140. * here's how inodes get read from disk:
  141. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  142. * found? : return the in-memory inode
  143. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  144. */
  145. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  146. {
  147. struct ocfs2_find_inode_args *args = NULL;
  148. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  149. int ret = 0;
  150. mlog_entry("(0x%p, %lu, 0x%p)\n", inode, inode->i_ino, opaque);
  151. args = opaque;
  152. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  153. if (oi->ip_blkno != args->fi_blkno)
  154. goto bail;
  155. ret = 1;
  156. bail:
  157. mlog_exit(ret);
  158. return ret;
  159. }
  160. /*
  161. * initialize the new inode, but don't do anything that would cause
  162. * us to sleep.
  163. * return 0 on success, 1 on failure
  164. */
  165. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  166. {
  167. struct ocfs2_find_inode_args *args = opaque;
  168. mlog_entry("inode = %p, opaque = %p\n", inode, opaque);
  169. inode->i_ino = args->fi_ino;
  170. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  171. mlog_exit(0);
  172. return 0;
  173. }
  174. int ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  175. int create_ino)
  176. {
  177. struct super_block *sb;
  178. struct ocfs2_super *osb;
  179. int status = -EINVAL;
  180. mlog_entry("(0x%p, size:%llu)\n", inode,
  181. (unsigned long long)le64_to_cpu(fe->i_size));
  182. sb = inode->i_sb;
  183. osb = OCFS2_SB(sb);
  184. /* this means that read_inode cannot create a superblock inode
  185. * today. change if needed. */
  186. if (!OCFS2_IS_VALID_DINODE(fe) ||
  187. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  188. mlog(0, "Invalid dinode: i_ino=%lu, i_blkno=%llu, "
  189. "signature = %.*s, flags = 0x%x\n",
  190. inode->i_ino,
  191. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  192. fe->i_signature, le32_to_cpu(fe->i_flags));
  193. goto bail;
  194. }
  195. if (le32_to_cpu(fe->i_fs_generation) != osb->fs_generation) {
  196. mlog(ML_ERROR, "file entry generation does not match "
  197. "superblock! osb->fs_generation=%x, "
  198. "fe->i_fs_generation=%x\n",
  199. osb->fs_generation, le32_to_cpu(fe->i_fs_generation));
  200. goto bail;
  201. }
  202. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  203. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  204. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  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(0, "Invalid dinode #%llu: signature = %.*s\n",
  392. (unsigned long long)args->fi_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. /*
  437. * This check will also skip truncate of inodes with inline
  438. * data and fast symlinks.
  439. */
  440. if (fe->i_clusters) {
  441. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  442. if (IS_ERR(handle)) {
  443. status = PTR_ERR(handle);
  444. mlog_errno(status);
  445. goto out;
  446. }
  447. status = ocfs2_journal_access(handle, inode, fe_bh,
  448. OCFS2_JOURNAL_ACCESS_WRITE);
  449. if (status < 0) {
  450. mlog_errno(status);
  451. goto out;
  452. }
  453. i_size_write(inode, 0);
  454. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  455. if (status < 0) {
  456. mlog_errno(status);
  457. goto out;
  458. }
  459. ocfs2_commit_trans(osb, handle);
  460. handle = NULL;
  461. status = ocfs2_prepare_truncate(osb, inode, fe_bh, &tc);
  462. if (status < 0) {
  463. mlog_errno(status);
  464. goto out;
  465. }
  466. status = ocfs2_commit_truncate(osb, inode, fe_bh, tc);
  467. if (status < 0) {
  468. mlog_errno(status);
  469. goto out;
  470. }
  471. }
  472. out:
  473. if (handle)
  474. ocfs2_commit_trans(osb, handle);
  475. mlog_exit(status);
  476. return status;
  477. }
  478. static int ocfs2_remove_inode(struct inode *inode,
  479. struct buffer_head *di_bh,
  480. struct inode *orphan_dir_inode,
  481. struct buffer_head *orphan_dir_bh)
  482. {
  483. int status;
  484. struct inode *inode_alloc_inode = NULL;
  485. struct buffer_head *inode_alloc_bh = NULL;
  486. handle_t *handle;
  487. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  488. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  489. inode_alloc_inode =
  490. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  491. le16_to_cpu(di->i_suballoc_slot));
  492. if (!inode_alloc_inode) {
  493. status = -EEXIST;
  494. mlog_errno(status);
  495. goto bail;
  496. }
  497. mutex_lock(&inode_alloc_inode->i_mutex);
  498. status = ocfs2_meta_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  499. if (status < 0) {
  500. mutex_unlock(&inode_alloc_inode->i_mutex);
  501. mlog_errno(status);
  502. goto bail;
  503. }
  504. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS);
  505. if (IS_ERR(handle)) {
  506. status = PTR_ERR(handle);
  507. mlog_errno(status);
  508. goto bail_unlock;
  509. }
  510. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  511. orphan_dir_bh);
  512. if (status < 0) {
  513. mlog_errno(status);
  514. goto bail_commit;
  515. }
  516. /* set the inodes dtime */
  517. status = ocfs2_journal_access(handle, inode, di_bh,
  518. OCFS2_JOURNAL_ACCESS_WRITE);
  519. if (status < 0) {
  520. mlog_errno(status);
  521. goto bail_commit;
  522. }
  523. di->i_dtime = cpu_to_le64(CURRENT_TIME.tv_sec);
  524. le32_and_cpu(&di->i_flags, ~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  525. status = ocfs2_journal_dirty(handle, di_bh);
  526. if (status < 0) {
  527. mlog_errno(status);
  528. goto bail_commit;
  529. }
  530. ocfs2_remove_from_cache(inode, di_bh);
  531. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  532. inode_alloc_bh, di);
  533. if (status < 0)
  534. mlog_errno(status);
  535. bail_commit:
  536. ocfs2_commit_trans(osb, handle);
  537. bail_unlock:
  538. ocfs2_meta_unlock(inode_alloc_inode, 1);
  539. mutex_unlock(&inode_alloc_inode->i_mutex);
  540. brelse(inode_alloc_bh);
  541. bail:
  542. iput(inode_alloc_inode);
  543. return status;
  544. }
  545. /*
  546. * Serialize with orphan dir recovery. If the process doing
  547. * recovery on this orphan dir does an iget() with the dir
  548. * i_mutex held, we'll deadlock here. Instead we detect this
  549. * and exit early - recovery will wipe this inode for us.
  550. */
  551. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  552. int slot)
  553. {
  554. int ret = 0;
  555. spin_lock(&osb->osb_lock);
  556. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  557. mlog(0, "Recovery is happening on orphan dir %d, will skip "
  558. "this inode\n", slot);
  559. ret = -EDEADLK;
  560. goto out;
  561. }
  562. /* This signals to the orphan recovery process that it should
  563. * wait for us to handle the wipe. */
  564. osb->osb_orphan_wipes[slot]++;
  565. out:
  566. spin_unlock(&osb->osb_lock);
  567. return ret;
  568. }
  569. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  570. int slot)
  571. {
  572. spin_lock(&osb->osb_lock);
  573. osb->osb_orphan_wipes[slot]--;
  574. spin_unlock(&osb->osb_lock);
  575. wake_up(&osb->osb_wipe_event);
  576. }
  577. static int ocfs2_wipe_inode(struct inode *inode,
  578. struct buffer_head *di_bh)
  579. {
  580. int status, orphaned_slot;
  581. struct inode *orphan_dir_inode = NULL;
  582. struct buffer_head *orphan_dir_bh = NULL;
  583. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  584. struct ocfs2_dinode *di;
  585. di = (struct ocfs2_dinode *) di_bh->b_data;
  586. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  587. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  588. if (status)
  589. return status;
  590. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  591. ORPHAN_DIR_SYSTEM_INODE,
  592. orphaned_slot);
  593. if (!orphan_dir_inode) {
  594. status = -EEXIST;
  595. mlog_errno(status);
  596. goto bail;
  597. }
  598. /* Lock the orphan dir. The lock will be held for the entire
  599. * delete_inode operation. We do this now to avoid races with
  600. * recovery completion on other nodes. */
  601. mutex_lock(&orphan_dir_inode->i_mutex);
  602. status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  603. if (status < 0) {
  604. mutex_unlock(&orphan_dir_inode->i_mutex);
  605. mlog_errno(status);
  606. goto bail;
  607. }
  608. /* we do this while holding the orphan dir lock because we
  609. * don't want recovery being run from another node to try an
  610. * inode delete underneath us -- this will result in two nodes
  611. * truncating the same file! */
  612. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  613. if (status < 0) {
  614. mlog_errno(status);
  615. goto bail_unlock_dir;
  616. }
  617. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  618. orphan_dir_bh);
  619. if (status < 0)
  620. mlog_errno(status);
  621. bail_unlock_dir:
  622. ocfs2_meta_unlock(orphan_dir_inode, 1);
  623. mutex_unlock(&orphan_dir_inode->i_mutex);
  624. brelse(orphan_dir_bh);
  625. bail:
  626. iput(orphan_dir_inode);
  627. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  628. return status;
  629. }
  630. /* There is a series of simple checks that should be done before a
  631. * trylock is even considered. Encapsulate those in this function. */
  632. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  633. {
  634. int ret = 0;
  635. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  636. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  637. /* We shouldn't be getting here for the root directory
  638. * inode.. */
  639. if (inode == osb->root_inode) {
  640. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  641. goto bail;
  642. }
  643. /* If we're coming from downconvert_thread we can't go into our own
  644. * voting [hello, deadlock city!], so unforuntately we just
  645. * have to skip deleting this guy. That's OK though because
  646. * the node who's doing the actual deleting should handle it
  647. * anyway. */
  648. if (current == osb->dc_task) {
  649. mlog(0, "Skipping delete of %lu because we're currently "
  650. "in downconvert\n", inode->i_ino);
  651. goto bail;
  652. }
  653. spin_lock(&oi->ip_lock);
  654. /* OCFS2 *never* deletes system files. This should technically
  655. * never get here as system file inodes should always have a
  656. * positive link count. */
  657. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  658. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  659. (unsigned long long)oi->ip_blkno);
  660. goto bail_unlock;
  661. }
  662. /* If we have allowd wipe of this inode for another node, it
  663. * will be marked here so we can safely skip it. Recovery will
  664. * cleanup any inodes we might inadvertantly 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)le64_to_cpu(di->i_blkno),
  713. le32_to_cpu(di->i_flags), 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. /*
  723. * This is how ocfs2 determines whether an inode is still live
  724. * within the cluster. Every node takes a shared read lock on
  725. * the inode open lock in ocfs2_read_locked_inode(). When we
  726. * get to ->delete_inode(), each node tries to convert it's
  727. * lock to an exclusive. Trylocks are serialized by the inode
  728. * meta data lock. If the upconvert suceeds, we know the inode
  729. * is no longer live and can be deleted.
  730. *
  731. * Though we call this with the meta data lock held, the
  732. * trylock keeps us from ABBA deadlock.
  733. */
  734. status = ocfs2_try_open_lock(inode, 1);
  735. if (status == -EAGAIN) {
  736. status = 0;
  737. mlog(0, "Skipping delete of %llu because it is in use on "
  738. "other nodes\n", (unsigned long long)oi->ip_blkno);
  739. goto bail;
  740. }
  741. if (status < 0) {
  742. mlog_errno(status);
  743. goto bail;
  744. }
  745. *wipe = 1;
  746. mlog(0, "Inode %llu is ok to wipe from orphan dir %u\n",
  747. (unsigned long long)oi->ip_blkno,
  748. le16_to_cpu(di->i_orphaned_slot));
  749. bail:
  750. return status;
  751. }
  752. /* Support function for ocfs2_delete_inode. Will help us keep the
  753. * inode data in a consistent state for clear_inode. Always truncates
  754. * pages, optionally sync's them first. */
  755. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  756. int sync_data)
  757. {
  758. mlog(0, "Cleanup inode %llu, sync = %d\n",
  759. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  760. if (sync_data)
  761. write_inode_now(inode, 1);
  762. truncate_inode_pages(&inode->i_data, 0);
  763. }
  764. void ocfs2_delete_inode(struct inode *inode)
  765. {
  766. int wipe, status;
  767. sigset_t blocked, oldset;
  768. struct buffer_head *di_bh = NULL;
  769. mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
  770. if (is_bad_inode(inode)) {
  771. mlog(0, "Skipping delete of bad inode\n");
  772. goto bail;
  773. }
  774. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  775. /* It's probably not necessary to truncate_inode_pages
  776. * here but we do it for safety anyway (it will most
  777. * likely be a no-op anyway) */
  778. ocfs2_cleanup_delete_inode(inode, 0);
  779. goto bail;
  780. }
  781. /* We want to block signals in delete_inode as the lock and
  782. * messaging paths may return us -ERESTARTSYS. Which would
  783. * cause us to exit early, resulting in inodes being orphaned
  784. * forever. */
  785. sigfillset(&blocked);
  786. status = sigprocmask(SIG_BLOCK, &blocked, &oldset);
  787. if (status < 0) {
  788. mlog_errno(status);
  789. ocfs2_cleanup_delete_inode(inode, 1);
  790. goto bail;
  791. }
  792. /* Lock down the inode. This gives us an up to date view of
  793. * it's metadata (for verification), and allows us to
  794. * serialize delete_inode on multiple nodes.
  795. *
  796. * Even though we might be doing a truncate, we don't take the
  797. * allocation lock here as it won't be needed - nobody will
  798. * have the file open.
  799. */
  800. status = ocfs2_meta_lock(inode, &di_bh, 1);
  801. if (status < 0) {
  802. if (status != -ENOENT)
  803. mlog_errno(status);
  804. ocfs2_cleanup_delete_inode(inode, 0);
  805. goto bail_unblock;
  806. }
  807. /* Query the cluster. This will be the final decision made
  808. * before we go ahead and wipe the inode. */
  809. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  810. if (!wipe || status < 0) {
  811. /* Error and remote inode busy both mean we won't be
  812. * removing the inode, so they take almost the same
  813. * path. */
  814. if (status < 0)
  815. mlog_errno(status);
  816. /* Someone in the cluster has disallowed a wipe of
  817. * this inode, or it was never completely
  818. * orphaned. Write out the pages and exit now. */
  819. ocfs2_cleanup_delete_inode(inode, 1);
  820. goto bail_unlock_inode;
  821. }
  822. ocfs2_cleanup_delete_inode(inode, 0);
  823. status = ocfs2_wipe_inode(inode, di_bh);
  824. if (status < 0) {
  825. if (status != -EDEADLK)
  826. mlog_errno(status);
  827. goto bail_unlock_inode;
  828. }
  829. /*
  830. * Mark the inode as successfully deleted.
  831. *
  832. * This is important for ocfs2_clear_inode() as it will check
  833. * this flag and skip any checkpointing work
  834. *
  835. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  836. * the LVB for other nodes.
  837. */
  838. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  839. bail_unlock_inode:
  840. ocfs2_meta_unlock(inode, 1);
  841. brelse(di_bh);
  842. bail_unblock:
  843. status = sigprocmask(SIG_SETMASK, &oldset, NULL);
  844. if (status < 0)
  845. mlog_errno(status);
  846. bail:
  847. clear_inode(inode);
  848. mlog_exit_void();
  849. }
  850. void ocfs2_clear_inode(struct inode *inode)
  851. {
  852. int status;
  853. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  854. mlog_entry_void();
  855. if (!inode)
  856. goto bail;
  857. mlog(0, "Clearing inode: %llu, nlink = %u\n",
  858. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_nlink);
  859. mlog_bug_on_msg(OCFS2_SB(inode->i_sb) == NULL,
  860. "Inode=%lu\n", inode->i_ino);
  861. /* To preven remote deletes we hold open lock before, now it
  862. * is time to unlock PR and EX open locks. */
  863. ocfs2_open_unlock(inode);
  864. /* Do these before all the other work so that we don't bounce
  865. * the downconvert thread while waiting to destroy the locks. */
  866. ocfs2_mark_lockres_freeing(&oi->ip_rw_lockres);
  867. ocfs2_mark_lockres_freeing(&oi->ip_meta_lockres);
  868. ocfs2_mark_lockres_freeing(&oi->ip_data_lockres);
  869. ocfs2_mark_lockres_freeing(&oi->ip_open_lockres);
  870. /* We very well may get a clear_inode before all an inodes
  871. * metadata has hit disk. Of course, we can't drop any cluster
  872. * locks until the journal has finished with it. The only
  873. * exception here are successfully wiped inodes - their
  874. * metadata can now be considered to be part of the system
  875. * inodes from which it came. */
  876. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED))
  877. ocfs2_checkpoint_inode(inode);
  878. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  879. "Clear inode of %llu, inode has io markers\n",
  880. (unsigned long long)oi->ip_blkno);
  881. ocfs2_extent_map_trunc(inode, 0);
  882. status = ocfs2_drop_inode_locks(inode);
  883. if (status < 0)
  884. mlog_errno(status);
  885. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  886. ocfs2_lock_res_free(&oi->ip_meta_lockres);
  887. ocfs2_lock_res_free(&oi->ip_data_lockres);
  888. ocfs2_lock_res_free(&oi->ip_open_lockres);
  889. ocfs2_metadata_cache_purge(inode);
  890. mlog_bug_on_msg(oi->ip_metadata_cache.ci_num_cached,
  891. "Clear inode of %llu, inode has %u cache items\n",
  892. (unsigned long long)oi->ip_blkno, oi->ip_metadata_cache.ci_num_cached);
  893. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  894. "Clear inode of %llu, inode has a bad flag\n",
  895. (unsigned long long)oi->ip_blkno);
  896. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  897. "Clear inode of %llu, inode is locked\n",
  898. (unsigned long long)oi->ip_blkno);
  899. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  900. "Clear inode of %llu, io_mutex is locked\n",
  901. (unsigned long long)oi->ip_blkno);
  902. mutex_unlock(&oi->ip_io_mutex);
  903. /*
  904. * down_trylock() returns 0, down_write_trylock() returns 1
  905. * kernel 1, world 0
  906. */
  907. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  908. "Clear inode of %llu, alloc_sem is locked\n",
  909. (unsigned long long)oi->ip_blkno);
  910. up_write(&oi->ip_alloc_sem);
  911. mlog_bug_on_msg(oi->ip_open_count,
  912. "Clear inode of %llu has open count %d\n",
  913. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  914. /* Clear all other flags. */
  915. oi->ip_flags = OCFS2_INODE_CACHE_INLINE;
  916. oi->ip_created_trans = 0;
  917. oi->ip_last_trans = 0;
  918. oi->ip_dir_start_lookup = 0;
  919. oi->ip_blkno = 0ULL;
  920. bail:
  921. mlog_exit_void();
  922. }
  923. /* Called under inode_lock, with no more references on the
  924. * struct inode, so it's safe here to check the flags field
  925. * and to manipulate i_nlink without any other locks. */
  926. void ocfs2_drop_inode(struct inode *inode)
  927. {
  928. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  929. mlog_entry_void();
  930. mlog(0, "Drop inode %llu, nlink = %u, ip_flags = 0x%x\n",
  931. (unsigned long long)oi->ip_blkno, inode->i_nlink, oi->ip_flags);
  932. if (oi->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)
  933. generic_delete_inode(inode);
  934. else
  935. generic_drop_inode(inode);
  936. mlog_exit_void();
  937. }
  938. /*
  939. * TODO: this should probably be merged into ocfs2_get_block
  940. *
  941. * However, you now need to pay attention to the cont_prepare_write()
  942. * stuff in ocfs2_get_block (that is, ocfs2_get_block pretty much
  943. * expects never to extend).
  944. */
  945. struct buffer_head *ocfs2_bread(struct inode *inode,
  946. int block, int *err, int reada)
  947. {
  948. struct buffer_head *bh = NULL;
  949. int tmperr;
  950. u64 p_blkno;
  951. int readflags = OCFS2_BH_CACHED;
  952. if (reada)
  953. readflags |= OCFS2_BH_READAHEAD;
  954. if (((u64)block << inode->i_sb->s_blocksize_bits) >=
  955. i_size_read(inode)) {
  956. BUG_ON(!reada);
  957. return NULL;
  958. }
  959. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  960. tmperr = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
  961. NULL);
  962. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  963. if (tmperr < 0) {
  964. mlog_errno(tmperr);
  965. goto fail;
  966. }
  967. tmperr = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno, &bh,
  968. readflags, inode);
  969. if (tmperr < 0)
  970. goto fail;
  971. tmperr = 0;
  972. *err = 0;
  973. return bh;
  974. fail:
  975. if (bh) {
  976. brelse(bh);
  977. bh = NULL;
  978. }
  979. *err = -EIO;
  980. return NULL;
  981. }
  982. /*
  983. * This is called from our getattr.
  984. */
  985. int ocfs2_inode_revalidate(struct dentry *dentry)
  986. {
  987. struct inode *inode = dentry->d_inode;
  988. int status = 0;
  989. mlog_entry("(inode = 0x%p, ino = %llu)\n", inode,
  990. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL);
  991. if (!inode) {
  992. mlog(0, "eep, no inode!\n");
  993. status = -ENOENT;
  994. goto bail;
  995. }
  996. spin_lock(&OCFS2_I(inode)->ip_lock);
  997. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  998. spin_unlock(&OCFS2_I(inode)->ip_lock);
  999. mlog(0, "inode deleted!\n");
  1000. status = -ENOENT;
  1001. goto bail;
  1002. }
  1003. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1004. /* Let ocfs2_meta_lock do the work of updating our struct
  1005. * inode for us. */
  1006. status = ocfs2_meta_lock(inode, NULL, 0);
  1007. if (status < 0) {
  1008. if (status != -ENOENT)
  1009. mlog_errno(status);
  1010. goto bail;
  1011. }
  1012. ocfs2_meta_unlock(inode, 0);
  1013. bail:
  1014. mlog_exit(status);
  1015. return status;
  1016. }
  1017. /*
  1018. * Updates a disk inode from a
  1019. * struct inode.
  1020. * Only takes ip_lock.
  1021. */
  1022. int ocfs2_mark_inode_dirty(handle_t *handle,
  1023. struct inode *inode,
  1024. struct buffer_head *bh)
  1025. {
  1026. int status;
  1027. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  1028. mlog_entry("(inode %llu)\n",
  1029. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1030. status = ocfs2_journal_access(handle, inode, bh,
  1031. OCFS2_JOURNAL_ACCESS_WRITE);
  1032. if (status < 0) {
  1033. mlog_errno(status);
  1034. goto leave;
  1035. }
  1036. spin_lock(&OCFS2_I(inode)->ip_lock);
  1037. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1038. ocfs2_get_inode_flags(OCFS2_I(inode));
  1039. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1040. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1041. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1042. fe->i_size = cpu_to_le64(i_size_read(inode));
  1043. fe->i_links_count = cpu_to_le16(inode->i_nlink);
  1044. fe->i_uid = cpu_to_le32(inode->i_uid);
  1045. fe->i_gid = cpu_to_le32(inode->i_gid);
  1046. fe->i_mode = cpu_to_le16(inode->i_mode);
  1047. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  1048. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  1049. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1050. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1051. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  1052. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1053. status = ocfs2_journal_dirty(handle, bh);
  1054. if (status < 0)
  1055. mlog_errno(status);
  1056. status = 0;
  1057. leave:
  1058. mlog_exit(status);
  1059. return status;
  1060. }
  1061. /*
  1062. *
  1063. * Updates a struct inode from a disk inode.
  1064. * does no i/o, only takes ip_lock.
  1065. */
  1066. void ocfs2_refresh_inode(struct inode *inode,
  1067. struct ocfs2_dinode *fe)
  1068. {
  1069. spin_lock(&OCFS2_I(inode)->ip_lock);
  1070. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1071. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1072. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1073. ocfs2_set_inode_flags(inode);
  1074. i_size_write(inode, le64_to_cpu(fe->i_size));
  1075. inode->i_nlink = le16_to_cpu(fe->i_links_count);
  1076. inode->i_uid = le32_to_cpu(fe->i_uid);
  1077. inode->i_gid = le32_to_cpu(fe->i_gid);
  1078. inode->i_mode = le16_to_cpu(fe->i_mode);
  1079. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1080. inode->i_blocks = 0;
  1081. else
  1082. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1083. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  1084. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  1085. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  1086. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  1087. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  1088. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  1089. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1090. }