inode.c 35 KB

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