inode.c 39 KB

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