file.c 31 KB

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