inode.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * inode.c
  6. *
  7. * vfs' aops, fops, dops and iops
  8. *
  9. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/types.h>
  13. #include <linux/highmem.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/quotaops.h>
  16. #include <linux/iversion.h>
  17. #include <asm/byteorder.h>
  18. #include <cluster/masklog.h>
  19. #include "ocfs2.h"
  20. #include "alloc.h"
  21. #include "dir.h"
  22. #include "blockcheck.h"
  23. #include "dlmglue.h"
  24. #include "extent_map.h"
  25. #include "file.h"
  26. #include "heartbeat.h"
  27. #include "inode.h"
  28. #include "journal.h"
  29. #include "namei.h"
  30. #include "suballoc.h"
  31. #include "super.h"
  32. #include "symlink.h"
  33. #include "sysfile.h"
  34. #include "uptodate.h"
  35. #include "xattr.h"
  36. #include "refcounttree.h"
  37. #include "ocfs2_trace.h"
  38. #include "filecheck.h"
  39. #include "buffer_head_io.h"
  40. struct ocfs2_find_inode_args
  41. {
  42. u64 fi_blkno;
  43. unsigned long fi_ino;
  44. unsigned int fi_flags;
  45. unsigned int fi_sysfile_type;
  46. };
  47. static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES];
  48. static int ocfs2_read_locked_inode(struct inode *inode,
  49. struct ocfs2_find_inode_args *args);
  50. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque);
  51. static int ocfs2_find_actor(struct inode *inode, void *opaque);
  52. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  53. struct inode *inode,
  54. struct buffer_head *fe_bh);
  55. static int ocfs2_filecheck_read_inode_block_full(struct inode *inode,
  56. struct buffer_head **bh,
  57. int flags, int type);
  58. static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
  59. struct buffer_head *bh);
  60. static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
  61. struct buffer_head *bh);
  62. void ocfs2_set_inode_flags(struct inode *inode)
  63. {
  64. unsigned int flags = OCFS2_I(inode)->ip_attr;
  65. inode->i_flags &= ~(S_IMMUTABLE |
  66. S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC);
  67. if (flags & OCFS2_IMMUTABLE_FL)
  68. inode->i_flags |= S_IMMUTABLE;
  69. if (flags & OCFS2_SYNC_FL)
  70. inode->i_flags |= S_SYNC;
  71. if (flags & OCFS2_APPEND_FL)
  72. inode->i_flags |= S_APPEND;
  73. if (flags & OCFS2_NOATIME_FL)
  74. inode->i_flags |= S_NOATIME;
  75. if (flags & OCFS2_DIRSYNC_FL)
  76. inode->i_flags |= S_DIRSYNC;
  77. }
  78. /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */
  79. void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi)
  80. {
  81. unsigned int flags = oi->vfs_inode.i_flags;
  82. oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL|
  83. OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL);
  84. if (flags & S_SYNC)
  85. oi->ip_attr |= OCFS2_SYNC_FL;
  86. if (flags & S_APPEND)
  87. oi->ip_attr |= OCFS2_APPEND_FL;
  88. if (flags & S_IMMUTABLE)
  89. oi->ip_attr |= OCFS2_IMMUTABLE_FL;
  90. if (flags & S_NOATIME)
  91. oi->ip_attr |= OCFS2_NOATIME_FL;
  92. if (flags & S_DIRSYNC)
  93. oi->ip_attr |= OCFS2_DIRSYNC_FL;
  94. }
  95. struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno)
  96. {
  97. struct ocfs2_find_inode_args args;
  98. args.fi_blkno = blkno;
  99. args.fi_flags = 0;
  100. args.fi_ino = ino_from_blkno(sb, blkno);
  101. args.fi_sysfile_type = 0;
  102. return ilookup5(sb, blkno, ocfs2_find_actor, &args);
  103. }
  104. struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags,
  105. int sysfile_type)
  106. {
  107. int rc = -ESTALE;
  108. struct inode *inode = NULL;
  109. struct super_block *sb = osb->sb;
  110. struct ocfs2_find_inode_args args;
  111. journal_t *journal = OCFS2_SB(sb)->journal->j_journal;
  112. trace_ocfs2_iget_begin((unsigned long long)blkno, flags,
  113. sysfile_type);
  114. /* Ok. By now we've either got the offsets passed to us by the
  115. * caller, or we just pulled them off the bh. Lets do some
  116. * sanity checks to make sure they're OK. */
  117. if (blkno == 0) {
  118. inode = ERR_PTR(-EINVAL);
  119. mlog_errno(PTR_ERR(inode));
  120. goto bail;
  121. }
  122. args.fi_blkno = blkno;
  123. args.fi_flags = flags;
  124. args.fi_ino = ino_from_blkno(sb, blkno);
  125. args.fi_sysfile_type = sysfile_type;
  126. inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor,
  127. ocfs2_init_locked_inode, &args);
  128. /* inode was *not* in the inode cache. 2.6.x requires
  129. * us to do our own read_inode call and unlock it
  130. * afterwards. */
  131. if (inode == NULL) {
  132. inode = ERR_PTR(-ENOMEM);
  133. mlog_errno(PTR_ERR(inode));
  134. goto bail;
  135. }
  136. trace_ocfs2_iget5_locked(inode->i_state);
  137. if (inode->i_state & I_NEW) {
  138. rc = ocfs2_read_locked_inode(inode, &args);
  139. unlock_new_inode(inode);
  140. }
  141. if (is_bad_inode(inode)) {
  142. iput(inode);
  143. inode = ERR_PTR(rc);
  144. goto bail;
  145. }
  146. /*
  147. * Set transaction id's of transactions that have to be committed
  148. * to finish f[data]sync. We set them to currently running transaction
  149. * as we cannot be sure that the inode or some of its metadata isn't
  150. * part of the transaction - the inode could have been reclaimed and
  151. * now it is reread from disk.
  152. */
  153. if (journal) {
  154. transaction_t *transaction;
  155. tid_t tid;
  156. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  157. read_lock(&journal->j_state_lock);
  158. if (journal->j_running_transaction)
  159. transaction = journal->j_running_transaction;
  160. else
  161. transaction = journal->j_committing_transaction;
  162. if (transaction)
  163. tid = transaction->t_tid;
  164. else
  165. tid = journal->j_commit_sequence;
  166. read_unlock(&journal->j_state_lock);
  167. oi->i_sync_tid = tid;
  168. oi->i_datasync_tid = tid;
  169. }
  170. bail:
  171. if (!IS_ERR(inode)) {
  172. trace_ocfs2_iget_end(inode,
  173. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  174. }
  175. return inode;
  176. }
  177. /*
  178. * here's how inodes get read from disk:
  179. * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR
  180. * found? : return the in-memory inode
  181. * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE
  182. */
  183. static int ocfs2_find_actor(struct inode *inode, void *opaque)
  184. {
  185. struct ocfs2_find_inode_args *args = NULL;
  186. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  187. int ret = 0;
  188. args = opaque;
  189. mlog_bug_on_msg(!inode, "No inode in find actor!\n");
  190. trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno);
  191. if (oi->ip_blkno != args->fi_blkno)
  192. goto bail;
  193. ret = 1;
  194. bail:
  195. return ret;
  196. }
  197. /*
  198. * initialize the new inode, but don't do anything that would cause
  199. * us to sleep.
  200. * return 0 on success, 1 on failure
  201. */
  202. static int ocfs2_init_locked_inode(struct inode *inode, void *opaque)
  203. {
  204. struct ocfs2_find_inode_args *args = opaque;
  205. static struct lock_class_key ocfs2_quota_ip_alloc_sem_key,
  206. ocfs2_file_ip_alloc_sem_key;
  207. inode->i_ino = args->fi_ino;
  208. OCFS2_I(inode)->ip_blkno = args->fi_blkno;
  209. if (args->fi_sysfile_type != 0)
  210. lockdep_set_class(&inode->i_rwsem,
  211. &ocfs2_sysfile_lock_key[args->fi_sysfile_type]);
  212. if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE ||
  213. args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE ||
  214. args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
  215. args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE)
  216. lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
  217. &ocfs2_quota_ip_alloc_sem_key);
  218. else
  219. lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem,
  220. &ocfs2_file_ip_alloc_sem_key);
  221. return 0;
  222. }
  223. void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe,
  224. int create_ino)
  225. {
  226. struct super_block *sb;
  227. struct ocfs2_super *osb;
  228. int use_plocks = 1;
  229. sb = inode->i_sb;
  230. osb = OCFS2_SB(sb);
  231. if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  232. ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks())
  233. use_plocks = 0;
  234. /*
  235. * These have all been checked by ocfs2_read_inode_block() or set
  236. * by ocfs2_mknod_locked(), so a failure is a code bug.
  237. */
  238. BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode
  239. cannot create a superblock
  240. inode today. change if
  241. that is needed. */
  242. BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)));
  243. BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation);
  244. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  245. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  246. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  247. inode_set_iversion(inode, 1);
  248. inode->i_generation = le32_to_cpu(fe->i_generation);
  249. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  250. inode->i_mode = le16_to_cpu(fe->i_mode);
  251. i_uid_write(inode, le32_to_cpu(fe->i_uid));
  252. i_gid_write(inode, le32_to_cpu(fe->i_gid));
  253. /* Fast symlinks will have i_size but no allocated clusters. */
  254. if (S_ISLNK(inode->i_mode) && !fe->i_clusters) {
  255. inode->i_blocks = 0;
  256. inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops;
  257. } else {
  258. inode->i_blocks = ocfs2_inode_sector_count(inode);
  259. inode->i_mapping->a_ops = &ocfs2_aops;
  260. }
  261. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  262. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  263. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  264. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  265. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  266. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  267. if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno))
  268. mlog(ML_ERROR,
  269. "ip_blkno %llu != i_blkno %llu!\n",
  270. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  271. (unsigned long long)le64_to_cpu(fe->i_blkno));
  272. set_nlink(inode, ocfs2_read_links_count(fe));
  273. trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno,
  274. le32_to_cpu(fe->i_flags));
  275. if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) {
  276. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE;
  277. inode->i_flags |= S_NOQUOTA;
  278. }
  279. if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) {
  280. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  281. } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) {
  282. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP;
  283. } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) {
  284. inode->i_flags |= S_NOQUOTA;
  285. } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) {
  286. /* we can't actually hit this as read_inode can't
  287. * handle superblocks today ;-) */
  288. BUG();
  289. }
  290. switch (inode->i_mode & S_IFMT) {
  291. case S_IFREG:
  292. if (use_plocks)
  293. inode->i_fop = &ocfs2_fops;
  294. else
  295. inode->i_fop = &ocfs2_fops_no_plocks;
  296. inode->i_op = &ocfs2_file_iops;
  297. i_size_write(inode, le64_to_cpu(fe->i_size));
  298. break;
  299. case S_IFDIR:
  300. inode->i_op = &ocfs2_dir_iops;
  301. if (use_plocks)
  302. inode->i_fop = &ocfs2_dops;
  303. else
  304. inode->i_fop = &ocfs2_dops_no_plocks;
  305. i_size_write(inode, le64_to_cpu(fe->i_size));
  306. OCFS2_I(inode)->ip_dir_lock_gen = 1;
  307. break;
  308. case S_IFLNK:
  309. inode->i_op = &ocfs2_symlink_inode_operations;
  310. inode_nohighmem(inode);
  311. i_size_write(inode, le64_to_cpu(fe->i_size));
  312. break;
  313. default:
  314. inode->i_op = &ocfs2_special_file_iops;
  315. init_special_inode(inode, inode->i_mode,
  316. inode->i_rdev);
  317. break;
  318. }
  319. if (create_ino) {
  320. inode->i_ino = ino_from_blkno(inode->i_sb,
  321. le64_to_cpu(fe->i_blkno));
  322. /*
  323. * If we ever want to create system files from kernel,
  324. * the generation argument to
  325. * ocfs2_inode_lock_res_init() will have to change.
  326. */
  327. BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL);
  328. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  329. OCFS2_LOCK_TYPE_META, 0, inode);
  330. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  331. OCFS2_LOCK_TYPE_OPEN, 0, inode);
  332. }
  333. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres,
  334. OCFS2_LOCK_TYPE_RW, inode->i_generation,
  335. inode);
  336. ocfs2_set_inode_flags(inode);
  337. OCFS2_I(inode)->ip_last_used_slot = 0;
  338. OCFS2_I(inode)->ip_last_used_group = 0;
  339. if (S_ISDIR(inode->i_mode))
  340. ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv,
  341. OCFS2_RESV_FLAG_DIR);
  342. }
  343. static int ocfs2_read_locked_inode(struct inode *inode,
  344. struct ocfs2_find_inode_args *args)
  345. {
  346. struct super_block *sb;
  347. struct ocfs2_super *osb;
  348. struct ocfs2_dinode *fe;
  349. struct buffer_head *bh = NULL;
  350. int status, can_lock, lock_level = 0;
  351. u32 generation = 0;
  352. status = -EINVAL;
  353. sb = inode->i_sb;
  354. osb = OCFS2_SB(sb);
  355. /*
  356. * To improve performance of cold-cache inode stats, we take
  357. * the cluster lock here if possible.
  358. *
  359. * Generally, OCFS2 never trusts the contents of an inode
  360. * unless it's holding a cluster lock, so taking it here isn't
  361. * a correctness issue as much as it is a performance
  362. * improvement.
  363. *
  364. * There are three times when taking the lock is not a good idea:
  365. *
  366. * 1) During startup, before we have initialized the DLM.
  367. *
  368. * 2) If we are reading certain system files which never get
  369. * cluster locks (local alloc, truncate log).
  370. *
  371. * 3) If the process doing the iget() is responsible for
  372. * orphan dir recovery. We're holding the orphan dir lock and
  373. * can get into a deadlock with another process on another
  374. * node in ->delete_inode().
  375. *
  376. * #1 and #2 can be simply solved by never taking the lock
  377. * here for system files (which are the only type we read
  378. * during mount). It's a heavier approach, but our main
  379. * concern is user-accessible files anyway.
  380. *
  381. * #3 works itself out because we'll eventually take the
  382. * cluster lock before trusting anything anyway.
  383. */
  384. can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  385. && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY)
  386. && !ocfs2_mount_local(osb);
  387. trace_ocfs2_read_locked_inode(
  388. (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock);
  389. /*
  390. * To maintain backwards compatibility with older versions of
  391. * ocfs2-tools, we still store the generation value for system
  392. * files. The only ones that actually matter to userspace are
  393. * the journals, but it's easier and inexpensive to just flag
  394. * all system files similarly.
  395. */
  396. if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE)
  397. generation = osb->fs_generation;
  398. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres,
  399. OCFS2_LOCK_TYPE_META,
  400. generation, inode);
  401. ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres,
  402. OCFS2_LOCK_TYPE_OPEN,
  403. 0, inode);
  404. if (can_lock) {
  405. status = ocfs2_open_lock(inode);
  406. if (status) {
  407. make_bad_inode(inode);
  408. mlog_errno(status);
  409. return status;
  410. }
  411. status = ocfs2_inode_lock(inode, NULL, lock_level);
  412. if (status) {
  413. make_bad_inode(inode);
  414. mlog_errno(status);
  415. return status;
  416. }
  417. }
  418. if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) {
  419. status = ocfs2_try_open_lock(inode, 0);
  420. if (status) {
  421. make_bad_inode(inode);
  422. return status;
  423. }
  424. }
  425. if (can_lock) {
  426. if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
  427. status = ocfs2_filecheck_read_inode_block_full(inode,
  428. &bh, OCFS2_BH_IGNORE_CACHE, 0);
  429. else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
  430. status = ocfs2_filecheck_read_inode_block_full(inode,
  431. &bh, OCFS2_BH_IGNORE_CACHE, 1);
  432. else
  433. status = ocfs2_read_inode_block_full(inode,
  434. &bh, OCFS2_BH_IGNORE_CACHE);
  435. } else {
  436. status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh);
  437. /*
  438. * If buffer is in jbd, then its checksum may not have been
  439. * computed as yet.
  440. */
  441. if (!status && !buffer_jbd(bh)) {
  442. if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK)
  443. status = ocfs2_filecheck_validate_inode_block(
  444. osb->sb, bh);
  445. else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX)
  446. status = ocfs2_filecheck_repair_inode_block(
  447. osb->sb, bh);
  448. else
  449. status = ocfs2_validate_inode_block(
  450. osb->sb, bh);
  451. }
  452. }
  453. if (status < 0) {
  454. mlog_errno(status);
  455. goto bail;
  456. }
  457. status = -EINVAL;
  458. fe = (struct ocfs2_dinode *) bh->b_data;
  459. /*
  460. * This is a code bug. Right now the caller needs to
  461. * understand whether it is asking for a system file inode or
  462. * not so the proper lock names can be built.
  463. */
  464. mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) !=
  465. !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE),
  466. "Inode %llu: system file state is ambiguous\n",
  467. (unsigned long long)args->fi_blkno);
  468. if (S_ISCHR(le16_to_cpu(fe->i_mode)) ||
  469. S_ISBLK(le16_to_cpu(fe->i_mode)))
  470. inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev));
  471. ocfs2_populate_inode(inode, fe, 0);
  472. BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno));
  473. if (buffer_dirty(bh) && !buffer_jbd(bh)) {
  474. if (can_lock) {
  475. ocfs2_inode_unlock(inode, lock_level);
  476. lock_level = 1;
  477. ocfs2_inode_lock(inode, NULL, lock_level);
  478. }
  479. status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
  480. if (status < 0) {
  481. mlog_errno(status);
  482. goto bail;
  483. }
  484. }
  485. status = 0;
  486. bail:
  487. if (can_lock)
  488. ocfs2_inode_unlock(inode, lock_level);
  489. if (status < 0)
  490. make_bad_inode(inode);
  491. brelse(bh);
  492. return status;
  493. }
  494. void ocfs2_sync_blockdev(struct super_block *sb)
  495. {
  496. sync_blockdev(sb->s_bdev);
  497. }
  498. static int ocfs2_truncate_for_delete(struct ocfs2_super *osb,
  499. struct inode *inode,
  500. struct buffer_head *fe_bh)
  501. {
  502. int status = 0;
  503. struct ocfs2_dinode *fe;
  504. handle_t *handle = NULL;
  505. fe = (struct ocfs2_dinode *) fe_bh->b_data;
  506. /*
  507. * This check will also skip truncate of inodes with inline
  508. * data and fast symlinks.
  509. */
  510. if (fe->i_clusters) {
  511. if (ocfs2_should_order_data(inode))
  512. ocfs2_begin_ordered_truncate(inode, 0);
  513. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  514. if (IS_ERR(handle)) {
  515. status = PTR_ERR(handle);
  516. handle = NULL;
  517. mlog_errno(status);
  518. goto out;
  519. }
  520. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
  521. fe_bh,
  522. OCFS2_JOURNAL_ACCESS_WRITE);
  523. if (status < 0) {
  524. mlog_errno(status);
  525. goto out;
  526. }
  527. i_size_write(inode, 0);
  528. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  529. if (status < 0) {
  530. mlog_errno(status);
  531. goto out;
  532. }
  533. ocfs2_commit_trans(osb, handle);
  534. handle = NULL;
  535. status = ocfs2_commit_truncate(osb, inode, fe_bh);
  536. if (status < 0)
  537. mlog_errno(status);
  538. }
  539. out:
  540. if (handle)
  541. ocfs2_commit_trans(osb, handle);
  542. return status;
  543. }
  544. static int ocfs2_remove_inode(struct inode *inode,
  545. struct buffer_head *di_bh,
  546. struct inode *orphan_dir_inode,
  547. struct buffer_head *orphan_dir_bh)
  548. {
  549. int status;
  550. struct inode *inode_alloc_inode = NULL;
  551. struct buffer_head *inode_alloc_bh = NULL;
  552. handle_t *handle;
  553. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  554. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  555. inode_alloc_inode =
  556. ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  557. le16_to_cpu(di->i_suballoc_slot));
  558. if (!inode_alloc_inode) {
  559. status = -ENOENT;
  560. mlog_errno(status);
  561. goto bail;
  562. }
  563. inode_lock(inode_alloc_inode);
  564. status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1);
  565. if (status < 0) {
  566. inode_unlock(inode_alloc_inode);
  567. mlog_errno(status);
  568. goto bail;
  569. }
  570. handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS +
  571. ocfs2_quota_trans_credits(inode->i_sb));
  572. if (IS_ERR(handle)) {
  573. status = PTR_ERR(handle);
  574. mlog_errno(status);
  575. goto bail_unlock;
  576. }
  577. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  578. status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode,
  579. orphan_dir_bh, false);
  580. if (status < 0) {
  581. mlog_errno(status);
  582. goto bail_commit;
  583. }
  584. }
  585. /* set the inodes dtime */
  586. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  587. OCFS2_JOURNAL_ACCESS_WRITE);
  588. if (status < 0) {
  589. mlog_errno(status);
  590. goto bail_commit;
  591. }
  592. di->i_dtime = cpu_to_le64(ktime_get_real_seconds());
  593. di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL));
  594. ocfs2_journal_dirty(handle, di_bh);
  595. ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh);
  596. dquot_free_inode(inode);
  597. status = ocfs2_free_dinode(handle, inode_alloc_inode,
  598. inode_alloc_bh, di);
  599. if (status < 0)
  600. mlog_errno(status);
  601. bail_commit:
  602. ocfs2_commit_trans(osb, handle);
  603. bail_unlock:
  604. ocfs2_inode_unlock(inode_alloc_inode, 1);
  605. inode_unlock(inode_alloc_inode);
  606. brelse(inode_alloc_bh);
  607. bail:
  608. iput(inode_alloc_inode);
  609. return status;
  610. }
  611. /*
  612. * Serialize with orphan dir recovery. If the process doing
  613. * recovery on this orphan dir does an iget() with the dir
  614. * i_mutex held, we'll deadlock here. Instead we detect this
  615. * and exit early - recovery will wipe this inode for us.
  616. */
  617. static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb,
  618. int slot)
  619. {
  620. int ret = 0;
  621. spin_lock(&osb->osb_lock);
  622. if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) {
  623. ret = -EDEADLK;
  624. goto out;
  625. }
  626. /* This signals to the orphan recovery process that it should
  627. * wait for us to handle the wipe. */
  628. osb->osb_orphan_wipes[slot]++;
  629. out:
  630. spin_unlock(&osb->osb_lock);
  631. trace_ocfs2_check_orphan_recovery_state(slot, ret);
  632. return ret;
  633. }
  634. static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb,
  635. int slot)
  636. {
  637. spin_lock(&osb->osb_lock);
  638. osb->osb_orphan_wipes[slot]--;
  639. spin_unlock(&osb->osb_lock);
  640. wake_up(&osb->osb_wipe_event);
  641. }
  642. static int ocfs2_wipe_inode(struct inode *inode,
  643. struct buffer_head *di_bh)
  644. {
  645. int status, orphaned_slot = -1;
  646. struct inode *orphan_dir_inode = NULL;
  647. struct buffer_head *orphan_dir_bh = NULL;
  648. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  649. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  650. if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  651. orphaned_slot = le16_to_cpu(di->i_orphaned_slot);
  652. status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot);
  653. if (status)
  654. return status;
  655. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  656. ORPHAN_DIR_SYSTEM_INODE,
  657. orphaned_slot);
  658. if (!orphan_dir_inode) {
  659. status = -ENOENT;
  660. mlog_errno(status);
  661. goto bail;
  662. }
  663. /* Lock the orphan dir. The lock will be held for the entire
  664. * delete_inode operation. We do this now to avoid races with
  665. * recovery completion on other nodes. */
  666. inode_lock(orphan_dir_inode);
  667. status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1);
  668. if (status < 0) {
  669. inode_unlock(orphan_dir_inode);
  670. mlog_errno(status);
  671. goto bail;
  672. }
  673. }
  674. /* we do this while holding the orphan dir lock because we
  675. * don't want recovery being run from another node to try an
  676. * inode delete underneath us -- this will result in two nodes
  677. * truncating the same file! */
  678. status = ocfs2_truncate_for_delete(osb, inode, di_bh);
  679. if (status < 0) {
  680. mlog_errno(status);
  681. goto bail_unlock_dir;
  682. }
  683. /* Remove any dir index tree */
  684. if (S_ISDIR(inode->i_mode)) {
  685. status = ocfs2_dx_dir_truncate(inode, di_bh);
  686. if (status) {
  687. mlog_errno(status);
  688. goto bail_unlock_dir;
  689. }
  690. }
  691. /*Free extended attribute resources associated with this inode.*/
  692. status = ocfs2_xattr_remove(inode, di_bh);
  693. if (status < 0) {
  694. mlog_errno(status);
  695. goto bail_unlock_dir;
  696. }
  697. status = ocfs2_remove_refcount_tree(inode, di_bh);
  698. if (status < 0) {
  699. mlog_errno(status);
  700. goto bail_unlock_dir;
  701. }
  702. status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode,
  703. orphan_dir_bh);
  704. if (status < 0)
  705. mlog_errno(status);
  706. bail_unlock_dir:
  707. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)
  708. return status;
  709. ocfs2_inode_unlock(orphan_dir_inode, 1);
  710. inode_unlock(orphan_dir_inode);
  711. brelse(orphan_dir_bh);
  712. bail:
  713. iput(orphan_dir_inode);
  714. ocfs2_signal_wipe_completion(osb, orphaned_slot);
  715. return status;
  716. }
  717. /* There is a series of simple checks that should be done before a
  718. * trylock is even considered. Encapsulate those in this function. */
  719. static int ocfs2_inode_is_valid_to_delete(struct inode *inode)
  720. {
  721. int ret = 0;
  722. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  723. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  724. trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task,
  725. (unsigned long long)oi->ip_blkno,
  726. oi->ip_flags);
  727. /* We shouldn't be getting here for the root directory
  728. * inode.. */
  729. if (inode == osb->root_inode) {
  730. mlog(ML_ERROR, "Skipping delete of root inode.\n");
  731. goto bail;
  732. }
  733. /*
  734. * If we're coming from downconvert_thread we can't go into our own
  735. * voting [hello, deadlock city!] so we cannot delete the inode. But
  736. * since we dropped last inode ref when downconverting dentry lock,
  737. * we cannot have the file open and thus the node doing unlink will
  738. * take care of deleting the inode.
  739. */
  740. if (current == osb->dc_task)
  741. goto bail;
  742. spin_lock(&oi->ip_lock);
  743. /* OCFS2 *never* deletes system files. This should technically
  744. * never get here as system file inodes should always have a
  745. * positive link count. */
  746. if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
  747. mlog(ML_ERROR, "Skipping delete of system file %llu\n",
  748. (unsigned long long)oi->ip_blkno);
  749. goto bail_unlock;
  750. }
  751. ret = 1;
  752. bail_unlock:
  753. spin_unlock(&oi->ip_lock);
  754. bail:
  755. return ret;
  756. }
  757. /* Query the cluster to determine whether we should wipe an inode from
  758. * disk or not.
  759. *
  760. * Requires the inode to have the cluster lock. */
  761. static int ocfs2_query_inode_wipe(struct inode *inode,
  762. struct buffer_head *di_bh,
  763. int *wipe)
  764. {
  765. int status = 0, reason = 0;
  766. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  767. struct ocfs2_dinode *di;
  768. *wipe = 0;
  769. trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno,
  770. inode->i_nlink);
  771. /* While we were waiting for the cluster lock in
  772. * ocfs2_delete_inode, another node might have asked to delete
  773. * the inode. Recheck our flags to catch this. */
  774. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  775. reason = 1;
  776. goto bail;
  777. }
  778. /* Now that we have an up to date inode, we can double check
  779. * the link count. */
  780. if (inode->i_nlink)
  781. goto bail;
  782. /* Do some basic inode verification... */
  783. di = (struct ocfs2_dinode *) di_bh->b_data;
  784. if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) &&
  785. !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) {
  786. /*
  787. * Inodes in the orphan dir must have ORPHANED_FL. The only
  788. * inodes that come back out of the orphan dir are reflink
  789. * targets. A reflink target may be moved out of the orphan
  790. * dir between the time we scan the directory and the time we
  791. * process it. This would lead to HAS_REFCOUNT_FL being set but
  792. * ORPHANED_FL not.
  793. */
  794. if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) {
  795. reason = 2;
  796. goto bail;
  797. }
  798. /* for lack of a better error? */
  799. status = -EEXIST;
  800. mlog(ML_ERROR,
  801. "Inode %llu (on-disk %llu) not orphaned! "
  802. "Disk flags 0x%x, inode flags 0x%x\n",
  803. (unsigned long long)oi->ip_blkno,
  804. (unsigned long long)le64_to_cpu(di->i_blkno),
  805. le32_to_cpu(di->i_flags), oi->ip_flags);
  806. goto bail;
  807. }
  808. /* has someone already deleted us?! baaad... */
  809. if (di->i_dtime) {
  810. status = -EEXIST;
  811. mlog_errno(status);
  812. goto bail;
  813. }
  814. /*
  815. * This is how ocfs2 determines whether an inode is still live
  816. * within the cluster. Every node takes a shared read lock on
  817. * the inode open lock in ocfs2_read_locked_inode(). When we
  818. * get to ->delete_inode(), each node tries to convert it's
  819. * lock to an exclusive. Trylocks are serialized by the inode
  820. * meta data lock. If the upconvert succeeds, we know the inode
  821. * is no longer live and can be deleted.
  822. *
  823. * Though we call this with the meta data lock held, the
  824. * trylock keeps us from ABBA deadlock.
  825. */
  826. status = ocfs2_try_open_lock(inode, 1);
  827. if (status == -EAGAIN) {
  828. status = 0;
  829. reason = 3;
  830. goto bail;
  831. }
  832. if (status < 0) {
  833. mlog_errno(status);
  834. goto bail;
  835. }
  836. *wipe = 1;
  837. trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot));
  838. bail:
  839. trace_ocfs2_query_inode_wipe_end(status, reason);
  840. return status;
  841. }
  842. /* Support function for ocfs2_delete_inode. Will help us keep the
  843. * inode data in a consistent state for clear_inode. Always truncates
  844. * pages, optionally sync's them first. */
  845. static void ocfs2_cleanup_delete_inode(struct inode *inode,
  846. int sync_data)
  847. {
  848. trace_ocfs2_cleanup_delete_inode(
  849. (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data);
  850. if (sync_data)
  851. filemap_write_and_wait(inode->i_mapping);
  852. truncate_inode_pages_final(&inode->i_data);
  853. }
  854. static void ocfs2_delete_inode(struct inode *inode)
  855. {
  856. int wipe, status;
  857. sigset_t oldset;
  858. struct buffer_head *di_bh = NULL;
  859. struct ocfs2_dinode *di = NULL;
  860. trace_ocfs2_delete_inode(inode->i_ino,
  861. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  862. is_bad_inode(inode));
  863. /* When we fail in read_inode() we mark inode as bad. The second test
  864. * catches the case when inode allocation fails before allocating
  865. * a block for inode. */
  866. if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno)
  867. goto bail;
  868. if (!ocfs2_inode_is_valid_to_delete(inode)) {
  869. /* It's probably not necessary to truncate_inode_pages
  870. * here but we do it for safety anyway (it will most
  871. * likely be a no-op anyway) */
  872. ocfs2_cleanup_delete_inode(inode, 0);
  873. goto bail;
  874. }
  875. dquot_initialize(inode);
  876. /* We want to block signals in delete_inode as the lock and
  877. * messaging paths may return us -ERESTARTSYS. Which would
  878. * cause us to exit early, resulting in inodes being orphaned
  879. * forever. */
  880. ocfs2_block_signals(&oldset);
  881. /*
  882. * Synchronize us against ocfs2_get_dentry. We take this in
  883. * shared mode so that all nodes can still concurrently
  884. * process deletes.
  885. */
  886. status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0);
  887. if (status < 0) {
  888. mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status);
  889. ocfs2_cleanup_delete_inode(inode, 0);
  890. goto bail_unblock;
  891. }
  892. /* Lock down the inode. This gives us an up to date view of
  893. * it's metadata (for verification), and allows us to
  894. * serialize delete_inode on multiple nodes.
  895. *
  896. * Even though we might be doing a truncate, we don't take the
  897. * allocation lock here as it won't be needed - nobody will
  898. * have the file open.
  899. */
  900. status = ocfs2_inode_lock(inode, &di_bh, 1);
  901. if (status < 0) {
  902. if (status != -ENOENT)
  903. mlog_errno(status);
  904. ocfs2_cleanup_delete_inode(inode, 0);
  905. goto bail_unlock_nfs_sync;
  906. }
  907. di = (struct ocfs2_dinode *)di_bh->b_data;
  908. /* Skip inode deletion and wait for dio orphan entry recovered
  909. * first */
  910. if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) {
  911. ocfs2_cleanup_delete_inode(inode, 0);
  912. goto bail_unlock_inode;
  913. }
  914. /* Query the cluster. This will be the final decision made
  915. * before we go ahead and wipe the inode. */
  916. status = ocfs2_query_inode_wipe(inode, di_bh, &wipe);
  917. if (!wipe || status < 0) {
  918. /* Error and remote inode busy both mean we won't be
  919. * removing the inode, so they take almost the same
  920. * path. */
  921. if (status < 0)
  922. mlog_errno(status);
  923. /* Someone in the cluster has disallowed a wipe of
  924. * this inode, or it was never completely
  925. * orphaned. Write out the pages and exit now. */
  926. ocfs2_cleanup_delete_inode(inode, 1);
  927. goto bail_unlock_inode;
  928. }
  929. ocfs2_cleanup_delete_inode(inode, 0);
  930. status = ocfs2_wipe_inode(inode, di_bh);
  931. if (status < 0) {
  932. if (status != -EDEADLK)
  933. mlog_errno(status);
  934. goto bail_unlock_inode;
  935. }
  936. /*
  937. * Mark the inode as successfully deleted.
  938. *
  939. * This is important for ocfs2_clear_inode() as it will check
  940. * this flag and skip any checkpointing work
  941. *
  942. * ocfs2_stuff_meta_lvb() also uses this flag to invalidate
  943. * the LVB for other nodes.
  944. */
  945. OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED;
  946. bail_unlock_inode:
  947. ocfs2_inode_unlock(inode, 1);
  948. brelse(di_bh);
  949. bail_unlock_nfs_sync:
  950. ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0);
  951. bail_unblock:
  952. ocfs2_unblock_signals(&oldset);
  953. bail:
  954. return;
  955. }
  956. static void ocfs2_clear_inode(struct inode *inode)
  957. {
  958. int status;
  959. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  960. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  961. clear_inode(inode);
  962. trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno,
  963. inode->i_nlink);
  964. mlog_bug_on_msg(osb == NULL,
  965. "Inode=%lu\n", inode->i_ino);
  966. dquot_drop(inode);
  967. /* To preven remote deletes we hold open lock before, now it
  968. * is time to unlock PR and EX open locks. */
  969. ocfs2_open_unlock(inode);
  970. /* Do these before all the other work so that we don't bounce
  971. * the downconvert thread while waiting to destroy the locks. */
  972. ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres);
  973. ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres);
  974. ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres);
  975. ocfs2_resv_discard(&osb->osb_la_resmap,
  976. &oi->ip_la_data_resv);
  977. ocfs2_resv_init_once(&oi->ip_la_data_resv);
  978. /* We very well may get a clear_inode before all an inodes
  979. * metadata has hit disk. Of course, we can't drop any cluster
  980. * locks until the journal has finished with it. The only
  981. * exception here are successfully wiped inodes - their
  982. * metadata can now be considered to be part of the system
  983. * inodes from which it came. */
  984. if (!(oi->ip_flags & OCFS2_INODE_DELETED))
  985. ocfs2_checkpoint_inode(inode);
  986. mlog_bug_on_msg(!list_empty(&oi->ip_io_markers),
  987. "Clear inode of %llu, inode has io markers\n",
  988. (unsigned long long)oi->ip_blkno);
  989. mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list),
  990. "Clear inode of %llu, inode has unwritten extents\n",
  991. (unsigned long long)oi->ip_blkno);
  992. ocfs2_extent_map_trunc(inode, 0);
  993. status = ocfs2_drop_inode_locks(inode);
  994. if (status < 0)
  995. mlog_errno(status);
  996. ocfs2_lock_res_free(&oi->ip_rw_lockres);
  997. ocfs2_lock_res_free(&oi->ip_inode_lockres);
  998. ocfs2_lock_res_free(&oi->ip_open_lockres);
  999. ocfs2_metadata_cache_exit(INODE_CACHE(inode));
  1000. mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached,
  1001. "Clear inode of %llu, inode has %u cache items\n",
  1002. (unsigned long long)oi->ip_blkno,
  1003. INODE_CACHE(inode)->ci_num_cached);
  1004. mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE),
  1005. "Clear inode of %llu, inode has a bad flag\n",
  1006. (unsigned long long)oi->ip_blkno);
  1007. mlog_bug_on_msg(spin_is_locked(&oi->ip_lock),
  1008. "Clear inode of %llu, inode is locked\n",
  1009. (unsigned long long)oi->ip_blkno);
  1010. mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex),
  1011. "Clear inode of %llu, io_mutex is locked\n",
  1012. (unsigned long long)oi->ip_blkno);
  1013. mutex_unlock(&oi->ip_io_mutex);
  1014. /*
  1015. * down_trylock() returns 0, down_write_trylock() returns 1
  1016. * kernel 1, world 0
  1017. */
  1018. mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem),
  1019. "Clear inode of %llu, alloc_sem is locked\n",
  1020. (unsigned long long)oi->ip_blkno);
  1021. up_write(&oi->ip_alloc_sem);
  1022. mlog_bug_on_msg(oi->ip_open_count,
  1023. "Clear inode of %llu has open count %d\n",
  1024. (unsigned long long)oi->ip_blkno, oi->ip_open_count);
  1025. /* Clear all other flags. */
  1026. oi->ip_flags = 0;
  1027. oi->ip_dir_start_lookup = 0;
  1028. oi->ip_blkno = 0ULL;
  1029. /*
  1030. * ip_jinode is used to track txns against this inode. We ensure that
  1031. * the journal is flushed before journal shutdown. Thus it is safe to
  1032. * have inodes get cleaned up after journal shutdown.
  1033. */
  1034. jbd2_journal_release_jbd_inode(osb->journal->j_journal,
  1035. &oi->ip_jinode);
  1036. }
  1037. void ocfs2_evict_inode(struct inode *inode)
  1038. {
  1039. if (!inode->i_nlink ||
  1040. (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) {
  1041. ocfs2_delete_inode(inode);
  1042. } else {
  1043. truncate_inode_pages_final(&inode->i_data);
  1044. }
  1045. ocfs2_clear_inode(inode);
  1046. }
  1047. /* Called under inode_lock, with no more references on the
  1048. * struct inode, so it's safe here to check the flags field
  1049. * and to manipulate i_nlink without any other locks. */
  1050. int ocfs2_drop_inode(struct inode *inode)
  1051. {
  1052. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1053. trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno,
  1054. inode->i_nlink, oi->ip_flags);
  1055. assert_spin_locked(&inode->i_lock);
  1056. inode->i_state |= I_WILL_FREE;
  1057. spin_unlock(&inode->i_lock);
  1058. write_inode_now(inode, 1);
  1059. spin_lock(&inode->i_lock);
  1060. WARN_ON(inode->i_state & I_NEW);
  1061. inode->i_state &= ~I_WILL_FREE;
  1062. return 1;
  1063. }
  1064. /*
  1065. * This is called from our getattr.
  1066. */
  1067. int ocfs2_inode_revalidate(struct dentry *dentry)
  1068. {
  1069. struct inode *inode = d_inode(dentry);
  1070. int status = 0;
  1071. trace_ocfs2_inode_revalidate(inode,
  1072. inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL,
  1073. inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0);
  1074. if (!inode) {
  1075. status = -ENOENT;
  1076. goto bail;
  1077. }
  1078. spin_lock(&OCFS2_I(inode)->ip_lock);
  1079. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  1080. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1081. status = -ENOENT;
  1082. goto bail;
  1083. }
  1084. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1085. /* Let ocfs2_inode_lock do the work of updating our struct
  1086. * inode for us. */
  1087. status = ocfs2_inode_lock(inode, NULL, 0);
  1088. if (status < 0) {
  1089. if (status != -ENOENT)
  1090. mlog_errno(status);
  1091. goto bail;
  1092. }
  1093. ocfs2_inode_unlock(inode, 0);
  1094. bail:
  1095. return status;
  1096. }
  1097. /*
  1098. * Updates a disk inode from a
  1099. * struct inode.
  1100. * Only takes ip_lock.
  1101. */
  1102. int ocfs2_mark_inode_dirty(handle_t *handle,
  1103. struct inode *inode,
  1104. struct buffer_head *bh)
  1105. {
  1106. int status;
  1107. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  1108. trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno);
  1109. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1110. OCFS2_JOURNAL_ACCESS_WRITE);
  1111. if (status < 0) {
  1112. mlog_errno(status);
  1113. goto leave;
  1114. }
  1115. spin_lock(&OCFS2_I(inode)->ip_lock);
  1116. fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1117. ocfs2_get_inode_flags(OCFS2_I(inode));
  1118. fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr);
  1119. fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
  1120. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1121. fe->i_size = cpu_to_le64(i_size_read(inode));
  1122. ocfs2_set_links_count(fe, inode->i_nlink);
  1123. fe->i_uid = cpu_to_le32(i_uid_read(inode));
  1124. fe->i_gid = cpu_to_le32(i_gid_read(inode));
  1125. fe->i_mode = cpu_to_le16(inode->i_mode);
  1126. fe->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  1127. fe->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  1128. fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1129. fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1130. fe->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
  1131. fe->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  1132. ocfs2_journal_dirty(handle, bh);
  1133. ocfs2_update_inode_fsync_trans(handle, inode, 1);
  1134. leave:
  1135. return status;
  1136. }
  1137. /*
  1138. *
  1139. * Updates a struct inode from a disk inode.
  1140. * does no i/o, only takes ip_lock.
  1141. */
  1142. void ocfs2_refresh_inode(struct inode *inode,
  1143. struct ocfs2_dinode *fe)
  1144. {
  1145. spin_lock(&OCFS2_I(inode)->ip_lock);
  1146. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1147. OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr);
  1148. OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features);
  1149. ocfs2_set_inode_flags(inode);
  1150. i_size_write(inode, le64_to_cpu(fe->i_size));
  1151. set_nlink(inode, ocfs2_read_links_count(fe));
  1152. i_uid_write(inode, le32_to_cpu(fe->i_uid));
  1153. i_gid_write(inode, le32_to_cpu(fe->i_gid));
  1154. inode->i_mode = le16_to_cpu(fe->i_mode);
  1155. if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0)
  1156. inode->i_blocks = 0;
  1157. else
  1158. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1159. inode->i_atime.tv_sec = le64_to_cpu(fe->i_atime);
  1160. inode->i_atime.tv_nsec = le32_to_cpu(fe->i_atime_nsec);
  1161. inode->i_mtime.tv_sec = le64_to_cpu(fe->i_mtime);
  1162. inode->i_mtime.tv_nsec = le32_to_cpu(fe->i_mtime_nsec);
  1163. inode->i_ctime.tv_sec = le64_to_cpu(fe->i_ctime);
  1164. inode->i_ctime.tv_nsec = le32_to_cpu(fe->i_ctime_nsec);
  1165. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1166. }
  1167. int ocfs2_validate_inode_block(struct super_block *sb,
  1168. struct buffer_head *bh)
  1169. {
  1170. int rc;
  1171. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1172. trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr);
  1173. BUG_ON(!buffer_uptodate(bh));
  1174. /*
  1175. * If the ecc fails, we return the error but otherwise
  1176. * leave the filesystem running. We know any error is
  1177. * local to this block.
  1178. */
  1179. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1180. if (rc) {
  1181. mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
  1182. (unsigned long long)bh->b_blocknr);
  1183. goto bail;
  1184. }
  1185. /*
  1186. * Errors after here are fatal.
  1187. */
  1188. rc = -EINVAL;
  1189. if (!OCFS2_IS_VALID_DINODE(di)) {
  1190. rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n",
  1191. (unsigned long long)bh->b_blocknr, 7,
  1192. di->i_signature);
  1193. goto bail;
  1194. }
  1195. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1196. rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n",
  1197. (unsigned long long)bh->b_blocknr,
  1198. (unsigned long long)le64_to_cpu(di->i_blkno));
  1199. goto bail;
  1200. }
  1201. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1202. rc = ocfs2_error(sb,
  1203. "Invalid dinode #%llu: OCFS2_VALID_FL not set\n",
  1204. (unsigned long long)bh->b_blocknr);
  1205. goto bail;
  1206. }
  1207. if (le32_to_cpu(di->i_fs_generation) !=
  1208. OCFS2_SB(sb)->fs_generation) {
  1209. rc = ocfs2_error(sb,
  1210. "Invalid dinode #%llu: fs_generation is %u\n",
  1211. (unsigned long long)bh->b_blocknr,
  1212. le32_to_cpu(di->i_fs_generation));
  1213. goto bail;
  1214. }
  1215. rc = 0;
  1216. bail:
  1217. return rc;
  1218. }
  1219. static int ocfs2_filecheck_validate_inode_block(struct super_block *sb,
  1220. struct buffer_head *bh)
  1221. {
  1222. int rc = 0;
  1223. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1224. trace_ocfs2_filecheck_validate_inode_block(
  1225. (unsigned long long)bh->b_blocknr);
  1226. BUG_ON(!buffer_uptodate(bh));
  1227. /*
  1228. * Call ocfs2_validate_meta_ecc() first since it has ecc repair
  1229. * function, but we should not return error immediately when ecc
  1230. * validation fails, because the reason is quite likely the invalid
  1231. * inode number inputed.
  1232. */
  1233. rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check);
  1234. if (rc) {
  1235. mlog(ML_ERROR,
  1236. "Filecheck: checksum failed for dinode %llu\n",
  1237. (unsigned long long)bh->b_blocknr);
  1238. rc = -OCFS2_FILECHECK_ERR_BLOCKECC;
  1239. }
  1240. if (!OCFS2_IS_VALID_DINODE(di)) {
  1241. mlog(ML_ERROR,
  1242. "Filecheck: invalid dinode #%llu: signature = %.*s\n",
  1243. (unsigned long long)bh->b_blocknr, 7, di->i_signature);
  1244. rc = -OCFS2_FILECHECK_ERR_INVALIDINO;
  1245. goto bail;
  1246. } else if (rc)
  1247. goto bail;
  1248. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1249. mlog(ML_ERROR,
  1250. "Filecheck: invalid dinode #%llu: i_blkno is %llu\n",
  1251. (unsigned long long)bh->b_blocknr,
  1252. (unsigned long long)le64_to_cpu(di->i_blkno));
  1253. rc = -OCFS2_FILECHECK_ERR_BLOCKNO;
  1254. goto bail;
  1255. }
  1256. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1257. mlog(ML_ERROR,
  1258. "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL "
  1259. "not set\n",
  1260. (unsigned long long)bh->b_blocknr);
  1261. rc = -OCFS2_FILECHECK_ERR_VALIDFLAG;
  1262. goto bail;
  1263. }
  1264. if (le32_to_cpu(di->i_fs_generation) !=
  1265. OCFS2_SB(sb)->fs_generation) {
  1266. mlog(ML_ERROR,
  1267. "Filecheck: invalid dinode #%llu: fs_generation is %u\n",
  1268. (unsigned long long)bh->b_blocknr,
  1269. le32_to_cpu(di->i_fs_generation));
  1270. rc = -OCFS2_FILECHECK_ERR_GENERATION;
  1271. }
  1272. bail:
  1273. return rc;
  1274. }
  1275. static int ocfs2_filecheck_repair_inode_block(struct super_block *sb,
  1276. struct buffer_head *bh)
  1277. {
  1278. int changed = 0;
  1279. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  1280. if (!ocfs2_filecheck_validate_inode_block(sb, bh))
  1281. return 0;
  1282. trace_ocfs2_filecheck_repair_inode_block(
  1283. (unsigned long long)bh->b_blocknr);
  1284. if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) ||
  1285. ocfs2_is_soft_readonly(OCFS2_SB(sb))) {
  1286. mlog(ML_ERROR,
  1287. "Filecheck: cannot repair dinode #%llu "
  1288. "on readonly filesystem\n",
  1289. (unsigned long long)bh->b_blocknr);
  1290. return -OCFS2_FILECHECK_ERR_READONLY;
  1291. }
  1292. if (buffer_jbd(bh)) {
  1293. mlog(ML_ERROR,
  1294. "Filecheck: cannot repair dinode #%llu, "
  1295. "its buffer is in jbd\n",
  1296. (unsigned long long)bh->b_blocknr);
  1297. return -OCFS2_FILECHECK_ERR_INJBD;
  1298. }
  1299. if (!OCFS2_IS_VALID_DINODE(di)) {
  1300. /* Cannot fix invalid inode block */
  1301. return -OCFS2_FILECHECK_ERR_INVALIDINO;
  1302. }
  1303. if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) {
  1304. /* Cannot just add VALID_FL flag back as a fix,
  1305. * need more things to check here.
  1306. */
  1307. return -OCFS2_FILECHECK_ERR_VALIDFLAG;
  1308. }
  1309. if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) {
  1310. di->i_blkno = cpu_to_le64(bh->b_blocknr);
  1311. changed = 1;
  1312. mlog(ML_ERROR,
  1313. "Filecheck: reset dinode #%llu: i_blkno to %llu\n",
  1314. (unsigned long long)bh->b_blocknr,
  1315. (unsigned long long)le64_to_cpu(di->i_blkno));
  1316. }
  1317. if (le32_to_cpu(di->i_fs_generation) !=
  1318. OCFS2_SB(sb)->fs_generation) {
  1319. di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  1320. changed = 1;
  1321. mlog(ML_ERROR,
  1322. "Filecheck: reset dinode #%llu: fs_generation to %u\n",
  1323. (unsigned long long)bh->b_blocknr,
  1324. le32_to_cpu(di->i_fs_generation));
  1325. }
  1326. if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) {
  1327. ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check);
  1328. mark_buffer_dirty(bh);
  1329. mlog(ML_ERROR,
  1330. "Filecheck: reset dinode #%llu: compute meta ecc\n",
  1331. (unsigned long long)bh->b_blocknr);
  1332. }
  1333. return 0;
  1334. }
  1335. static int
  1336. ocfs2_filecheck_read_inode_block_full(struct inode *inode,
  1337. struct buffer_head **bh,
  1338. int flags, int type)
  1339. {
  1340. int rc;
  1341. struct buffer_head *tmp = *bh;
  1342. if (!type) /* Check inode block */
  1343. rc = ocfs2_read_blocks(INODE_CACHE(inode),
  1344. OCFS2_I(inode)->ip_blkno,
  1345. 1, &tmp, flags,
  1346. ocfs2_filecheck_validate_inode_block);
  1347. else /* Repair inode block */
  1348. rc = ocfs2_read_blocks(INODE_CACHE(inode),
  1349. OCFS2_I(inode)->ip_blkno,
  1350. 1, &tmp, flags,
  1351. ocfs2_filecheck_repair_inode_block);
  1352. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1353. if (!rc && !*bh)
  1354. *bh = tmp;
  1355. return rc;
  1356. }
  1357. int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh,
  1358. int flags)
  1359. {
  1360. int rc;
  1361. struct buffer_head *tmp = *bh;
  1362. rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno,
  1363. 1, &tmp, flags, ocfs2_validate_inode_block);
  1364. /* If ocfs2_read_blocks() got us a new bh, pass it up. */
  1365. if (!rc && !*bh)
  1366. *bh = tmp;
  1367. return rc;
  1368. }
  1369. int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh)
  1370. {
  1371. return ocfs2_read_inode_block_full(inode, bh, 0);
  1372. }
  1373. static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci)
  1374. {
  1375. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1376. return oi->ip_blkno;
  1377. }
  1378. static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci)
  1379. {
  1380. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1381. return oi->vfs_inode.i_sb;
  1382. }
  1383. static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci)
  1384. {
  1385. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1386. spin_lock(&oi->ip_lock);
  1387. }
  1388. static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci)
  1389. {
  1390. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1391. spin_unlock(&oi->ip_lock);
  1392. }
  1393. static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci)
  1394. {
  1395. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1396. mutex_lock(&oi->ip_io_mutex);
  1397. }
  1398. static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci)
  1399. {
  1400. struct ocfs2_inode_info *oi = cache_info_to_inode(ci);
  1401. mutex_unlock(&oi->ip_io_mutex);
  1402. }
  1403. const struct ocfs2_caching_operations ocfs2_inode_caching_ops = {
  1404. .co_owner = ocfs2_inode_cache_owner,
  1405. .co_get_super = ocfs2_inode_cache_get_super,
  1406. .co_cache_lock = ocfs2_inode_cache_lock,
  1407. .co_cache_unlock = ocfs2_inode_cache_unlock,
  1408. .co_io_lock = ocfs2_inode_cache_io_lock,
  1409. .co_io_unlock = ocfs2_inode_cache_io_unlock,
  1410. };