file.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  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/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/pipe_fs_i.h>
  34. #include <linux/mount.h>
  35. #define MLOG_MASK_PREFIX ML_INODE
  36. #include <cluster/masklog.h>
  37. #include "ocfs2.h"
  38. #include "alloc.h"
  39. #include "aops.h"
  40. #include "dir.h"
  41. #include "dlmglue.h"
  42. #include "extent_map.h"
  43. #include "file.h"
  44. #include "sysfile.h"
  45. #include "inode.h"
  46. #include "ioctl.h"
  47. #include "journal.h"
  48. #include "mmap.h"
  49. #include "suballoc.h"
  50. #include "super.h"
  51. #include "buffer_head_io.h"
  52. static int ocfs2_sync_inode(struct inode *inode)
  53. {
  54. filemap_fdatawrite(inode->i_mapping);
  55. return sync_mapping_buffers(inode->i_mapping);
  56. }
  57. static int ocfs2_file_open(struct inode *inode, struct file *file)
  58. {
  59. int status;
  60. int mode = file->f_flags;
  61. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  62. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  63. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  64. spin_lock(&oi->ip_lock);
  65. /* Check that the inode hasn't been wiped from disk by another
  66. * node. If it hasn't then we're safe as long as we hold the
  67. * spin lock until our increment of open count. */
  68. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  69. spin_unlock(&oi->ip_lock);
  70. status = -ENOENT;
  71. goto leave;
  72. }
  73. if (mode & O_DIRECT)
  74. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  75. oi->ip_open_count++;
  76. spin_unlock(&oi->ip_lock);
  77. status = 0;
  78. leave:
  79. mlog_exit(status);
  80. return status;
  81. }
  82. static int ocfs2_file_release(struct inode *inode, struct file *file)
  83. {
  84. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  85. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  86. file->f_path.dentry->d_name.len,
  87. file->f_path.dentry->d_name.name);
  88. spin_lock(&oi->ip_lock);
  89. if (!--oi->ip_open_count)
  90. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  91. spin_unlock(&oi->ip_lock);
  92. mlog_exit(0);
  93. return 0;
  94. }
  95. static int ocfs2_sync_file(struct file *file,
  96. struct dentry *dentry,
  97. int datasync)
  98. {
  99. int err = 0;
  100. journal_t *journal;
  101. struct inode *inode = dentry->d_inode;
  102. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  103. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  104. dentry->d_name.len, dentry->d_name.name);
  105. err = ocfs2_sync_inode(dentry->d_inode);
  106. if (err)
  107. goto bail;
  108. journal = osb->journal->j_journal;
  109. err = journal_force_commit(journal);
  110. bail:
  111. mlog_exit(err);
  112. return (err < 0) ? -EIO : 0;
  113. }
  114. int ocfs2_should_update_atime(struct inode *inode,
  115. struct vfsmount *vfsmnt)
  116. {
  117. struct timespec now;
  118. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  119. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  120. return 0;
  121. if ((inode->i_flags & S_NOATIME) ||
  122. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  123. return 0;
  124. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  125. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  126. return 0;
  127. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  128. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  129. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  130. return 1;
  131. return 0;
  132. }
  133. now = CURRENT_TIME;
  134. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  135. return 0;
  136. else
  137. return 1;
  138. }
  139. int ocfs2_update_inode_atime(struct inode *inode,
  140. struct buffer_head *bh)
  141. {
  142. int ret;
  143. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  144. handle_t *handle;
  145. mlog_entry_void();
  146. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  147. if (handle == NULL) {
  148. ret = -ENOMEM;
  149. mlog_errno(ret);
  150. goto out;
  151. }
  152. inode->i_atime = CURRENT_TIME;
  153. ret = ocfs2_mark_inode_dirty(handle, inode, bh);
  154. if (ret < 0)
  155. mlog_errno(ret);
  156. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  157. out:
  158. mlog_exit(ret);
  159. return ret;
  160. }
  161. int ocfs2_set_inode_size(handle_t *handle,
  162. struct inode *inode,
  163. struct buffer_head *fe_bh,
  164. u64 new_i_size)
  165. {
  166. int status;
  167. mlog_entry_void();
  168. i_size_write(inode, new_i_size);
  169. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  170. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  171. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  172. if (status < 0) {
  173. mlog_errno(status);
  174. goto bail;
  175. }
  176. bail:
  177. mlog_exit(status);
  178. return status;
  179. }
  180. static int ocfs2_simple_size_update(struct inode *inode,
  181. struct buffer_head *di_bh,
  182. u64 new_i_size)
  183. {
  184. int ret;
  185. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  186. handle_t *handle = NULL;
  187. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  188. if (handle == NULL) {
  189. ret = -ENOMEM;
  190. mlog_errno(ret);
  191. goto out;
  192. }
  193. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  194. new_i_size);
  195. if (ret < 0)
  196. mlog_errno(ret);
  197. ocfs2_commit_trans(osb, handle);
  198. out:
  199. return ret;
  200. }
  201. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  202. struct inode *inode,
  203. struct buffer_head *fe_bh,
  204. u64 new_i_size)
  205. {
  206. int status;
  207. handle_t *handle;
  208. mlog_entry_void();
  209. /* TODO: This needs to actually orphan the inode in this
  210. * transaction. */
  211. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  212. if (IS_ERR(handle)) {
  213. status = PTR_ERR(handle);
  214. mlog_errno(status);
  215. goto out;
  216. }
  217. status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size);
  218. if (status < 0)
  219. mlog_errno(status);
  220. ocfs2_commit_trans(osb, handle);
  221. out:
  222. mlog_exit(status);
  223. return status;
  224. }
  225. static int ocfs2_truncate_file(struct inode *inode,
  226. struct buffer_head *di_bh,
  227. u64 new_i_size)
  228. {
  229. int status = 0;
  230. struct ocfs2_dinode *fe = NULL;
  231. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  232. struct ocfs2_truncate_context *tc = NULL;
  233. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  234. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  235. (unsigned long long)new_i_size);
  236. truncate_inode_pages(inode->i_mapping, new_i_size);
  237. fe = (struct ocfs2_dinode *) di_bh->b_data;
  238. if (!OCFS2_IS_VALID_DINODE(fe)) {
  239. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  240. status = -EIO;
  241. goto bail;
  242. }
  243. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  244. "Inode %llu, inode i_size = %lld != di "
  245. "i_size = %llu, i_flags = 0x%x\n",
  246. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  247. i_size_read(inode),
  248. (unsigned long long)le64_to_cpu(fe->i_size),
  249. le32_to_cpu(fe->i_flags));
  250. if (new_i_size > le64_to_cpu(fe->i_size)) {
  251. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  252. (unsigned long long)le64_to_cpu(fe->i_size),
  253. (unsigned long long)new_i_size);
  254. status = -EINVAL;
  255. mlog_errno(status);
  256. goto bail;
  257. }
  258. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  259. (unsigned long long)le64_to_cpu(fe->i_blkno),
  260. (unsigned long long)le64_to_cpu(fe->i_size),
  261. (unsigned long long)new_i_size);
  262. /* lets handle the simple truncate cases before doing any more
  263. * cluster locking. */
  264. if (new_i_size == le64_to_cpu(fe->i_size))
  265. goto bail;
  266. /* This forces other nodes to sync and drop their pages. Do
  267. * this even if we have a truncate without allocation change -
  268. * ocfs2 cluster sizes can be much greater than page size, so
  269. * we have to truncate them anyway. */
  270. status = ocfs2_data_lock(inode, 1);
  271. if (status < 0) {
  272. mlog_errno(status);
  273. goto bail;
  274. }
  275. ocfs2_data_unlock(inode, 1);
  276. if (le32_to_cpu(fe->i_clusters) ==
  277. ocfs2_clusters_for_bytes(osb->sb, new_i_size)) {
  278. mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n",
  279. fe->i_clusters);
  280. /* No allocation change is required, so lets fast path
  281. * this truncate. */
  282. status = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  283. if (status < 0)
  284. mlog_errno(status);
  285. goto bail;
  286. }
  287. /* alright, we're going to need to do a full blown alloc size
  288. * change. Orphan the inode so that recovery can complete the
  289. * truncate if necessary. This does the task of marking
  290. * i_size. */
  291. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  292. if (status < 0) {
  293. mlog_errno(status);
  294. goto bail;
  295. }
  296. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  297. if (status < 0) {
  298. mlog_errno(status);
  299. goto bail;
  300. }
  301. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  302. if (status < 0) {
  303. mlog_errno(status);
  304. goto bail;
  305. }
  306. /* TODO: orphan dir cleanup here. */
  307. bail:
  308. mlog_exit(status);
  309. return status;
  310. }
  311. /*
  312. * extend allocation only here.
  313. * we'll update all the disk stuff, and oip->alloc_size
  314. *
  315. * expect stuff to be locked, a transaction started and enough data /
  316. * metadata reservations in the contexts.
  317. *
  318. * Will return -EAGAIN, and a reason if a restart is needed.
  319. * If passed in, *reason will always be set, even in error.
  320. */
  321. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  322. struct inode *inode,
  323. u32 clusters_to_add,
  324. struct buffer_head *fe_bh,
  325. handle_t *handle,
  326. struct ocfs2_alloc_context *data_ac,
  327. struct ocfs2_alloc_context *meta_ac,
  328. enum ocfs2_alloc_restarted *reason_ret)
  329. {
  330. int status = 0;
  331. int free_extents;
  332. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  333. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  334. u32 bit_off, num_bits;
  335. u64 block;
  336. BUG_ON(!clusters_to_add);
  337. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  338. if (free_extents < 0) {
  339. status = free_extents;
  340. mlog_errno(status);
  341. goto leave;
  342. }
  343. /* there are two cases which could cause us to EAGAIN in the
  344. * we-need-more-metadata case:
  345. * 1) we haven't reserved *any*
  346. * 2) we are so fragmented, we've needed to add metadata too
  347. * many times. */
  348. if (!free_extents && !meta_ac) {
  349. mlog(0, "we haven't reserved any metadata!\n");
  350. status = -EAGAIN;
  351. reason = RESTART_META;
  352. goto leave;
  353. } else if ((!free_extents)
  354. && (ocfs2_alloc_context_bits_left(meta_ac)
  355. < ocfs2_extend_meta_needed(fe))) {
  356. mlog(0, "filesystem is really fragmented...\n");
  357. status = -EAGAIN;
  358. reason = RESTART_META;
  359. goto leave;
  360. }
  361. status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
  362. &bit_off, &num_bits);
  363. if (status < 0) {
  364. if (status != -ENOSPC)
  365. mlog_errno(status);
  366. goto leave;
  367. }
  368. BUG_ON(num_bits > clusters_to_add);
  369. /* reserve our write early -- insert_extent may update the inode */
  370. status = ocfs2_journal_access(handle, inode, fe_bh,
  371. OCFS2_JOURNAL_ACCESS_WRITE);
  372. if (status < 0) {
  373. mlog_errno(status);
  374. goto leave;
  375. }
  376. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  377. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  378. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  379. status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block,
  380. num_bits, meta_ac);
  381. if (status < 0) {
  382. mlog_errno(status);
  383. goto leave;
  384. }
  385. le32_add_cpu(&fe->i_clusters, num_bits);
  386. spin_lock(&OCFS2_I(inode)->ip_lock);
  387. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  388. spin_unlock(&OCFS2_I(inode)->ip_lock);
  389. status = ocfs2_journal_dirty(handle, fe_bh);
  390. if (status < 0) {
  391. mlog_errno(status);
  392. goto leave;
  393. }
  394. clusters_to_add -= num_bits;
  395. if (clusters_to_add) {
  396. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  397. "%u\n", fe->i_clusters, clusters_to_add);
  398. status = -EAGAIN;
  399. reason = RESTART_TRANS;
  400. }
  401. leave:
  402. mlog_exit(status);
  403. if (reason_ret)
  404. *reason_ret = reason;
  405. return status;
  406. }
  407. static int ocfs2_extend_allocation(struct inode *inode,
  408. u32 clusters_to_add)
  409. {
  410. int status = 0;
  411. int restart_func = 0;
  412. int drop_alloc_sem = 0;
  413. int credits, num_free_extents;
  414. u32 prev_clusters;
  415. struct buffer_head *bh = NULL;
  416. struct ocfs2_dinode *fe = NULL;
  417. handle_t *handle = NULL;
  418. struct ocfs2_alloc_context *data_ac = NULL;
  419. struct ocfs2_alloc_context *meta_ac = NULL;
  420. enum ocfs2_alloc_restarted why;
  421. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  422. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  423. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  424. OCFS2_BH_CACHED, inode);
  425. if (status < 0) {
  426. mlog_errno(status);
  427. goto leave;
  428. }
  429. fe = (struct ocfs2_dinode *) bh->b_data;
  430. if (!OCFS2_IS_VALID_DINODE(fe)) {
  431. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  432. status = -EIO;
  433. goto leave;
  434. }
  435. restart_all:
  436. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  437. mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, "
  438. "clusters_to_add = %u\n",
  439. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  440. fe->i_clusters, clusters_to_add);
  441. num_free_extents = ocfs2_num_free_extents(osb,
  442. inode,
  443. fe);
  444. if (num_free_extents < 0) {
  445. status = num_free_extents;
  446. mlog_errno(status);
  447. goto leave;
  448. }
  449. if (!num_free_extents) {
  450. status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
  451. if (status < 0) {
  452. if (status != -ENOSPC)
  453. mlog_errno(status);
  454. goto leave;
  455. }
  456. }
  457. status = ocfs2_reserve_clusters(osb, clusters_to_add, &data_ac);
  458. if (status < 0) {
  459. if (status != -ENOSPC)
  460. mlog_errno(status);
  461. goto leave;
  462. }
  463. /* blocks peope in read/write from reading our allocation
  464. * until we're done changing it. We depend on i_mutex to block
  465. * other extend/truncate calls while we're here. Ordering wrt
  466. * start_trans is important here -- always do it before! */
  467. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  468. drop_alloc_sem = 1;
  469. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  470. handle = ocfs2_start_trans(osb, credits);
  471. if (IS_ERR(handle)) {
  472. status = PTR_ERR(handle);
  473. handle = NULL;
  474. mlog_errno(status);
  475. goto leave;
  476. }
  477. restarted_transaction:
  478. /* reserve a write to the file entry early on - that we if we
  479. * run out of credits in the allocation path, we can still
  480. * update i_size. */
  481. status = ocfs2_journal_access(handle, inode, bh,
  482. OCFS2_JOURNAL_ACCESS_WRITE);
  483. if (status < 0) {
  484. mlog_errno(status);
  485. goto leave;
  486. }
  487. prev_clusters = OCFS2_I(inode)->ip_clusters;
  488. status = ocfs2_do_extend_allocation(osb,
  489. inode,
  490. clusters_to_add,
  491. bh,
  492. handle,
  493. data_ac,
  494. meta_ac,
  495. &why);
  496. if ((status < 0) && (status != -EAGAIN)) {
  497. if (status != -ENOSPC)
  498. mlog_errno(status);
  499. goto leave;
  500. }
  501. status = ocfs2_journal_dirty(handle, bh);
  502. if (status < 0) {
  503. mlog_errno(status);
  504. goto leave;
  505. }
  506. spin_lock(&OCFS2_I(inode)->ip_lock);
  507. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  508. spin_unlock(&OCFS2_I(inode)->ip_lock);
  509. if (why != RESTART_NONE && clusters_to_add) {
  510. if (why == RESTART_META) {
  511. mlog(0, "restarting function.\n");
  512. restart_func = 1;
  513. } else {
  514. BUG_ON(why != RESTART_TRANS);
  515. mlog(0, "restarting transaction.\n");
  516. /* TODO: This can be more intelligent. */
  517. credits = ocfs2_calc_extend_credits(osb->sb,
  518. fe,
  519. clusters_to_add);
  520. status = ocfs2_extend_trans(handle, credits);
  521. if (status < 0) {
  522. /* handle still has to be committed at
  523. * this point. */
  524. status = -ENOMEM;
  525. mlog_errno(status);
  526. goto leave;
  527. }
  528. goto restarted_transaction;
  529. }
  530. }
  531. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  532. fe->i_clusters, (unsigned long long)fe->i_size);
  533. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  534. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  535. leave:
  536. if (drop_alloc_sem) {
  537. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  538. drop_alloc_sem = 0;
  539. }
  540. if (handle) {
  541. ocfs2_commit_trans(osb, handle);
  542. handle = NULL;
  543. }
  544. if (data_ac) {
  545. ocfs2_free_alloc_context(data_ac);
  546. data_ac = NULL;
  547. }
  548. if (meta_ac) {
  549. ocfs2_free_alloc_context(meta_ac);
  550. meta_ac = NULL;
  551. }
  552. if ((!status) && restart_func) {
  553. restart_func = 0;
  554. goto restart_all;
  555. }
  556. if (bh) {
  557. brelse(bh);
  558. bh = NULL;
  559. }
  560. mlog_exit(status);
  561. return status;
  562. }
  563. /* Some parts of this taken from generic_cont_expand, which turned out
  564. * to be too fragile to do exactly what we need without us having to
  565. * worry about recursive locking in ->prepare_write() and
  566. * ->commit_write(). */
  567. static int ocfs2_write_zero_page(struct inode *inode,
  568. u64 size)
  569. {
  570. struct address_space *mapping = inode->i_mapping;
  571. struct page *page;
  572. unsigned long index;
  573. unsigned int offset;
  574. handle_t *handle = NULL;
  575. int ret;
  576. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  577. /* ugh. in prepare/commit_write, if from==to==start of block, we
  578. ** skip the prepare. make sure we never send an offset for the start
  579. ** of a block
  580. */
  581. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  582. offset++;
  583. }
  584. index = size >> PAGE_CACHE_SHIFT;
  585. page = grab_cache_page(mapping, index);
  586. if (!page) {
  587. ret = -ENOMEM;
  588. mlog_errno(ret);
  589. goto out;
  590. }
  591. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  592. if (ret < 0) {
  593. mlog_errno(ret);
  594. goto out_unlock;
  595. }
  596. if (ocfs2_should_order_data(inode)) {
  597. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  598. offset);
  599. if (IS_ERR(handle)) {
  600. ret = PTR_ERR(handle);
  601. handle = NULL;
  602. goto out_unlock;
  603. }
  604. }
  605. /* must not update i_size! */
  606. ret = block_commit_write(page, offset, offset);
  607. if (ret < 0)
  608. mlog_errno(ret);
  609. else
  610. ret = 0;
  611. if (handle)
  612. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  613. out_unlock:
  614. unlock_page(page);
  615. page_cache_release(page);
  616. out:
  617. return ret;
  618. }
  619. static int ocfs2_zero_extend(struct inode *inode,
  620. u64 zero_to_size)
  621. {
  622. int ret = 0;
  623. u64 start_off;
  624. struct super_block *sb = inode->i_sb;
  625. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  626. while (start_off < zero_to_size) {
  627. ret = ocfs2_write_zero_page(inode, start_off);
  628. if (ret < 0) {
  629. mlog_errno(ret);
  630. goto out;
  631. }
  632. start_off += sb->s_blocksize;
  633. /*
  634. * Very large extends have the potential to lock up
  635. * the cpu for extended periods of time.
  636. */
  637. cond_resched();
  638. }
  639. out:
  640. return ret;
  641. }
  642. /*
  643. * A tail_to_skip value > 0 indicates that we're being called from
  644. * ocfs2_file_aio_write(). This has the following implications:
  645. *
  646. * - we don't want to update i_size
  647. * - di_bh will be NULL, which is fine because it's only used in the
  648. * case where we want to update i_size.
  649. * - ocfs2_zero_extend() will then only be filling the hole created
  650. * between i_size and the start of the write.
  651. */
  652. static int ocfs2_extend_file(struct inode *inode,
  653. struct buffer_head *di_bh,
  654. u64 new_i_size,
  655. size_t tail_to_skip)
  656. {
  657. int ret = 0;
  658. u32 clusters_to_add;
  659. BUG_ON(!tail_to_skip && !di_bh);
  660. /* setattr sometimes calls us like this. */
  661. if (new_i_size == 0)
  662. goto out;
  663. if (i_size_read(inode) == new_i_size)
  664. goto out;
  665. BUG_ON(new_i_size < i_size_read(inode));
  666. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  667. OCFS2_I(inode)->ip_clusters;
  668. /*
  669. * protect the pages that ocfs2_zero_extend is going to be
  670. * pulling into the page cache.. we do this before the
  671. * metadata extend so that we don't get into the situation
  672. * where we've extended the metadata but can't get the data
  673. * lock to zero.
  674. */
  675. ret = ocfs2_data_lock(inode, 1);
  676. if (ret < 0) {
  677. mlog_errno(ret);
  678. goto out;
  679. }
  680. if (clusters_to_add) {
  681. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  682. if (ret < 0) {
  683. mlog_errno(ret);
  684. goto out_unlock;
  685. }
  686. }
  687. /*
  688. * Call this even if we don't add any clusters to the tree. We
  689. * still need to zero the area between the old i_size and the
  690. * new i_size.
  691. */
  692. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  693. if (ret < 0) {
  694. mlog_errno(ret);
  695. goto out_unlock;
  696. }
  697. if (!tail_to_skip) {
  698. /* We're being called from ocfs2_setattr() which wants
  699. * us to update i_size */
  700. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  701. if (ret < 0)
  702. mlog_errno(ret);
  703. }
  704. out_unlock:
  705. ocfs2_data_unlock(inode, 1);
  706. out:
  707. return ret;
  708. }
  709. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  710. {
  711. int status = 0, size_change;
  712. struct inode *inode = dentry->d_inode;
  713. struct super_block *sb = inode->i_sb;
  714. struct ocfs2_super *osb = OCFS2_SB(sb);
  715. struct buffer_head *bh = NULL;
  716. handle_t *handle = NULL;
  717. mlog_entry("(0x%p, '%.*s')\n", dentry,
  718. dentry->d_name.len, dentry->d_name.name);
  719. if (attr->ia_valid & ATTR_MODE)
  720. mlog(0, "mode change: %d\n", attr->ia_mode);
  721. if (attr->ia_valid & ATTR_UID)
  722. mlog(0, "uid change: %d\n", attr->ia_uid);
  723. if (attr->ia_valid & ATTR_GID)
  724. mlog(0, "gid change: %d\n", attr->ia_gid);
  725. if (attr->ia_valid & ATTR_SIZE)
  726. mlog(0, "size change...\n");
  727. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  728. mlog(0, "time change...\n");
  729. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  730. | ATTR_GID | ATTR_UID | ATTR_MODE)
  731. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  732. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  733. return 0;
  734. }
  735. status = inode_change_ok(inode, attr);
  736. if (status)
  737. return status;
  738. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  739. if (size_change) {
  740. status = ocfs2_rw_lock(inode, 1);
  741. if (status < 0) {
  742. mlog_errno(status);
  743. goto bail;
  744. }
  745. }
  746. status = ocfs2_meta_lock(inode, &bh, 1);
  747. if (status < 0) {
  748. if (status != -ENOENT)
  749. mlog_errno(status);
  750. goto bail_unlock_rw;
  751. }
  752. if (size_change && attr->ia_size != i_size_read(inode)) {
  753. if (i_size_read(inode) > attr->ia_size)
  754. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  755. else
  756. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  757. if (status < 0) {
  758. if (status != -ENOSPC)
  759. mlog_errno(status);
  760. status = -ENOSPC;
  761. goto bail_unlock;
  762. }
  763. }
  764. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  765. if (IS_ERR(handle)) {
  766. status = PTR_ERR(handle);
  767. mlog_errno(status);
  768. goto bail_unlock;
  769. }
  770. status = inode_setattr(inode, attr);
  771. if (status < 0) {
  772. mlog_errno(status);
  773. goto bail_commit;
  774. }
  775. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  776. if (status < 0)
  777. mlog_errno(status);
  778. bail_commit:
  779. ocfs2_commit_trans(osb, handle);
  780. bail_unlock:
  781. ocfs2_meta_unlock(inode, 1);
  782. bail_unlock_rw:
  783. if (size_change)
  784. ocfs2_rw_unlock(inode, 1);
  785. bail:
  786. if (bh)
  787. brelse(bh);
  788. mlog_exit(status);
  789. return status;
  790. }
  791. int ocfs2_getattr(struct vfsmount *mnt,
  792. struct dentry *dentry,
  793. struct kstat *stat)
  794. {
  795. struct inode *inode = dentry->d_inode;
  796. struct super_block *sb = dentry->d_inode->i_sb;
  797. struct ocfs2_super *osb = sb->s_fs_info;
  798. int err;
  799. mlog_entry_void();
  800. err = ocfs2_inode_revalidate(dentry);
  801. if (err) {
  802. if (err != -ENOENT)
  803. mlog_errno(err);
  804. goto bail;
  805. }
  806. generic_fillattr(inode, stat);
  807. /* We set the blksize from the cluster size for performance */
  808. stat->blksize = osb->s_clustersize;
  809. bail:
  810. mlog_exit(err);
  811. return err;
  812. }
  813. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  814. {
  815. int ret;
  816. mlog_entry_void();
  817. ret = ocfs2_meta_lock(inode, NULL, 0);
  818. if (ret) {
  819. mlog_errno(ret);
  820. goto out;
  821. }
  822. ret = generic_permission(inode, mask, NULL);
  823. if (ret)
  824. mlog_errno(ret);
  825. ocfs2_meta_unlock(inode, 0);
  826. out:
  827. mlog_exit(ret);
  828. return ret;
  829. }
  830. static int ocfs2_write_remove_suid(struct inode *inode)
  831. {
  832. int ret;
  833. struct buffer_head *bh = NULL;
  834. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  835. handle_t *handle;
  836. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  837. struct ocfs2_dinode *di;
  838. mlog_entry("(Inode %llu, mode 0%o)\n",
  839. (unsigned long long)oi->ip_blkno, inode->i_mode);
  840. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  841. if (handle == NULL) {
  842. ret = -ENOMEM;
  843. mlog_errno(ret);
  844. goto out;
  845. }
  846. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  847. if (ret < 0) {
  848. mlog_errno(ret);
  849. goto out_trans;
  850. }
  851. ret = ocfs2_journal_access(handle, inode, bh,
  852. OCFS2_JOURNAL_ACCESS_WRITE);
  853. if (ret < 0) {
  854. mlog_errno(ret);
  855. goto out_bh;
  856. }
  857. inode->i_mode &= ~S_ISUID;
  858. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  859. inode->i_mode &= ~S_ISGID;
  860. di = (struct ocfs2_dinode *) bh->b_data;
  861. di->i_mode = cpu_to_le16(inode->i_mode);
  862. ret = ocfs2_journal_dirty(handle, bh);
  863. if (ret < 0)
  864. mlog_errno(ret);
  865. out_bh:
  866. brelse(bh);
  867. out_trans:
  868. ocfs2_commit_trans(osb, handle);
  869. out:
  870. mlog_exit(ret);
  871. return ret;
  872. }
  873. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  874. loff_t *ppos,
  875. size_t count,
  876. int appending)
  877. {
  878. int ret = 0, meta_level = appending;
  879. struct inode *inode = dentry->d_inode;
  880. u32 clusters;
  881. loff_t newsize, saved_pos;
  882. /*
  883. * We sample i_size under a read level meta lock to see if our write
  884. * is extending the file, if it is we back off and get a write level
  885. * meta lock.
  886. */
  887. for(;;) {
  888. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  889. if (ret < 0) {
  890. meta_level = -1;
  891. mlog_errno(ret);
  892. goto out;
  893. }
  894. /* Clear suid / sgid if necessary. We do this here
  895. * instead of later in the write path because
  896. * remove_suid() calls ->setattr without any hint that
  897. * we may have already done our cluster locking. Since
  898. * ocfs2_setattr() *must* take cluster locks to
  899. * proceeed, this will lead us to recursively lock the
  900. * inode. There's also the dinode i_size state which
  901. * can be lost via setattr during extending writes (we
  902. * set inode->i_size at the end of a write. */
  903. if (should_remove_suid(dentry)) {
  904. if (meta_level == 0) {
  905. ocfs2_meta_unlock(inode, meta_level);
  906. meta_level = 1;
  907. continue;
  908. }
  909. ret = ocfs2_write_remove_suid(inode);
  910. if (ret < 0) {
  911. mlog_errno(ret);
  912. goto out_unlock;
  913. }
  914. }
  915. /* work on a copy of ppos until we're sure that we won't have
  916. * to recalculate it due to relocking. */
  917. if (appending) {
  918. saved_pos = i_size_read(inode);
  919. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  920. } else {
  921. saved_pos = *ppos;
  922. }
  923. newsize = count + saved_pos;
  924. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  925. (long long) saved_pos, (long long) newsize,
  926. (long long) i_size_read(inode));
  927. /* No need for a higher level metadata lock if we're
  928. * never going past i_size. */
  929. if (newsize <= i_size_read(inode))
  930. break;
  931. if (meta_level == 0) {
  932. ocfs2_meta_unlock(inode, meta_level);
  933. meta_level = 1;
  934. continue;
  935. }
  936. spin_lock(&OCFS2_I(inode)->ip_lock);
  937. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  938. OCFS2_I(inode)->ip_clusters;
  939. spin_unlock(&OCFS2_I(inode)->ip_lock);
  940. mlog(0, "Writing at EOF, may need more allocation: "
  941. "i_size = %lld, newsize = %lld, need %u clusters\n",
  942. (long long) i_size_read(inode), (long long) newsize,
  943. clusters);
  944. /* We only want to continue the rest of this loop if
  945. * our extend will actually require more
  946. * allocation. */
  947. if (!clusters)
  948. break;
  949. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  950. if (ret < 0) {
  951. if (ret != -ENOSPC)
  952. mlog_errno(ret);
  953. goto out_unlock;
  954. }
  955. break;
  956. }
  957. if (appending)
  958. *ppos = saved_pos;
  959. out_unlock:
  960. ocfs2_meta_unlock(inode, meta_level);
  961. out:
  962. return ret;
  963. }
  964. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  965. const struct iovec *iov,
  966. unsigned long nr_segs,
  967. loff_t pos)
  968. {
  969. int ret, rw_level, have_alloc_sem = 0;
  970. struct file *filp = iocb->ki_filp;
  971. struct inode *inode = filp->f_path.dentry->d_inode;
  972. int appending = filp->f_flags & O_APPEND ? 1 : 0;
  973. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  974. (unsigned int)nr_segs,
  975. filp->f_path.dentry->d_name.len,
  976. filp->f_path.dentry->d_name.name);
  977. /* happy write of zero bytes */
  978. if (iocb->ki_left == 0)
  979. return 0;
  980. mutex_lock(&inode->i_mutex);
  981. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  982. if (filp->f_flags & O_DIRECT) {
  983. have_alloc_sem = 1;
  984. down_read(&inode->i_alloc_sem);
  985. }
  986. /* concurrent O_DIRECT writes are allowed */
  987. rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1;
  988. ret = ocfs2_rw_lock(inode, rw_level);
  989. if (ret < 0) {
  990. rw_level = -1;
  991. mlog_errno(ret);
  992. goto out;
  993. }
  994. ret = ocfs2_prepare_inode_for_write(filp->f_path.dentry, &iocb->ki_pos,
  995. iocb->ki_left, appending);
  996. if (ret < 0) {
  997. mlog_errno(ret);
  998. goto out;
  999. }
  1000. /* communicate with ocfs2_dio_end_io */
  1001. ocfs2_iocb_set_rw_locked(iocb);
  1002. ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos);
  1003. /* buffered aio wouldn't have proper lock coverage today */
  1004. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1005. /*
  1006. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1007. * function pointer which is called when o_direct io completes so that
  1008. * it can unlock our rw lock. (it's the clustered equivalent of
  1009. * i_alloc_sem; protects truncate from racing with pending ios).
  1010. * Unfortunately there are error cases which call end_io and others
  1011. * that don't. so we don't have to unlock the rw_lock if either an
  1012. * async dio is going to do it in the future or an end_io after an
  1013. * error has already done it.
  1014. */
  1015. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1016. rw_level = -1;
  1017. have_alloc_sem = 0;
  1018. }
  1019. out:
  1020. if (have_alloc_sem)
  1021. up_read(&inode->i_alloc_sem);
  1022. if (rw_level != -1)
  1023. ocfs2_rw_unlock(inode, rw_level);
  1024. mutex_unlock(&inode->i_mutex);
  1025. mlog_exit(ret);
  1026. return ret;
  1027. }
  1028. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1029. struct file *out,
  1030. loff_t *ppos,
  1031. size_t len,
  1032. unsigned int flags)
  1033. {
  1034. int ret;
  1035. struct inode *inode = out->f_path.dentry->d_inode;
  1036. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1037. (unsigned int)len,
  1038. out->f_path.dentry->d_name.len,
  1039. out->f_path.dentry->d_name.name);
  1040. inode_double_lock(inode, pipe->inode);
  1041. ret = ocfs2_rw_lock(inode, 1);
  1042. if (ret < 0) {
  1043. mlog_errno(ret);
  1044. goto out;
  1045. }
  1046. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0);
  1047. if (ret < 0) {
  1048. mlog_errno(ret);
  1049. goto out_unlock;
  1050. }
  1051. /* ok, we're done with i_size and alloc work */
  1052. ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
  1053. out_unlock:
  1054. ocfs2_rw_unlock(inode, 1);
  1055. out:
  1056. inode_double_unlock(inode, pipe->inode);
  1057. mlog_exit(ret);
  1058. return ret;
  1059. }
  1060. static ssize_t ocfs2_file_splice_read(struct file *in,
  1061. loff_t *ppos,
  1062. struct pipe_inode_info *pipe,
  1063. size_t len,
  1064. unsigned int flags)
  1065. {
  1066. int ret = 0;
  1067. struct inode *inode = in->f_path.dentry->d_inode;
  1068. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1069. (unsigned int)len,
  1070. in->f_path.dentry->d_name.len,
  1071. in->f_path.dentry->d_name.name);
  1072. /*
  1073. * See the comment in ocfs2_file_aio_read()
  1074. */
  1075. ret = ocfs2_meta_lock(inode, NULL, 0);
  1076. if (ret < 0) {
  1077. mlog_errno(ret);
  1078. goto bail;
  1079. }
  1080. ocfs2_meta_unlock(inode, 0);
  1081. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1082. bail:
  1083. mlog_exit(ret);
  1084. return ret;
  1085. }
  1086. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1087. const struct iovec *iov,
  1088. unsigned long nr_segs,
  1089. loff_t pos)
  1090. {
  1091. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1092. struct file *filp = iocb->ki_filp;
  1093. struct inode *inode = filp->f_path.dentry->d_inode;
  1094. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1095. (unsigned int)nr_segs,
  1096. filp->f_path.dentry->d_name.len,
  1097. filp->f_path.dentry->d_name.name);
  1098. if (!inode) {
  1099. ret = -EINVAL;
  1100. mlog_errno(ret);
  1101. goto bail;
  1102. }
  1103. /*
  1104. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1105. * need locks to protect pending reads from racing with truncate.
  1106. */
  1107. if (filp->f_flags & O_DIRECT) {
  1108. down_read(&inode->i_alloc_sem);
  1109. have_alloc_sem = 1;
  1110. ret = ocfs2_rw_lock(inode, 0);
  1111. if (ret < 0) {
  1112. mlog_errno(ret);
  1113. goto bail;
  1114. }
  1115. rw_level = 0;
  1116. /* communicate with ocfs2_dio_end_io */
  1117. ocfs2_iocb_set_rw_locked(iocb);
  1118. }
  1119. /*
  1120. * We're fine letting folks race truncates and extending
  1121. * writes with read across the cluster, just like they can
  1122. * locally. Hence no rw_lock during read.
  1123. *
  1124. * Take and drop the meta data lock to update inode fields
  1125. * like i_size. This allows the checks down below
  1126. * generic_file_aio_read() a chance of actually working.
  1127. */
  1128. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1129. if (ret < 0) {
  1130. mlog_errno(ret);
  1131. goto bail;
  1132. }
  1133. ocfs2_meta_unlock(inode, lock_level);
  1134. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1135. if (ret == -EINVAL)
  1136. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1137. /* buffered aio wouldn't have proper lock coverage today */
  1138. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1139. /* see ocfs2_file_aio_write */
  1140. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1141. rw_level = -1;
  1142. have_alloc_sem = 0;
  1143. }
  1144. bail:
  1145. if (have_alloc_sem)
  1146. up_read(&inode->i_alloc_sem);
  1147. if (rw_level != -1)
  1148. ocfs2_rw_unlock(inode, rw_level);
  1149. mlog_exit(ret);
  1150. return ret;
  1151. }
  1152. struct inode_operations ocfs2_file_iops = {
  1153. .setattr = ocfs2_setattr,
  1154. .getattr = ocfs2_getattr,
  1155. .permission = ocfs2_permission,
  1156. };
  1157. struct inode_operations ocfs2_special_file_iops = {
  1158. .setattr = ocfs2_setattr,
  1159. .getattr = ocfs2_getattr,
  1160. .permission = ocfs2_permission,
  1161. };
  1162. const struct file_operations ocfs2_fops = {
  1163. .read = do_sync_read,
  1164. .write = do_sync_write,
  1165. .sendfile = generic_file_sendfile,
  1166. .mmap = ocfs2_mmap,
  1167. .fsync = ocfs2_sync_file,
  1168. .release = ocfs2_file_release,
  1169. .open = ocfs2_file_open,
  1170. .aio_read = ocfs2_file_aio_read,
  1171. .aio_write = ocfs2_file_aio_write,
  1172. .ioctl = ocfs2_ioctl,
  1173. .splice_read = ocfs2_file_splice_read,
  1174. .splice_write = ocfs2_file_splice_write,
  1175. };
  1176. const struct file_operations ocfs2_dops = {
  1177. .read = generic_read_dir,
  1178. .readdir = ocfs2_readdir,
  1179. .fsync = ocfs2_sync_file,
  1180. .ioctl = ocfs2_ioctl,
  1181. };