inode.c 34 KB

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