file.c 30 KB

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