inode.c 36 KB

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