file.c 30 KB

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