inode.c 39 KB

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