inode.c 37 KB

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