file.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #define MLOG_MASK_PREFIX ML_INODE
  37. #include <cluster/masklog.h>
  38. #include "ocfs2.h"
  39. #include "alloc.h"
  40. #include "aops.h"
  41. #include "dir.h"
  42. #include "dlmglue.h"
  43. #include "extent_map.h"
  44. #include "file.h"
  45. #include "sysfile.h"
  46. #include "inode.h"
  47. #include "ioctl.h"
  48. #include "journal.h"
  49. #include "mmap.h"
  50. #include "suballoc.h"
  51. #include "super.h"
  52. #include "buffer_head_io.h"
  53. static int ocfs2_sync_inode(struct inode *inode)
  54. {
  55. filemap_fdatawrite(inode->i_mapping);
  56. return sync_mapping_buffers(inode->i_mapping);
  57. }
  58. static int ocfs2_file_open(struct inode *inode, struct file *file)
  59. {
  60. int status;
  61. int mode = file->f_flags;
  62. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  63. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  64. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  65. spin_lock(&oi->ip_lock);
  66. /* Check that the inode hasn't been wiped from disk by another
  67. * node. If it hasn't then we're safe as long as we hold the
  68. * spin lock until our increment of open count. */
  69. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  70. spin_unlock(&oi->ip_lock);
  71. status = -ENOENT;
  72. goto leave;
  73. }
  74. if (mode & O_DIRECT)
  75. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  76. oi->ip_open_count++;
  77. spin_unlock(&oi->ip_lock);
  78. status = 0;
  79. leave:
  80. mlog_exit(status);
  81. return status;
  82. }
  83. static int ocfs2_file_release(struct inode *inode, struct file *file)
  84. {
  85. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  86. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  87. file->f_path.dentry->d_name.len,
  88. file->f_path.dentry->d_name.name);
  89. spin_lock(&oi->ip_lock);
  90. if (!--oi->ip_open_count)
  91. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  92. spin_unlock(&oi->ip_lock);
  93. mlog_exit(0);
  94. return 0;
  95. }
  96. static int ocfs2_sync_file(struct file *file,
  97. struct dentry *dentry,
  98. int datasync)
  99. {
  100. int err = 0;
  101. journal_t *journal;
  102. struct inode *inode = dentry->d_inode;
  103. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  104. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  105. dentry->d_name.len, dentry->d_name.name);
  106. err = ocfs2_sync_inode(dentry->d_inode);
  107. if (err)
  108. goto bail;
  109. journal = osb->journal->j_journal;
  110. err = journal_force_commit(journal);
  111. bail:
  112. mlog_exit(err);
  113. return (err < 0) ? -EIO : 0;
  114. }
  115. int ocfs2_should_update_atime(struct inode *inode,
  116. struct vfsmount *vfsmnt)
  117. {
  118. struct timespec now;
  119. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  120. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  121. return 0;
  122. if ((inode->i_flags & S_NOATIME) ||
  123. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  124. return 0;
  125. /*
  126. * We can be called with no vfsmnt structure - NFSD will
  127. * sometimes do this.
  128. *
  129. * Note that our action here is different than touch_atime() -
  130. * if we can't tell whether this is a noatime mount, then we
  131. * don't know whether to trust the value of s_atime_quantum.
  132. */
  133. if (vfsmnt == NULL)
  134. return 0;
  135. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  136. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  137. return 0;
  138. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  139. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  140. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  141. return 1;
  142. return 0;
  143. }
  144. now = CURRENT_TIME;
  145. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  146. return 0;
  147. else
  148. return 1;
  149. }
  150. int ocfs2_update_inode_atime(struct inode *inode,
  151. struct buffer_head *bh)
  152. {
  153. int ret;
  154. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  155. handle_t *handle;
  156. mlog_entry_void();
  157. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  158. if (handle == NULL) {
  159. ret = -ENOMEM;
  160. mlog_errno(ret);
  161. goto out;
  162. }
  163. inode->i_atime = CURRENT_TIME;
  164. ret = ocfs2_mark_inode_dirty(handle, inode, bh);
  165. if (ret < 0)
  166. mlog_errno(ret);
  167. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  168. out:
  169. mlog_exit(ret);
  170. return ret;
  171. }
  172. static int ocfs2_set_inode_size(handle_t *handle,
  173. struct inode *inode,
  174. struct buffer_head *fe_bh,
  175. u64 new_i_size)
  176. {
  177. int status;
  178. mlog_entry_void();
  179. i_size_write(inode, new_i_size);
  180. inode->i_blocks = ocfs2_inode_sector_count(inode);
  181. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  182. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  183. if (status < 0) {
  184. mlog_errno(status);
  185. goto bail;
  186. }
  187. bail:
  188. mlog_exit(status);
  189. return status;
  190. }
  191. static int ocfs2_simple_size_update(struct inode *inode,
  192. struct buffer_head *di_bh,
  193. u64 new_i_size)
  194. {
  195. int ret;
  196. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  197. handle_t *handle = NULL;
  198. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  199. if (handle == NULL) {
  200. ret = -ENOMEM;
  201. mlog_errno(ret);
  202. goto out;
  203. }
  204. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  205. new_i_size);
  206. if (ret < 0)
  207. mlog_errno(ret);
  208. ocfs2_commit_trans(osb, handle);
  209. out:
  210. return ret;
  211. }
  212. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  213. struct inode *inode,
  214. struct buffer_head *fe_bh,
  215. u64 new_i_size)
  216. {
  217. int status;
  218. handle_t *handle;
  219. struct ocfs2_dinode *di;
  220. mlog_entry_void();
  221. /* TODO: This needs to actually orphan the inode in this
  222. * transaction. */
  223. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  224. if (IS_ERR(handle)) {
  225. status = PTR_ERR(handle);
  226. mlog_errno(status);
  227. goto out;
  228. }
  229. status = ocfs2_journal_access(handle, inode, fe_bh,
  230. OCFS2_JOURNAL_ACCESS_WRITE);
  231. if (status < 0) {
  232. mlog_errno(status);
  233. goto out_commit;
  234. }
  235. /*
  236. * Do this before setting i_size.
  237. */
  238. status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size);
  239. if (status) {
  240. mlog_errno(status);
  241. goto out_commit;
  242. }
  243. i_size_write(inode, new_i_size);
  244. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  245. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  246. di = (struct ocfs2_dinode *) fe_bh->b_data;
  247. di->i_size = cpu_to_le64(new_i_size);
  248. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  249. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  250. status = ocfs2_journal_dirty(handle, fe_bh);
  251. if (status < 0)
  252. mlog_errno(status);
  253. out_commit:
  254. ocfs2_commit_trans(osb, handle);
  255. out:
  256. mlog_exit(status);
  257. return status;
  258. }
  259. static int ocfs2_truncate_file(struct inode *inode,
  260. struct buffer_head *di_bh,
  261. u64 new_i_size)
  262. {
  263. int status = 0;
  264. struct ocfs2_dinode *fe = NULL;
  265. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  266. struct ocfs2_truncate_context *tc = NULL;
  267. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  268. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  269. (unsigned long long)new_i_size);
  270. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  271. truncate_inode_pages(inode->i_mapping, new_i_size);
  272. fe = (struct ocfs2_dinode *) di_bh->b_data;
  273. if (!OCFS2_IS_VALID_DINODE(fe)) {
  274. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  275. status = -EIO;
  276. goto bail;
  277. }
  278. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  279. "Inode %llu, inode i_size = %lld != di "
  280. "i_size = %llu, i_flags = 0x%x\n",
  281. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  282. i_size_read(inode),
  283. (unsigned long long)le64_to_cpu(fe->i_size),
  284. le32_to_cpu(fe->i_flags));
  285. if (new_i_size > le64_to_cpu(fe->i_size)) {
  286. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  287. (unsigned long long)le64_to_cpu(fe->i_size),
  288. (unsigned long long)new_i_size);
  289. status = -EINVAL;
  290. mlog_errno(status);
  291. goto bail;
  292. }
  293. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  294. (unsigned long long)le64_to_cpu(fe->i_blkno),
  295. (unsigned long long)le64_to_cpu(fe->i_size),
  296. (unsigned long long)new_i_size);
  297. /* lets handle the simple truncate cases before doing any more
  298. * cluster locking. */
  299. if (new_i_size == le64_to_cpu(fe->i_size))
  300. goto bail;
  301. /* This forces other nodes to sync and drop their pages. Do
  302. * this even if we have a truncate without allocation change -
  303. * ocfs2 cluster sizes can be much greater than page size, so
  304. * we have to truncate them anyway. */
  305. status = ocfs2_data_lock(inode, 1);
  306. if (status < 0) {
  307. mlog_errno(status);
  308. goto bail;
  309. }
  310. /* alright, we're going to need to do a full blown alloc size
  311. * change. Orphan the inode so that recovery can complete the
  312. * truncate if necessary. This does the task of marking
  313. * i_size. */
  314. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  315. if (status < 0) {
  316. mlog_errno(status);
  317. goto bail_unlock_data;
  318. }
  319. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  320. if (status < 0) {
  321. mlog_errno(status);
  322. goto bail_unlock_data;
  323. }
  324. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  325. if (status < 0) {
  326. mlog_errno(status);
  327. goto bail_unlock_data;
  328. }
  329. /* TODO: orphan dir cleanup here. */
  330. bail_unlock_data:
  331. ocfs2_data_unlock(inode, 1);
  332. bail:
  333. mlog_exit(status);
  334. return status;
  335. }
  336. /*
  337. * extend allocation only here.
  338. * we'll update all the disk stuff, and oip->alloc_size
  339. *
  340. * expect stuff to be locked, a transaction started and enough data /
  341. * metadata reservations in the contexts.
  342. *
  343. * Will return -EAGAIN, and a reason if a restart is needed.
  344. * If passed in, *reason will always be set, even in error.
  345. */
  346. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  347. struct inode *inode,
  348. u32 *logical_offset,
  349. u32 clusters_to_add,
  350. struct buffer_head *fe_bh,
  351. handle_t *handle,
  352. struct ocfs2_alloc_context *data_ac,
  353. struct ocfs2_alloc_context *meta_ac,
  354. enum ocfs2_alloc_restarted *reason_ret)
  355. {
  356. int status = 0;
  357. int free_extents;
  358. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  359. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  360. u32 bit_off, num_bits;
  361. u64 block;
  362. BUG_ON(!clusters_to_add);
  363. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  364. if (free_extents < 0) {
  365. status = free_extents;
  366. mlog_errno(status);
  367. goto leave;
  368. }
  369. /* there are two cases which could cause us to EAGAIN in the
  370. * we-need-more-metadata case:
  371. * 1) we haven't reserved *any*
  372. * 2) we are so fragmented, we've needed to add metadata too
  373. * many times. */
  374. if (!free_extents && !meta_ac) {
  375. mlog(0, "we haven't reserved any metadata!\n");
  376. status = -EAGAIN;
  377. reason = RESTART_META;
  378. goto leave;
  379. } else if ((!free_extents)
  380. && (ocfs2_alloc_context_bits_left(meta_ac)
  381. < ocfs2_extend_meta_needed(fe))) {
  382. mlog(0, "filesystem is really fragmented...\n");
  383. status = -EAGAIN;
  384. reason = RESTART_META;
  385. goto leave;
  386. }
  387. status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
  388. &bit_off, &num_bits);
  389. if (status < 0) {
  390. if (status != -ENOSPC)
  391. mlog_errno(status);
  392. goto leave;
  393. }
  394. BUG_ON(num_bits > clusters_to_add);
  395. /* reserve our write early -- insert_extent may update the inode */
  396. status = ocfs2_journal_access(handle, inode, fe_bh,
  397. OCFS2_JOURNAL_ACCESS_WRITE);
  398. if (status < 0) {
  399. mlog_errno(status);
  400. goto leave;
  401. }
  402. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  403. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  404. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  405. status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
  406. *logical_offset, block, num_bits,
  407. meta_ac);
  408. if (status < 0) {
  409. mlog_errno(status);
  410. goto leave;
  411. }
  412. status = ocfs2_journal_dirty(handle, fe_bh);
  413. if (status < 0) {
  414. mlog_errno(status);
  415. goto leave;
  416. }
  417. clusters_to_add -= num_bits;
  418. *logical_offset += num_bits;
  419. if (clusters_to_add) {
  420. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  421. "%u\n", fe->i_clusters, clusters_to_add);
  422. status = -EAGAIN;
  423. reason = RESTART_TRANS;
  424. }
  425. leave:
  426. mlog_exit(status);
  427. if (reason_ret)
  428. *reason_ret = reason;
  429. return status;
  430. }
  431. /*
  432. * For a given allocation, determine which allocators will need to be
  433. * accessed, and lock them, reserving the appropriate number of bits.
  434. *
  435. * Called from ocfs2_extend_allocation() for file systems which don't
  436. * support holes, and from ocfs2_write() for file systems which
  437. * understand sparse inodes.
  438. */
  439. int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
  440. u32 clusters_to_add,
  441. struct ocfs2_alloc_context **data_ac,
  442. struct ocfs2_alloc_context **meta_ac)
  443. {
  444. int ret, num_free_extents;
  445. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  446. *meta_ac = NULL;
  447. *data_ac = NULL;
  448. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  449. "clusters_to_add = %u\n",
  450. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  451. le32_to_cpu(di->i_clusters), clusters_to_add);
  452. num_free_extents = ocfs2_num_free_extents(osb, inode, di);
  453. if (num_free_extents < 0) {
  454. ret = num_free_extents;
  455. mlog_errno(ret);
  456. goto out;
  457. }
  458. /*
  459. * Sparse allocation file systems need to be more conservative
  460. * with reserving room for expansion - the actual allocation
  461. * happens while we've got a journal handle open so re-taking
  462. * a cluster lock (because we ran out of room for another
  463. * extent) will violate ordering rules.
  464. *
  465. * Most of the time we'll only be seeing this 1 cluster at a time
  466. * anyway.
  467. */
  468. if (!num_free_extents ||
  469. (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) {
  470. ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
  471. if (ret < 0) {
  472. if (ret != -ENOSPC)
  473. mlog_errno(ret);
  474. goto out;
  475. }
  476. }
  477. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  478. if (ret < 0) {
  479. if (ret != -ENOSPC)
  480. mlog_errno(ret);
  481. goto out;
  482. }
  483. out:
  484. if (ret) {
  485. if (*meta_ac) {
  486. ocfs2_free_alloc_context(*meta_ac);
  487. *meta_ac = NULL;
  488. }
  489. /*
  490. * We cannot have an error and a non null *data_ac.
  491. */
  492. }
  493. return ret;
  494. }
  495. static int ocfs2_extend_allocation(struct inode *inode,
  496. u32 clusters_to_add)
  497. {
  498. int status = 0;
  499. int restart_func = 0;
  500. int drop_alloc_sem = 0;
  501. int credits;
  502. u32 prev_clusters, logical_start;
  503. struct buffer_head *bh = NULL;
  504. struct ocfs2_dinode *fe = NULL;
  505. handle_t *handle = NULL;
  506. struct ocfs2_alloc_context *data_ac = NULL;
  507. struct ocfs2_alloc_context *meta_ac = NULL;
  508. enum ocfs2_alloc_restarted why;
  509. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  510. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  511. /*
  512. * This function only exists for file systems which don't
  513. * support holes.
  514. */
  515. BUG_ON(ocfs2_sparse_alloc(osb));
  516. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  517. OCFS2_BH_CACHED, inode);
  518. if (status < 0) {
  519. mlog_errno(status);
  520. goto leave;
  521. }
  522. fe = (struct ocfs2_dinode *) bh->b_data;
  523. if (!OCFS2_IS_VALID_DINODE(fe)) {
  524. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  525. status = -EIO;
  526. goto leave;
  527. }
  528. logical_start = OCFS2_I(inode)->ip_clusters;
  529. restart_all:
  530. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  531. /* blocks peope in read/write from reading our allocation
  532. * until we're done changing it. We depend on i_mutex to block
  533. * other extend/truncate calls while we're here. Ordering wrt
  534. * start_trans is important here -- always do it before! */
  535. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  536. drop_alloc_sem = 1;
  537. status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac,
  538. &meta_ac);
  539. if (status) {
  540. mlog_errno(status);
  541. goto leave;
  542. }
  543. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  544. handle = ocfs2_start_trans(osb, credits);
  545. if (IS_ERR(handle)) {
  546. status = PTR_ERR(handle);
  547. handle = NULL;
  548. mlog_errno(status);
  549. goto leave;
  550. }
  551. restarted_transaction:
  552. /* reserve a write to the file entry early on - that we if we
  553. * run out of credits in the allocation path, we can still
  554. * update i_size. */
  555. status = ocfs2_journal_access(handle, inode, bh,
  556. OCFS2_JOURNAL_ACCESS_WRITE);
  557. if (status < 0) {
  558. mlog_errno(status);
  559. goto leave;
  560. }
  561. prev_clusters = OCFS2_I(inode)->ip_clusters;
  562. status = ocfs2_do_extend_allocation(osb,
  563. inode,
  564. &logical_start,
  565. clusters_to_add,
  566. bh,
  567. handle,
  568. data_ac,
  569. meta_ac,
  570. &why);
  571. if ((status < 0) && (status != -EAGAIN)) {
  572. if (status != -ENOSPC)
  573. mlog_errno(status);
  574. goto leave;
  575. }
  576. status = ocfs2_journal_dirty(handle, bh);
  577. if (status < 0) {
  578. mlog_errno(status);
  579. goto leave;
  580. }
  581. spin_lock(&OCFS2_I(inode)->ip_lock);
  582. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  583. spin_unlock(&OCFS2_I(inode)->ip_lock);
  584. if (why != RESTART_NONE && clusters_to_add) {
  585. if (why == RESTART_META) {
  586. mlog(0, "restarting function.\n");
  587. restart_func = 1;
  588. } else {
  589. BUG_ON(why != RESTART_TRANS);
  590. mlog(0, "restarting transaction.\n");
  591. /* TODO: This can be more intelligent. */
  592. credits = ocfs2_calc_extend_credits(osb->sb,
  593. fe,
  594. clusters_to_add);
  595. status = ocfs2_extend_trans(handle, credits);
  596. if (status < 0) {
  597. /* handle still has to be committed at
  598. * this point. */
  599. status = -ENOMEM;
  600. mlog_errno(status);
  601. goto leave;
  602. }
  603. goto restarted_transaction;
  604. }
  605. }
  606. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  607. le32_to_cpu(fe->i_clusters),
  608. (unsigned long long)le64_to_cpu(fe->i_size));
  609. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  610. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  611. leave:
  612. if (drop_alloc_sem) {
  613. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  614. drop_alloc_sem = 0;
  615. }
  616. if (handle) {
  617. ocfs2_commit_trans(osb, handle);
  618. handle = NULL;
  619. }
  620. if (data_ac) {
  621. ocfs2_free_alloc_context(data_ac);
  622. data_ac = NULL;
  623. }
  624. if (meta_ac) {
  625. ocfs2_free_alloc_context(meta_ac);
  626. meta_ac = NULL;
  627. }
  628. if ((!status) && restart_func) {
  629. restart_func = 0;
  630. goto restart_all;
  631. }
  632. if (bh) {
  633. brelse(bh);
  634. bh = NULL;
  635. }
  636. mlog_exit(status);
  637. return status;
  638. }
  639. /* Some parts of this taken from generic_cont_expand, which turned out
  640. * to be too fragile to do exactly what we need without us having to
  641. * worry about recursive locking in ->prepare_write() and
  642. * ->commit_write(). */
  643. static int ocfs2_write_zero_page(struct inode *inode,
  644. u64 size)
  645. {
  646. struct address_space *mapping = inode->i_mapping;
  647. struct page *page;
  648. unsigned long index;
  649. unsigned int offset;
  650. handle_t *handle = NULL;
  651. int ret;
  652. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  653. /* ugh. in prepare/commit_write, if from==to==start of block, we
  654. ** skip the prepare. make sure we never send an offset for the start
  655. ** of a block
  656. */
  657. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  658. offset++;
  659. }
  660. index = size >> PAGE_CACHE_SHIFT;
  661. page = grab_cache_page(mapping, index);
  662. if (!page) {
  663. ret = -ENOMEM;
  664. mlog_errno(ret);
  665. goto out;
  666. }
  667. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  668. if (ret < 0) {
  669. mlog_errno(ret);
  670. goto out_unlock;
  671. }
  672. if (ocfs2_should_order_data(inode)) {
  673. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  674. offset);
  675. if (IS_ERR(handle)) {
  676. ret = PTR_ERR(handle);
  677. handle = NULL;
  678. goto out_unlock;
  679. }
  680. }
  681. /* must not update i_size! */
  682. ret = block_commit_write(page, offset, offset);
  683. if (ret < 0)
  684. mlog_errno(ret);
  685. else
  686. ret = 0;
  687. if (handle)
  688. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  689. out_unlock:
  690. unlock_page(page);
  691. page_cache_release(page);
  692. out:
  693. return ret;
  694. }
  695. static int ocfs2_zero_extend(struct inode *inode,
  696. u64 zero_to_size)
  697. {
  698. int ret = 0;
  699. u64 start_off;
  700. struct super_block *sb = inode->i_sb;
  701. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  702. while (start_off < zero_to_size) {
  703. ret = ocfs2_write_zero_page(inode, start_off);
  704. if (ret < 0) {
  705. mlog_errno(ret);
  706. goto out;
  707. }
  708. start_off += sb->s_blocksize;
  709. /*
  710. * Very large extends have the potential to lock up
  711. * the cpu for extended periods of time.
  712. */
  713. cond_resched();
  714. }
  715. out:
  716. return ret;
  717. }
  718. /*
  719. * A tail_to_skip value > 0 indicates that we're being called from
  720. * ocfs2_file_aio_write(). This has the following implications:
  721. *
  722. * - we don't want to update i_size
  723. * - di_bh will be NULL, which is fine because it's only used in the
  724. * case where we want to update i_size.
  725. * - ocfs2_zero_extend() will then only be filling the hole created
  726. * between i_size and the start of the write.
  727. */
  728. static int ocfs2_extend_file(struct inode *inode,
  729. struct buffer_head *di_bh,
  730. u64 new_i_size,
  731. size_t tail_to_skip)
  732. {
  733. int ret = 0;
  734. u32 clusters_to_add = 0;
  735. BUG_ON(!tail_to_skip && !di_bh);
  736. /* setattr sometimes calls us like this. */
  737. if (new_i_size == 0)
  738. goto out;
  739. if (i_size_read(inode) == new_i_size)
  740. goto out;
  741. BUG_ON(new_i_size < i_size_read(inode));
  742. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  743. BUG_ON(tail_to_skip != 0);
  744. goto out_update_size;
  745. }
  746. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  747. OCFS2_I(inode)->ip_clusters;
  748. /*
  749. * protect the pages that ocfs2_zero_extend is going to be
  750. * pulling into the page cache.. we do this before the
  751. * metadata extend so that we don't get into the situation
  752. * where we've extended the metadata but can't get the data
  753. * lock to zero.
  754. */
  755. ret = ocfs2_data_lock(inode, 1);
  756. if (ret < 0) {
  757. mlog_errno(ret);
  758. goto out;
  759. }
  760. if (clusters_to_add) {
  761. ret = ocfs2_extend_allocation(inode, clusters_to_add);
  762. if (ret < 0) {
  763. mlog_errno(ret);
  764. goto out_unlock;
  765. }
  766. }
  767. /*
  768. * Call this even if we don't add any clusters to the tree. We
  769. * still need to zero the area between the old i_size and the
  770. * new i_size.
  771. */
  772. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  773. if (ret < 0) {
  774. mlog_errno(ret);
  775. goto out_unlock;
  776. }
  777. out_update_size:
  778. if (!tail_to_skip) {
  779. /* We're being called from ocfs2_setattr() which wants
  780. * us to update i_size */
  781. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  782. if (ret < 0)
  783. mlog_errno(ret);
  784. }
  785. out_unlock:
  786. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  787. ocfs2_data_unlock(inode, 1);
  788. out:
  789. return ret;
  790. }
  791. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  792. {
  793. int status = 0, size_change;
  794. struct inode *inode = dentry->d_inode;
  795. struct super_block *sb = inode->i_sb;
  796. struct ocfs2_super *osb = OCFS2_SB(sb);
  797. struct buffer_head *bh = NULL;
  798. handle_t *handle = NULL;
  799. mlog_entry("(0x%p, '%.*s')\n", dentry,
  800. dentry->d_name.len, dentry->d_name.name);
  801. if (attr->ia_valid & ATTR_MODE)
  802. mlog(0, "mode change: %d\n", attr->ia_mode);
  803. if (attr->ia_valid & ATTR_UID)
  804. mlog(0, "uid change: %d\n", attr->ia_uid);
  805. if (attr->ia_valid & ATTR_GID)
  806. mlog(0, "gid change: %d\n", attr->ia_gid);
  807. if (attr->ia_valid & ATTR_SIZE)
  808. mlog(0, "size change...\n");
  809. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  810. mlog(0, "time change...\n");
  811. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  812. | ATTR_GID | ATTR_UID | ATTR_MODE)
  813. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  814. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  815. return 0;
  816. }
  817. status = inode_change_ok(inode, attr);
  818. if (status)
  819. return status;
  820. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  821. if (size_change) {
  822. status = ocfs2_rw_lock(inode, 1);
  823. if (status < 0) {
  824. mlog_errno(status);
  825. goto bail;
  826. }
  827. }
  828. status = ocfs2_meta_lock(inode, &bh, 1);
  829. if (status < 0) {
  830. if (status != -ENOENT)
  831. mlog_errno(status);
  832. goto bail_unlock_rw;
  833. }
  834. if (size_change && attr->ia_size != i_size_read(inode)) {
  835. if (i_size_read(inode) > attr->ia_size)
  836. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  837. else
  838. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  839. if (status < 0) {
  840. if (status != -ENOSPC)
  841. mlog_errno(status);
  842. status = -ENOSPC;
  843. goto bail_unlock;
  844. }
  845. }
  846. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  847. if (IS_ERR(handle)) {
  848. status = PTR_ERR(handle);
  849. mlog_errno(status);
  850. goto bail_unlock;
  851. }
  852. status = inode_setattr(inode, attr);
  853. if (status < 0) {
  854. mlog_errno(status);
  855. goto bail_commit;
  856. }
  857. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  858. if (status < 0)
  859. mlog_errno(status);
  860. bail_commit:
  861. ocfs2_commit_trans(osb, handle);
  862. bail_unlock:
  863. ocfs2_meta_unlock(inode, 1);
  864. bail_unlock_rw:
  865. if (size_change)
  866. ocfs2_rw_unlock(inode, 1);
  867. bail:
  868. if (bh)
  869. brelse(bh);
  870. mlog_exit(status);
  871. return status;
  872. }
  873. int ocfs2_getattr(struct vfsmount *mnt,
  874. struct dentry *dentry,
  875. struct kstat *stat)
  876. {
  877. struct inode *inode = dentry->d_inode;
  878. struct super_block *sb = dentry->d_inode->i_sb;
  879. struct ocfs2_super *osb = sb->s_fs_info;
  880. int err;
  881. mlog_entry_void();
  882. err = ocfs2_inode_revalidate(dentry);
  883. if (err) {
  884. if (err != -ENOENT)
  885. mlog_errno(err);
  886. goto bail;
  887. }
  888. generic_fillattr(inode, stat);
  889. /* We set the blksize from the cluster size for performance */
  890. stat->blksize = osb->s_clustersize;
  891. bail:
  892. mlog_exit(err);
  893. return err;
  894. }
  895. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  896. {
  897. int ret;
  898. mlog_entry_void();
  899. ret = ocfs2_meta_lock(inode, NULL, 0);
  900. if (ret) {
  901. if (ret != -ENOENT)
  902. mlog_errno(ret);
  903. goto out;
  904. }
  905. ret = generic_permission(inode, mask, NULL);
  906. ocfs2_meta_unlock(inode, 0);
  907. out:
  908. mlog_exit(ret);
  909. return ret;
  910. }
  911. static int ocfs2_write_remove_suid(struct inode *inode)
  912. {
  913. int ret;
  914. struct buffer_head *bh = NULL;
  915. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  916. handle_t *handle;
  917. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  918. struct ocfs2_dinode *di;
  919. mlog_entry("(Inode %llu, mode 0%o)\n",
  920. (unsigned long long)oi->ip_blkno, inode->i_mode);
  921. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  922. if (handle == NULL) {
  923. ret = -ENOMEM;
  924. mlog_errno(ret);
  925. goto out;
  926. }
  927. ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  928. if (ret < 0) {
  929. mlog_errno(ret);
  930. goto out_trans;
  931. }
  932. ret = ocfs2_journal_access(handle, inode, bh,
  933. OCFS2_JOURNAL_ACCESS_WRITE);
  934. if (ret < 0) {
  935. mlog_errno(ret);
  936. goto out_bh;
  937. }
  938. inode->i_mode &= ~S_ISUID;
  939. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  940. inode->i_mode &= ~S_ISGID;
  941. di = (struct ocfs2_dinode *) bh->b_data;
  942. di->i_mode = cpu_to_le16(inode->i_mode);
  943. ret = ocfs2_journal_dirty(handle, bh);
  944. if (ret < 0)
  945. mlog_errno(ret);
  946. out_bh:
  947. brelse(bh);
  948. out_trans:
  949. ocfs2_commit_trans(osb, handle);
  950. out:
  951. mlog_exit(ret);
  952. return ret;
  953. }
  954. /*
  955. * Will look for holes and unwritten extents in the range starting at
  956. * pos for count bytes (inclusive).
  957. */
  958. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  959. size_t count)
  960. {
  961. int ret = 0;
  962. unsigned int extent_flags;
  963. u32 cpos, clusters, extent_len, phys_cpos;
  964. struct super_block *sb = inode->i_sb;
  965. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  966. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  967. while (clusters) {
  968. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  969. &extent_flags);
  970. if (ret < 0) {
  971. mlog_errno(ret);
  972. goto out;
  973. }
  974. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  975. ret = 1;
  976. break;
  977. }
  978. if (extent_len > clusters)
  979. extent_len = clusters;
  980. clusters -= extent_len;
  981. cpos += extent_len;
  982. }
  983. out:
  984. return ret;
  985. }
  986. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  987. loff_t *ppos,
  988. size_t count,
  989. int appending,
  990. int *direct_io)
  991. {
  992. int ret = 0, meta_level = appending;
  993. struct inode *inode = dentry->d_inode;
  994. u32 clusters;
  995. loff_t newsize, saved_pos;
  996. /*
  997. * We sample i_size under a read level meta lock to see if our write
  998. * is extending the file, if it is we back off and get a write level
  999. * meta lock.
  1000. */
  1001. for(;;) {
  1002. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  1003. if (ret < 0) {
  1004. meta_level = -1;
  1005. mlog_errno(ret);
  1006. goto out;
  1007. }
  1008. /* Clear suid / sgid if necessary. We do this here
  1009. * instead of later in the write path because
  1010. * remove_suid() calls ->setattr without any hint that
  1011. * we may have already done our cluster locking. Since
  1012. * ocfs2_setattr() *must* take cluster locks to
  1013. * proceeed, this will lead us to recursively lock the
  1014. * inode. There's also the dinode i_size state which
  1015. * can be lost via setattr during extending writes (we
  1016. * set inode->i_size at the end of a write. */
  1017. if (should_remove_suid(dentry)) {
  1018. if (meta_level == 0) {
  1019. ocfs2_meta_unlock(inode, meta_level);
  1020. meta_level = 1;
  1021. continue;
  1022. }
  1023. ret = ocfs2_write_remove_suid(inode);
  1024. if (ret < 0) {
  1025. mlog_errno(ret);
  1026. goto out_unlock;
  1027. }
  1028. }
  1029. /* work on a copy of ppos until we're sure that we won't have
  1030. * to recalculate it due to relocking. */
  1031. if (appending) {
  1032. saved_pos = i_size_read(inode);
  1033. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1034. } else {
  1035. saved_pos = *ppos;
  1036. }
  1037. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  1038. loff_t end = saved_pos + count;
  1039. /*
  1040. * Skip the O_DIRECT checks if we don't need
  1041. * them.
  1042. */
  1043. if (!direct_io || !(*direct_io))
  1044. break;
  1045. /*
  1046. * Allowing concurrent direct writes means
  1047. * i_size changes wouldn't be synchronized, so
  1048. * one node could wind up truncating another
  1049. * nodes writes.
  1050. */
  1051. if (end > i_size_read(inode)) {
  1052. *direct_io = 0;
  1053. break;
  1054. }
  1055. /*
  1056. * We don't fill holes during direct io, so
  1057. * check for them here. If any are found, the
  1058. * caller will have to retake some cluster
  1059. * locks and initiate the io as buffered.
  1060. */
  1061. ret = ocfs2_check_range_for_holes(inode, saved_pos,
  1062. count);
  1063. if (ret == 1) {
  1064. *direct_io = 0;
  1065. ret = 0;
  1066. } else if (ret < 0)
  1067. mlog_errno(ret);
  1068. break;
  1069. }
  1070. /*
  1071. * The rest of this loop is concerned with legacy file
  1072. * systems which don't support sparse files.
  1073. */
  1074. newsize = count + saved_pos;
  1075. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  1076. (long long) saved_pos, (long long) newsize,
  1077. (long long) i_size_read(inode));
  1078. /* No need for a higher level metadata lock if we're
  1079. * never going past i_size. */
  1080. if (newsize <= i_size_read(inode))
  1081. break;
  1082. if (meta_level == 0) {
  1083. ocfs2_meta_unlock(inode, meta_level);
  1084. meta_level = 1;
  1085. continue;
  1086. }
  1087. spin_lock(&OCFS2_I(inode)->ip_lock);
  1088. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  1089. OCFS2_I(inode)->ip_clusters;
  1090. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1091. mlog(0, "Writing at EOF, may need more allocation: "
  1092. "i_size = %lld, newsize = %lld, need %u clusters\n",
  1093. (long long) i_size_read(inode), (long long) newsize,
  1094. clusters);
  1095. /* We only want to continue the rest of this loop if
  1096. * our extend will actually require more
  1097. * allocation. */
  1098. if (!clusters)
  1099. break;
  1100. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  1101. if (ret < 0) {
  1102. if (ret != -ENOSPC)
  1103. mlog_errno(ret);
  1104. goto out_unlock;
  1105. }
  1106. break;
  1107. }
  1108. if (appending)
  1109. *ppos = saved_pos;
  1110. out_unlock:
  1111. ocfs2_meta_unlock(inode, meta_level);
  1112. out:
  1113. return ret;
  1114. }
  1115. static inline void
  1116. ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
  1117. {
  1118. const struct iovec *iov = *iovp;
  1119. size_t base = *basep;
  1120. do {
  1121. int copy = min(bytes, iov->iov_len - base);
  1122. bytes -= copy;
  1123. base += copy;
  1124. if (iov->iov_len == base) {
  1125. iov++;
  1126. base = 0;
  1127. }
  1128. } while (bytes);
  1129. *iovp = iov;
  1130. *basep = base;
  1131. }
  1132. static struct page * ocfs2_get_write_source(struct ocfs2_buffered_write_priv *bp,
  1133. const struct iovec *cur_iov,
  1134. size_t iov_offset)
  1135. {
  1136. int ret;
  1137. char *buf;
  1138. struct page *src_page = NULL;
  1139. buf = cur_iov->iov_base + iov_offset;
  1140. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1141. /*
  1142. * Pull in the user page. We want to do this outside
  1143. * of the meta data locks in order to preserve locking
  1144. * order in case of page fault.
  1145. */
  1146. ret = get_user_pages(current, current->mm,
  1147. (unsigned long)buf & PAGE_CACHE_MASK, 1,
  1148. 0, 0, &src_page, NULL);
  1149. if (ret == 1)
  1150. bp->b_src_buf = kmap(src_page);
  1151. else
  1152. src_page = ERR_PTR(-EFAULT);
  1153. } else {
  1154. bp->b_src_buf = buf;
  1155. }
  1156. return src_page;
  1157. }
  1158. static void ocfs2_put_write_source(struct ocfs2_buffered_write_priv *bp,
  1159. struct page *page)
  1160. {
  1161. if (page) {
  1162. kunmap(page);
  1163. page_cache_release(page);
  1164. }
  1165. }
  1166. static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
  1167. const struct iovec *iov,
  1168. unsigned long nr_segs,
  1169. size_t count,
  1170. ssize_t o_direct_written)
  1171. {
  1172. int ret = 0;
  1173. ssize_t copied, total = 0;
  1174. size_t iov_offset = 0;
  1175. const struct iovec *cur_iov = iov;
  1176. struct ocfs2_buffered_write_priv bp;
  1177. struct page *page;
  1178. /*
  1179. * handle partial DIO write. Adjust cur_iov if needed.
  1180. */
  1181. ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
  1182. do {
  1183. bp.b_cur_off = iov_offset;
  1184. bp.b_cur_iov = cur_iov;
  1185. page = ocfs2_get_write_source(&bp, cur_iov, iov_offset);
  1186. if (IS_ERR(page)) {
  1187. ret = PTR_ERR(page);
  1188. goto out;
  1189. }
  1190. copied = ocfs2_buffered_write_cluster(file, *ppos, count,
  1191. ocfs2_map_and_write_user_data,
  1192. &bp);
  1193. ocfs2_put_write_source(&bp, page);
  1194. if (copied < 0) {
  1195. mlog_errno(copied);
  1196. ret = copied;
  1197. goto out;
  1198. }
  1199. total += copied;
  1200. *ppos = *ppos + copied;
  1201. count -= copied;
  1202. ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
  1203. } while(count);
  1204. out:
  1205. return total ? total : ret;
  1206. }
  1207. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1208. const struct iovec *iov,
  1209. unsigned long nr_segs,
  1210. loff_t pos)
  1211. {
  1212. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1213. int can_do_direct, sync = 0;
  1214. ssize_t written = 0;
  1215. size_t ocount; /* original count */
  1216. size_t count; /* after file limit checks */
  1217. loff_t *ppos = &iocb->ki_pos;
  1218. struct file *file = iocb->ki_filp;
  1219. struct inode *inode = file->f_path.dentry->d_inode;
  1220. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1221. (unsigned int)nr_segs,
  1222. file->f_path.dentry->d_name.len,
  1223. file->f_path.dentry->d_name.name);
  1224. if (iocb->ki_left == 0)
  1225. return 0;
  1226. ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1227. if (ret)
  1228. return ret;
  1229. count = ocount;
  1230. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1231. appending = file->f_flags & O_APPEND ? 1 : 0;
  1232. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1233. mutex_lock(&inode->i_mutex);
  1234. relock:
  1235. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1236. if (direct_io) {
  1237. down_read(&inode->i_alloc_sem);
  1238. have_alloc_sem = 1;
  1239. }
  1240. /* concurrent O_DIRECT writes are allowed */
  1241. rw_level = !direct_io;
  1242. ret = ocfs2_rw_lock(inode, rw_level);
  1243. if (ret < 0) {
  1244. mlog_errno(ret);
  1245. goto out_sems;
  1246. }
  1247. can_do_direct = direct_io;
  1248. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1249. iocb->ki_left, appending,
  1250. &can_do_direct);
  1251. if (ret < 0) {
  1252. mlog_errno(ret);
  1253. goto out;
  1254. }
  1255. /*
  1256. * We can't complete the direct I/O as requested, fall back to
  1257. * buffered I/O.
  1258. */
  1259. if (direct_io && !can_do_direct) {
  1260. ocfs2_rw_unlock(inode, rw_level);
  1261. up_read(&inode->i_alloc_sem);
  1262. have_alloc_sem = 0;
  1263. rw_level = -1;
  1264. direct_io = 0;
  1265. sync = 1;
  1266. goto relock;
  1267. }
  1268. if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
  1269. sync = 1;
  1270. /*
  1271. * XXX: Is it ok to execute these checks a second time?
  1272. */
  1273. ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
  1274. if (ret)
  1275. goto out;
  1276. /*
  1277. * Set pos so that sync_page_range_nolock() below understands
  1278. * where to start from. We might've moved it around via the
  1279. * calls above. The range we want to actually sync starts from
  1280. * *ppos here.
  1281. *
  1282. */
  1283. pos = *ppos;
  1284. /* communicate with ocfs2_dio_end_io */
  1285. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1286. if (direct_io) {
  1287. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1288. ppos, count, ocount);
  1289. if (written < 0) {
  1290. ret = written;
  1291. goto out_dio;
  1292. }
  1293. } else {
  1294. written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
  1295. count, written);
  1296. if (written < 0) {
  1297. ret = written;
  1298. if (ret != -EFAULT || ret != -ENOSPC)
  1299. mlog_errno(ret);
  1300. goto out;
  1301. }
  1302. }
  1303. out_dio:
  1304. /* buffered aio wouldn't have proper lock coverage today */
  1305. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1306. /*
  1307. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1308. * function pointer which is called when o_direct io completes so that
  1309. * it can unlock our rw lock. (it's the clustered equivalent of
  1310. * i_alloc_sem; protects truncate from racing with pending ios).
  1311. * Unfortunately there are error cases which call end_io and others
  1312. * that don't. so we don't have to unlock the rw_lock if either an
  1313. * async dio is going to do it in the future or an end_io after an
  1314. * error has already done it.
  1315. */
  1316. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1317. rw_level = -1;
  1318. have_alloc_sem = 0;
  1319. }
  1320. out:
  1321. if (rw_level != -1)
  1322. ocfs2_rw_unlock(inode, rw_level);
  1323. out_sems:
  1324. if (have_alloc_sem)
  1325. up_read(&inode->i_alloc_sem);
  1326. if (written > 0 && sync) {
  1327. ssize_t err;
  1328. err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
  1329. if (err < 0)
  1330. written = err;
  1331. }
  1332. mutex_unlock(&inode->i_mutex);
  1333. mlog_exit(ret);
  1334. return written ? written : ret;
  1335. }
  1336. static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
  1337. struct pipe_buffer *buf,
  1338. struct splice_desc *sd)
  1339. {
  1340. int ret, count, total = 0;
  1341. ssize_t copied = 0;
  1342. struct ocfs2_splice_write_priv sp;
  1343. ret = buf->ops->confirm(pipe, buf);
  1344. if (ret)
  1345. goto out;
  1346. sp.s_sd = sd;
  1347. sp.s_buf = buf;
  1348. sp.s_pipe = pipe;
  1349. sp.s_offset = sd->pos & ~PAGE_CACHE_MASK;
  1350. sp.s_buf_offset = buf->offset;
  1351. count = sd->len;
  1352. if (count + sp.s_offset > PAGE_CACHE_SIZE)
  1353. count = PAGE_CACHE_SIZE - sp.s_offset;
  1354. do {
  1355. /*
  1356. * splice wants us to copy up to one page at a
  1357. * time. For pagesize > cluster size, this means we
  1358. * might enter ocfs2_buffered_write_cluster() more
  1359. * than once, so keep track of our progress here.
  1360. */
  1361. copied = ocfs2_buffered_write_cluster(sd->u.file,
  1362. (loff_t)sd->pos + total,
  1363. count,
  1364. ocfs2_map_and_write_splice_data,
  1365. &sp);
  1366. if (copied < 0) {
  1367. mlog_errno(copied);
  1368. ret = copied;
  1369. goto out;
  1370. }
  1371. count -= copied;
  1372. sp.s_offset += copied;
  1373. sp.s_buf_offset += copied;
  1374. total += copied;
  1375. } while (count);
  1376. ret = 0;
  1377. out:
  1378. return total ? total : ret;
  1379. }
  1380. static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1381. struct file *out,
  1382. loff_t *ppos,
  1383. size_t len,
  1384. unsigned int flags)
  1385. {
  1386. int ret, err;
  1387. struct address_space *mapping = out->f_mapping;
  1388. struct inode *inode = mapping->host;
  1389. struct splice_desc sd = {
  1390. .total_len = len,
  1391. .flags = flags,
  1392. .pos = *ppos,
  1393. .u.file = out,
  1394. };
  1395. ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
  1396. if (ret > 0) {
  1397. *ppos += ret;
  1398. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1399. err = generic_osync_inode(inode, mapping,
  1400. OSYNC_METADATA|OSYNC_DATA);
  1401. if (err)
  1402. ret = err;
  1403. }
  1404. }
  1405. return ret;
  1406. }
  1407. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1408. struct file *out,
  1409. loff_t *ppos,
  1410. size_t len,
  1411. unsigned int flags)
  1412. {
  1413. int ret;
  1414. struct inode *inode = out->f_path.dentry->d_inode;
  1415. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1416. (unsigned int)len,
  1417. out->f_path.dentry->d_name.len,
  1418. out->f_path.dentry->d_name.name);
  1419. inode_double_lock(inode, pipe->inode);
  1420. ret = ocfs2_rw_lock(inode, 1);
  1421. if (ret < 0) {
  1422. mlog_errno(ret);
  1423. goto out;
  1424. }
  1425. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1426. NULL);
  1427. if (ret < 0) {
  1428. mlog_errno(ret);
  1429. goto out_unlock;
  1430. }
  1431. /* ok, we're done with i_size and alloc work */
  1432. ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
  1433. out_unlock:
  1434. ocfs2_rw_unlock(inode, 1);
  1435. out:
  1436. inode_double_unlock(inode, pipe->inode);
  1437. mlog_exit(ret);
  1438. return ret;
  1439. }
  1440. static ssize_t ocfs2_file_splice_read(struct file *in,
  1441. loff_t *ppos,
  1442. struct pipe_inode_info *pipe,
  1443. size_t len,
  1444. unsigned int flags)
  1445. {
  1446. int ret = 0;
  1447. struct inode *inode = in->f_path.dentry->d_inode;
  1448. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1449. (unsigned int)len,
  1450. in->f_path.dentry->d_name.len,
  1451. in->f_path.dentry->d_name.name);
  1452. /*
  1453. * See the comment in ocfs2_file_aio_read()
  1454. */
  1455. ret = ocfs2_meta_lock(inode, NULL, 0);
  1456. if (ret < 0) {
  1457. mlog_errno(ret);
  1458. goto bail;
  1459. }
  1460. ocfs2_meta_unlock(inode, 0);
  1461. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1462. bail:
  1463. mlog_exit(ret);
  1464. return ret;
  1465. }
  1466. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1467. const struct iovec *iov,
  1468. unsigned long nr_segs,
  1469. loff_t pos)
  1470. {
  1471. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1472. struct file *filp = iocb->ki_filp;
  1473. struct inode *inode = filp->f_path.dentry->d_inode;
  1474. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1475. (unsigned int)nr_segs,
  1476. filp->f_path.dentry->d_name.len,
  1477. filp->f_path.dentry->d_name.name);
  1478. if (!inode) {
  1479. ret = -EINVAL;
  1480. mlog_errno(ret);
  1481. goto bail;
  1482. }
  1483. /*
  1484. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1485. * need locks to protect pending reads from racing with truncate.
  1486. */
  1487. if (filp->f_flags & O_DIRECT) {
  1488. down_read(&inode->i_alloc_sem);
  1489. have_alloc_sem = 1;
  1490. ret = ocfs2_rw_lock(inode, 0);
  1491. if (ret < 0) {
  1492. mlog_errno(ret);
  1493. goto bail;
  1494. }
  1495. rw_level = 0;
  1496. /* communicate with ocfs2_dio_end_io */
  1497. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1498. }
  1499. /*
  1500. * We're fine letting folks race truncates and extending
  1501. * writes with read across the cluster, just like they can
  1502. * locally. Hence no rw_lock during read.
  1503. *
  1504. * Take and drop the meta data lock to update inode fields
  1505. * like i_size. This allows the checks down below
  1506. * generic_file_aio_read() a chance of actually working.
  1507. */
  1508. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1509. if (ret < 0) {
  1510. mlog_errno(ret);
  1511. goto bail;
  1512. }
  1513. ocfs2_meta_unlock(inode, lock_level);
  1514. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1515. if (ret == -EINVAL)
  1516. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1517. /* buffered aio wouldn't have proper lock coverage today */
  1518. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1519. /* see ocfs2_file_aio_write */
  1520. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1521. rw_level = -1;
  1522. have_alloc_sem = 0;
  1523. }
  1524. bail:
  1525. if (have_alloc_sem)
  1526. up_read(&inode->i_alloc_sem);
  1527. if (rw_level != -1)
  1528. ocfs2_rw_unlock(inode, rw_level);
  1529. mlog_exit(ret);
  1530. return ret;
  1531. }
  1532. const struct inode_operations ocfs2_file_iops = {
  1533. .setattr = ocfs2_setattr,
  1534. .getattr = ocfs2_getattr,
  1535. .permission = ocfs2_permission,
  1536. };
  1537. const struct inode_operations ocfs2_special_file_iops = {
  1538. .setattr = ocfs2_setattr,
  1539. .getattr = ocfs2_getattr,
  1540. .permission = ocfs2_permission,
  1541. };
  1542. const struct file_operations ocfs2_fops = {
  1543. .read = do_sync_read,
  1544. .write = do_sync_write,
  1545. .mmap = ocfs2_mmap,
  1546. .fsync = ocfs2_sync_file,
  1547. .release = ocfs2_file_release,
  1548. .open = ocfs2_file_open,
  1549. .aio_read = ocfs2_file_aio_read,
  1550. .aio_write = ocfs2_file_aio_write,
  1551. .ioctl = ocfs2_ioctl,
  1552. #ifdef CONFIG_COMPAT
  1553. .compat_ioctl = ocfs2_compat_ioctl,
  1554. #endif
  1555. .splice_read = ocfs2_file_splice_read,
  1556. .splice_write = ocfs2_file_splice_write,
  1557. };
  1558. const struct file_operations ocfs2_dops = {
  1559. .read = generic_read_dir,
  1560. .readdir = ocfs2_readdir,
  1561. .fsync = ocfs2_sync_file,
  1562. .ioctl = ocfs2_ioctl,
  1563. #ifdef CONFIG_COMPAT
  1564. .compat_ioctl = ocfs2_compat_ioctl,
  1565. #endif
  1566. };