file.c 44 KB

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