file.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  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. u64 cluster_bytes;
  221. mlog_entry_void();
  222. /* TODO: This needs to actually orphan the inode in this
  223. * transaction. */
  224. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  225. if (IS_ERR(handle)) {
  226. status = PTR_ERR(handle);
  227. mlog_errno(status);
  228. goto out;
  229. }
  230. status = ocfs2_journal_access(handle, inode, fe_bh,
  231. OCFS2_JOURNAL_ACCESS_WRITE);
  232. if (status < 0) {
  233. mlog_errno(status);
  234. goto out_commit;
  235. }
  236. /*
  237. * Do this before setting i_size.
  238. */
  239. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  240. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  241. cluster_bytes);
  242. if (status) {
  243. mlog_errno(status);
  244. goto out_commit;
  245. }
  246. i_size_write(inode, new_i_size);
  247. inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size);
  248. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  249. di = (struct ocfs2_dinode *) fe_bh->b_data;
  250. di->i_size = cpu_to_le64(new_i_size);
  251. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  252. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  253. status = ocfs2_journal_dirty(handle, fe_bh);
  254. if (status < 0)
  255. mlog_errno(status);
  256. out_commit:
  257. ocfs2_commit_trans(osb, handle);
  258. out:
  259. mlog_exit(status);
  260. return status;
  261. }
  262. static int ocfs2_truncate_file(struct inode *inode,
  263. struct buffer_head *di_bh,
  264. u64 new_i_size)
  265. {
  266. int status = 0;
  267. struct ocfs2_dinode *fe = NULL;
  268. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  269. struct ocfs2_truncate_context *tc = NULL;
  270. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  271. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  272. (unsigned long long)new_i_size);
  273. fe = (struct ocfs2_dinode *) di_bh->b_data;
  274. if (!OCFS2_IS_VALID_DINODE(fe)) {
  275. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  276. status = -EIO;
  277. goto bail;
  278. }
  279. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  280. "Inode %llu, inode i_size = %lld != di "
  281. "i_size = %llu, i_flags = 0x%x\n",
  282. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  283. i_size_read(inode),
  284. (unsigned long long)le64_to_cpu(fe->i_size),
  285. le32_to_cpu(fe->i_flags));
  286. if (new_i_size > le64_to_cpu(fe->i_size)) {
  287. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  288. (unsigned long long)le64_to_cpu(fe->i_size),
  289. (unsigned long long)new_i_size);
  290. status = -EINVAL;
  291. mlog_errno(status);
  292. goto bail;
  293. }
  294. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  295. (unsigned long long)le64_to_cpu(fe->i_blkno),
  296. (unsigned long long)le64_to_cpu(fe->i_size),
  297. (unsigned long long)new_i_size);
  298. /* lets handle the simple truncate cases before doing any more
  299. * cluster locking. */
  300. if (new_i_size == le64_to_cpu(fe->i_size))
  301. goto bail;
  302. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  303. /* This forces other nodes to sync and drop their pages. Do
  304. * this even if we have a truncate without allocation change -
  305. * ocfs2 cluster sizes can be much greater than page size, so
  306. * we have to truncate them anyway. */
  307. status = ocfs2_data_lock(inode, 1);
  308. if (status < 0) {
  309. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  310. mlog_errno(status);
  311. goto bail;
  312. }
  313. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  314. truncate_inode_pages(inode->i_mapping, new_i_size);
  315. /* alright, we're going to need to do a full blown alloc size
  316. * change. Orphan the inode so that recovery can complete the
  317. * truncate if necessary. This does the task of marking
  318. * i_size. */
  319. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  320. if (status < 0) {
  321. mlog_errno(status);
  322. goto bail_unlock_data;
  323. }
  324. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  325. if (status < 0) {
  326. mlog_errno(status);
  327. goto bail_unlock_data;
  328. }
  329. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  330. if (status < 0) {
  331. mlog_errno(status);
  332. goto bail_unlock_data;
  333. }
  334. /* TODO: orphan dir cleanup here. */
  335. bail_unlock_data:
  336. ocfs2_data_unlock(inode, 1);
  337. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  338. bail:
  339. mlog_exit(status);
  340. return status;
  341. }
  342. /*
  343. * extend allocation only here.
  344. * we'll update all the disk stuff, and oip->alloc_size
  345. *
  346. * expect stuff to be locked, a transaction started and enough data /
  347. * metadata reservations in the contexts.
  348. *
  349. * Will return -EAGAIN, and a reason if a restart is needed.
  350. * If passed in, *reason will always be set, even in error.
  351. */
  352. int ocfs2_do_extend_allocation(struct ocfs2_super *osb,
  353. struct inode *inode,
  354. u32 *logical_offset,
  355. u32 clusters_to_add,
  356. int mark_unwritten,
  357. struct buffer_head *fe_bh,
  358. handle_t *handle,
  359. struct ocfs2_alloc_context *data_ac,
  360. struct ocfs2_alloc_context *meta_ac,
  361. enum ocfs2_alloc_restarted *reason_ret)
  362. {
  363. int status = 0;
  364. int free_extents;
  365. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  366. enum ocfs2_alloc_restarted reason = RESTART_NONE;
  367. u32 bit_off, num_bits;
  368. u64 block;
  369. u8 flags = 0;
  370. BUG_ON(!clusters_to_add);
  371. if (mark_unwritten)
  372. flags = OCFS2_EXT_UNWRITTEN;
  373. free_extents = ocfs2_num_free_extents(osb, inode, fe);
  374. if (free_extents < 0) {
  375. status = free_extents;
  376. mlog_errno(status);
  377. goto leave;
  378. }
  379. /* there are two cases which could cause us to EAGAIN in the
  380. * we-need-more-metadata case:
  381. * 1) we haven't reserved *any*
  382. * 2) we are so fragmented, we've needed to add metadata too
  383. * many times. */
  384. if (!free_extents && !meta_ac) {
  385. mlog(0, "we haven't reserved any metadata!\n");
  386. status = -EAGAIN;
  387. reason = RESTART_META;
  388. goto leave;
  389. } else if ((!free_extents)
  390. && (ocfs2_alloc_context_bits_left(meta_ac)
  391. < ocfs2_extend_meta_needed(fe))) {
  392. mlog(0, "filesystem is really fragmented...\n");
  393. status = -EAGAIN;
  394. reason = RESTART_META;
  395. goto leave;
  396. }
  397. status = ocfs2_claim_clusters(osb, handle, data_ac, 1,
  398. &bit_off, &num_bits);
  399. if (status < 0) {
  400. if (status != -ENOSPC)
  401. mlog_errno(status);
  402. goto leave;
  403. }
  404. BUG_ON(num_bits > clusters_to_add);
  405. /* reserve our write early -- insert_extent may update the inode */
  406. status = ocfs2_journal_access(handle, inode, fe_bh,
  407. OCFS2_JOURNAL_ACCESS_WRITE);
  408. if (status < 0) {
  409. mlog_errno(status);
  410. goto leave;
  411. }
  412. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  413. mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
  414. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  415. status = ocfs2_insert_extent(osb, handle, inode, fe_bh,
  416. *logical_offset, block, num_bits,
  417. flags, meta_ac);
  418. if (status < 0) {
  419. mlog_errno(status);
  420. goto leave;
  421. }
  422. status = ocfs2_journal_dirty(handle, fe_bh);
  423. if (status < 0) {
  424. mlog_errno(status);
  425. goto leave;
  426. }
  427. clusters_to_add -= num_bits;
  428. *logical_offset += num_bits;
  429. if (clusters_to_add) {
  430. mlog(0, "need to alloc once more, clusters = %u, wanted = "
  431. "%u\n", fe->i_clusters, clusters_to_add);
  432. status = -EAGAIN;
  433. reason = RESTART_TRANS;
  434. }
  435. leave:
  436. mlog_exit(status);
  437. if (reason_ret)
  438. *reason_ret = reason;
  439. return status;
  440. }
  441. /*
  442. * For a given allocation, determine which allocators will need to be
  443. * accessed, and lock them, reserving the appropriate number of bits.
  444. *
  445. * Sparse file systems call this from ocfs2_write_begin_nolock()
  446. * and ocfs2_allocate_unwritten_extents().
  447. *
  448. * File systems which don't support holes call this from
  449. * ocfs2_extend_allocation().
  450. */
  451. int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di,
  452. u32 clusters_to_add, u32 extents_to_split,
  453. struct ocfs2_alloc_context **data_ac,
  454. struct ocfs2_alloc_context **meta_ac)
  455. {
  456. int ret = 0, num_free_extents;
  457. unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
  458. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  459. *meta_ac = NULL;
  460. if (data_ac)
  461. *data_ac = NULL;
  462. BUG_ON(clusters_to_add != 0 && data_ac == NULL);
  463. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  464. "clusters_to_add = %u, extents_to_split = %u\n",
  465. (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode),
  466. le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split);
  467. num_free_extents = ocfs2_num_free_extents(osb, inode, di);
  468. if (num_free_extents < 0) {
  469. ret = num_free_extents;
  470. mlog_errno(ret);
  471. goto out;
  472. }
  473. /*
  474. * Sparse allocation file systems need to be more conservative
  475. * with reserving room for expansion - the actual allocation
  476. * happens while we've got a journal handle open so re-taking
  477. * a cluster lock (because we ran out of room for another
  478. * extent) will violate ordering rules.
  479. *
  480. * Most of the time we'll only be seeing this 1 cluster at a time
  481. * anyway.
  482. *
  483. * Always lock for any unwritten extents - we might want to
  484. * add blocks during a split.
  485. */
  486. if (!num_free_extents ||
  487. (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
  488. ret = ocfs2_reserve_new_metadata(osb, di, meta_ac);
  489. if (ret < 0) {
  490. if (ret != -ENOSPC)
  491. mlog_errno(ret);
  492. goto out;
  493. }
  494. }
  495. if (clusters_to_add == 0)
  496. goto out;
  497. ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  498. if (ret < 0) {
  499. if (ret != -ENOSPC)
  500. mlog_errno(ret);
  501. goto out;
  502. }
  503. out:
  504. if (ret) {
  505. if (*meta_ac) {
  506. ocfs2_free_alloc_context(*meta_ac);
  507. *meta_ac = NULL;
  508. }
  509. /*
  510. * We cannot have an error and a non null *data_ac.
  511. */
  512. }
  513. return ret;
  514. }
  515. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  516. u32 clusters_to_add, int mark_unwritten)
  517. {
  518. int status = 0;
  519. int restart_func = 0;
  520. int credits;
  521. u32 prev_clusters;
  522. struct buffer_head *bh = NULL;
  523. struct ocfs2_dinode *fe = NULL;
  524. handle_t *handle = NULL;
  525. struct ocfs2_alloc_context *data_ac = NULL;
  526. struct ocfs2_alloc_context *meta_ac = NULL;
  527. enum ocfs2_alloc_restarted why;
  528. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  529. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  530. /*
  531. * This function only exists for file systems which don't
  532. * support holes.
  533. */
  534. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  535. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  536. OCFS2_BH_CACHED, inode);
  537. if (status < 0) {
  538. mlog_errno(status);
  539. goto leave;
  540. }
  541. fe = (struct ocfs2_dinode *) bh->b_data;
  542. if (!OCFS2_IS_VALID_DINODE(fe)) {
  543. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  544. status = -EIO;
  545. goto leave;
  546. }
  547. restart_all:
  548. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  549. status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac,
  550. &meta_ac);
  551. if (status) {
  552. mlog_errno(status);
  553. goto leave;
  554. }
  555. credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add);
  556. handle = ocfs2_start_trans(osb, credits);
  557. if (IS_ERR(handle)) {
  558. status = PTR_ERR(handle);
  559. handle = NULL;
  560. mlog_errno(status);
  561. goto leave;
  562. }
  563. restarted_transaction:
  564. /* reserve a write to the file entry early on - that we if we
  565. * run out of credits in the allocation path, we can still
  566. * update i_size. */
  567. status = ocfs2_journal_access(handle, inode, bh,
  568. OCFS2_JOURNAL_ACCESS_WRITE);
  569. if (status < 0) {
  570. mlog_errno(status);
  571. goto leave;
  572. }
  573. prev_clusters = OCFS2_I(inode)->ip_clusters;
  574. status = ocfs2_do_extend_allocation(osb,
  575. inode,
  576. &logical_start,
  577. clusters_to_add,
  578. mark_unwritten,
  579. bh,
  580. handle,
  581. data_ac,
  582. meta_ac,
  583. &why);
  584. if ((status < 0) && (status != -EAGAIN)) {
  585. if (status != -ENOSPC)
  586. mlog_errno(status);
  587. goto leave;
  588. }
  589. status = ocfs2_journal_dirty(handle, bh);
  590. if (status < 0) {
  591. mlog_errno(status);
  592. goto leave;
  593. }
  594. spin_lock(&OCFS2_I(inode)->ip_lock);
  595. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  596. spin_unlock(&OCFS2_I(inode)->ip_lock);
  597. if (why != RESTART_NONE && clusters_to_add) {
  598. if (why == RESTART_META) {
  599. mlog(0, "restarting function.\n");
  600. restart_func = 1;
  601. } else {
  602. BUG_ON(why != RESTART_TRANS);
  603. mlog(0, "restarting transaction.\n");
  604. /* TODO: This can be more intelligent. */
  605. credits = ocfs2_calc_extend_credits(osb->sb,
  606. fe,
  607. clusters_to_add);
  608. status = ocfs2_extend_trans(handle, credits);
  609. if (status < 0) {
  610. /* handle still has to be committed at
  611. * this point. */
  612. status = -ENOMEM;
  613. mlog_errno(status);
  614. goto leave;
  615. }
  616. goto restarted_transaction;
  617. }
  618. }
  619. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  620. le32_to_cpu(fe->i_clusters),
  621. (unsigned long long)le64_to_cpu(fe->i_size));
  622. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  623. OCFS2_I(inode)->ip_clusters, i_size_read(inode));
  624. leave:
  625. if (handle) {
  626. ocfs2_commit_trans(osb, handle);
  627. handle = NULL;
  628. }
  629. if (data_ac) {
  630. ocfs2_free_alloc_context(data_ac);
  631. data_ac = NULL;
  632. }
  633. if (meta_ac) {
  634. ocfs2_free_alloc_context(meta_ac);
  635. meta_ac = NULL;
  636. }
  637. if ((!status) && restart_func) {
  638. restart_func = 0;
  639. goto restart_all;
  640. }
  641. if (bh) {
  642. brelse(bh);
  643. bh = NULL;
  644. }
  645. mlog_exit(status);
  646. return status;
  647. }
  648. static int ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  649. u32 clusters_to_add, int mark_unwritten)
  650. {
  651. int ret;
  652. /*
  653. * The alloc sem blocks peope in read/write from reading our
  654. * allocation until we're done changing it. We depend on
  655. * i_mutex to block other extend/truncate calls while we're
  656. * here.
  657. */
  658. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  659. ret = __ocfs2_extend_allocation(inode, logical_start, clusters_to_add,
  660. mark_unwritten);
  661. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  662. return ret;
  663. }
  664. /* Some parts of this taken from generic_cont_expand, which turned out
  665. * to be too fragile to do exactly what we need without us having to
  666. * worry about recursive locking in ->prepare_write() and
  667. * ->commit_write(). */
  668. static int ocfs2_write_zero_page(struct inode *inode,
  669. u64 size)
  670. {
  671. struct address_space *mapping = inode->i_mapping;
  672. struct page *page;
  673. unsigned long index;
  674. unsigned int offset;
  675. handle_t *handle = NULL;
  676. int ret;
  677. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  678. /* ugh. in prepare/commit_write, if from==to==start of block, we
  679. ** skip the prepare. make sure we never send an offset for the start
  680. ** of a block
  681. */
  682. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  683. offset++;
  684. }
  685. index = size >> PAGE_CACHE_SHIFT;
  686. page = grab_cache_page(mapping, index);
  687. if (!page) {
  688. ret = -ENOMEM;
  689. mlog_errno(ret);
  690. goto out;
  691. }
  692. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  693. if (ret < 0) {
  694. mlog_errno(ret);
  695. goto out_unlock;
  696. }
  697. if (ocfs2_should_order_data(inode)) {
  698. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  699. offset);
  700. if (IS_ERR(handle)) {
  701. ret = PTR_ERR(handle);
  702. handle = NULL;
  703. goto out_unlock;
  704. }
  705. }
  706. /* must not update i_size! */
  707. ret = block_commit_write(page, offset, offset);
  708. if (ret < 0)
  709. mlog_errno(ret);
  710. else
  711. ret = 0;
  712. if (handle)
  713. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  714. out_unlock:
  715. unlock_page(page);
  716. page_cache_release(page);
  717. out:
  718. return ret;
  719. }
  720. static int ocfs2_zero_extend(struct inode *inode,
  721. u64 zero_to_size)
  722. {
  723. int ret = 0;
  724. u64 start_off;
  725. struct super_block *sb = inode->i_sb;
  726. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  727. while (start_off < zero_to_size) {
  728. ret = ocfs2_write_zero_page(inode, start_off);
  729. if (ret < 0) {
  730. mlog_errno(ret);
  731. goto out;
  732. }
  733. start_off += sb->s_blocksize;
  734. /*
  735. * Very large extends have the potential to lock up
  736. * the cpu for extended periods of time.
  737. */
  738. cond_resched();
  739. }
  740. out:
  741. return ret;
  742. }
  743. /*
  744. * A tail_to_skip value > 0 indicates that we're being called from
  745. * ocfs2_file_aio_write(). This has the following implications:
  746. *
  747. * - we don't want to update i_size
  748. * - di_bh will be NULL, which is fine because it's only used in the
  749. * case where we want to update i_size.
  750. * - ocfs2_zero_extend() will then only be filling the hole created
  751. * between i_size and the start of the write.
  752. */
  753. static int ocfs2_extend_file(struct inode *inode,
  754. struct buffer_head *di_bh,
  755. u64 new_i_size,
  756. size_t tail_to_skip)
  757. {
  758. int ret = 0;
  759. u32 clusters_to_add = 0;
  760. BUG_ON(!tail_to_skip && !di_bh);
  761. /* setattr sometimes calls us like this. */
  762. if (new_i_size == 0)
  763. goto out;
  764. if (i_size_read(inode) == new_i_size)
  765. goto out;
  766. BUG_ON(new_i_size < i_size_read(inode));
  767. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  768. BUG_ON(tail_to_skip != 0);
  769. goto out_update_size;
  770. }
  771. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) -
  772. OCFS2_I(inode)->ip_clusters;
  773. /*
  774. * protect the pages that ocfs2_zero_extend is going to be
  775. * pulling into the page cache.. we do this before the
  776. * metadata extend so that we don't get into the situation
  777. * where we've extended the metadata but can't get the data
  778. * lock to zero.
  779. */
  780. ret = ocfs2_data_lock(inode, 1);
  781. if (ret < 0) {
  782. mlog_errno(ret);
  783. goto out;
  784. }
  785. if (clusters_to_add) {
  786. ret = ocfs2_extend_allocation(inode,
  787. OCFS2_I(inode)->ip_clusters,
  788. clusters_to_add, 0);
  789. if (ret < 0) {
  790. mlog_errno(ret);
  791. goto out_unlock;
  792. }
  793. }
  794. /*
  795. * Call this even if we don't add any clusters to the tree. We
  796. * still need to zero the area between the old i_size and the
  797. * new i_size.
  798. */
  799. ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip);
  800. if (ret < 0) {
  801. mlog_errno(ret);
  802. goto out_unlock;
  803. }
  804. out_update_size:
  805. if (!tail_to_skip) {
  806. /* We're being called from ocfs2_setattr() which wants
  807. * us to update i_size */
  808. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  809. if (ret < 0)
  810. mlog_errno(ret);
  811. }
  812. out_unlock:
  813. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  814. ocfs2_data_unlock(inode, 1);
  815. out:
  816. return ret;
  817. }
  818. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  819. {
  820. int status = 0, size_change;
  821. struct inode *inode = dentry->d_inode;
  822. struct super_block *sb = inode->i_sb;
  823. struct ocfs2_super *osb = OCFS2_SB(sb);
  824. struct buffer_head *bh = NULL;
  825. handle_t *handle = NULL;
  826. mlog_entry("(0x%p, '%.*s')\n", dentry,
  827. dentry->d_name.len, dentry->d_name.name);
  828. if (attr->ia_valid & ATTR_MODE)
  829. mlog(0, "mode change: %d\n", attr->ia_mode);
  830. if (attr->ia_valid & ATTR_UID)
  831. mlog(0, "uid change: %d\n", attr->ia_uid);
  832. if (attr->ia_valid & ATTR_GID)
  833. mlog(0, "gid change: %d\n", attr->ia_gid);
  834. if (attr->ia_valid & ATTR_SIZE)
  835. mlog(0, "size change...\n");
  836. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  837. mlog(0, "time change...\n");
  838. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  839. | ATTR_GID | ATTR_UID | ATTR_MODE)
  840. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  841. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  842. return 0;
  843. }
  844. status = inode_change_ok(inode, attr);
  845. if (status)
  846. return status;
  847. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  848. if (size_change) {
  849. status = ocfs2_rw_lock(inode, 1);
  850. if (status < 0) {
  851. mlog_errno(status);
  852. goto bail;
  853. }
  854. }
  855. status = ocfs2_meta_lock(inode, &bh, 1);
  856. if (status < 0) {
  857. if (status != -ENOENT)
  858. mlog_errno(status);
  859. goto bail_unlock_rw;
  860. }
  861. if (size_change && attr->ia_size != i_size_read(inode)) {
  862. if (i_size_read(inode) > attr->ia_size)
  863. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  864. else
  865. status = ocfs2_extend_file(inode, bh, attr->ia_size, 0);
  866. if (status < 0) {
  867. if (status != -ENOSPC)
  868. mlog_errno(status);
  869. status = -ENOSPC;
  870. goto bail_unlock;
  871. }
  872. }
  873. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  874. if (IS_ERR(handle)) {
  875. status = PTR_ERR(handle);
  876. mlog_errno(status);
  877. goto bail_unlock;
  878. }
  879. /*
  880. * This will intentionally not wind up calling vmtruncate(),
  881. * since all the work for a size change has been done above.
  882. * Otherwise, we could get into problems with truncate as
  883. * ip_alloc_sem is used there to protect against i_size
  884. * changes.
  885. */
  886. status = inode_setattr(inode, attr);
  887. if (status < 0) {
  888. mlog_errno(status);
  889. goto bail_commit;
  890. }
  891. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  892. if (status < 0)
  893. mlog_errno(status);
  894. bail_commit:
  895. ocfs2_commit_trans(osb, handle);
  896. bail_unlock:
  897. ocfs2_meta_unlock(inode, 1);
  898. bail_unlock_rw:
  899. if (size_change)
  900. ocfs2_rw_unlock(inode, 1);
  901. bail:
  902. if (bh)
  903. brelse(bh);
  904. mlog_exit(status);
  905. return status;
  906. }
  907. int ocfs2_getattr(struct vfsmount *mnt,
  908. struct dentry *dentry,
  909. struct kstat *stat)
  910. {
  911. struct inode *inode = dentry->d_inode;
  912. struct super_block *sb = dentry->d_inode->i_sb;
  913. struct ocfs2_super *osb = sb->s_fs_info;
  914. int err;
  915. mlog_entry_void();
  916. err = ocfs2_inode_revalidate(dentry);
  917. if (err) {
  918. if (err != -ENOENT)
  919. mlog_errno(err);
  920. goto bail;
  921. }
  922. generic_fillattr(inode, stat);
  923. /* We set the blksize from the cluster size for performance */
  924. stat->blksize = osb->s_clustersize;
  925. bail:
  926. mlog_exit(err);
  927. return err;
  928. }
  929. int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  930. {
  931. int ret;
  932. mlog_entry_void();
  933. ret = ocfs2_meta_lock(inode, NULL, 0);
  934. if (ret) {
  935. if (ret != -ENOENT)
  936. mlog_errno(ret);
  937. goto out;
  938. }
  939. ret = generic_permission(inode, mask, NULL);
  940. ocfs2_meta_unlock(inode, 0);
  941. out:
  942. mlog_exit(ret);
  943. return ret;
  944. }
  945. static int __ocfs2_write_remove_suid(struct inode *inode,
  946. struct buffer_head *bh)
  947. {
  948. int ret;
  949. handle_t *handle;
  950. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  951. struct ocfs2_dinode *di;
  952. mlog_entry("(Inode %llu, mode 0%o)\n",
  953. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  954. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  955. if (handle == NULL) {
  956. ret = -ENOMEM;
  957. mlog_errno(ret);
  958. goto out;
  959. }
  960. ret = ocfs2_journal_access(handle, inode, bh,
  961. OCFS2_JOURNAL_ACCESS_WRITE);
  962. if (ret < 0) {
  963. mlog_errno(ret);
  964. goto out_trans;
  965. }
  966. inode->i_mode &= ~S_ISUID;
  967. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  968. inode->i_mode &= ~S_ISGID;
  969. di = (struct ocfs2_dinode *) bh->b_data;
  970. di->i_mode = cpu_to_le16(inode->i_mode);
  971. ret = ocfs2_journal_dirty(handle, bh);
  972. if (ret < 0)
  973. mlog_errno(ret);
  974. out_trans:
  975. ocfs2_commit_trans(osb, handle);
  976. out:
  977. mlog_exit(ret);
  978. return ret;
  979. }
  980. /*
  981. * Will look for holes and unwritten extents in the range starting at
  982. * pos for count bytes (inclusive).
  983. */
  984. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  985. size_t count)
  986. {
  987. int ret = 0;
  988. unsigned int extent_flags;
  989. u32 cpos, clusters, extent_len, phys_cpos;
  990. struct super_block *sb = inode->i_sb;
  991. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  992. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  993. while (clusters) {
  994. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  995. &extent_flags);
  996. if (ret < 0) {
  997. mlog_errno(ret);
  998. goto out;
  999. }
  1000. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1001. ret = 1;
  1002. break;
  1003. }
  1004. if (extent_len > clusters)
  1005. extent_len = clusters;
  1006. clusters -= extent_len;
  1007. cpos += extent_len;
  1008. }
  1009. out:
  1010. return ret;
  1011. }
  1012. static int ocfs2_write_remove_suid(struct inode *inode)
  1013. {
  1014. int ret;
  1015. struct buffer_head *bh = NULL;
  1016. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1017. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1018. oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  1019. if (ret < 0) {
  1020. mlog_errno(ret);
  1021. goto out;
  1022. }
  1023. ret = __ocfs2_write_remove_suid(inode, bh);
  1024. out:
  1025. brelse(bh);
  1026. return ret;
  1027. }
  1028. /*
  1029. * Allocate enough extents to cover the region starting at byte offset
  1030. * start for len bytes. Existing extents are skipped, any extents
  1031. * added are marked as "unwritten".
  1032. */
  1033. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1034. u64 start, u64 len)
  1035. {
  1036. int ret;
  1037. u32 cpos, phys_cpos, clusters, alloc_size;
  1038. /*
  1039. * We consider both start and len to be inclusive.
  1040. */
  1041. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1042. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1043. clusters -= cpos;
  1044. while (clusters) {
  1045. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1046. &alloc_size, NULL);
  1047. if (ret) {
  1048. mlog_errno(ret);
  1049. goto out;
  1050. }
  1051. /*
  1052. * Hole or existing extent len can be arbitrary, so
  1053. * cap it to our own allocation request.
  1054. */
  1055. if (alloc_size > clusters)
  1056. alloc_size = clusters;
  1057. if (phys_cpos) {
  1058. /*
  1059. * We already have an allocation at this
  1060. * region so we can safely skip it.
  1061. */
  1062. goto next;
  1063. }
  1064. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1065. if (ret) {
  1066. if (ret != -ENOSPC)
  1067. mlog_errno(ret);
  1068. goto out;
  1069. }
  1070. next:
  1071. cpos += alloc_size;
  1072. clusters -= alloc_size;
  1073. }
  1074. ret = 0;
  1075. out:
  1076. return ret;
  1077. }
  1078. static int __ocfs2_remove_inode_range(struct inode *inode,
  1079. struct buffer_head *di_bh,
  1080. u32 cpos, u32 phys_cpos, u32 len,
  1081. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1082. {
  1083. int ret;
  1084. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  1085. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1086. struct inode *tl_inode = osb->osb_tl_inode;
  1087. handle_t *handle;
  1088. struct ocfs2_alloc_context *meta_ac = NULL;
  1089. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1090. ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac);
  1091. if (ret) {
  1092. mlog_errno(ret);
  1093. return ret;
  1094. }
  1095. mutex_lock(&tl_inode->i_mutex);
  1096. if (ocfs2_truncate_log_needs_flush(osb)) {
  1097. ret = __ocfs2_flush_truncate_log(osb);
  1098. if (ret < 0) {
  1099. mlog_errno(ret);
  1100. goto out;
  1101. }
  1102. }
  1103. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  1104. if (handle == NULL) {
  1105. ret = -ENOMEM;
  1106. mlog_errno(ret);
  1107. goto out;
  1108. }
  1109. ret = ocfs2_journal_access(handle, inode, di_bh,
  1110. OCFS2_JOURNAL_ACCESS_WRITE);
  1111. if (ret) {
  1112. mlog_errno(ret);
  1113. goto out;
  1114. }
  1115. ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac,
  1116. dealloc);
  1117. if (ret) {
  1118. mlog_errno(ret);
  1119. goto out_commit;
  1120. }
  1121. OCFS2_I(inode)->ip_clusters -= len;
  1122. di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1123. ret = ocfs2_journal_dirty(handle, di_bh);
  1124. if (ret) {
  1125. mlog_errno(ret);
  1126. goto out_commit;
  1127. }
  1128. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  1129. if (ret)
  1130. mlog_errno(ret);
  1131. out_commit:
  1132. ocfs2_commit_trans(osb, handle);
  1133. out:
  1134. mutex_unlock(&tl_inode->i_mutex);
  1135. if (meta_ac)
  1136. ocfs2_free_alloc_context(meta_ac);
  1137. return ret;
  1138. }
  1139. /*
  1140. * Truncate a byte range, avoiding pages within partial clusters. This
  1141. * preserves those pages for the zeroing code to write to.
  1142. */
  1143. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1144. u64 byte_len)
  1145. {
  1146. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1147. loff_t start, end;
  1148. struct address_space *mapping = inode->i_mapping;
  1149. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1150. end = byte_start + byte_len;
  1151. end = end & ~(osb->s_clustersize - 1);
  1152. if (start < end) {
  1153. unmap_mapping_range(mapping, start, end - start, 0);
  1154. truncate_inode_pages_range(mapping, start, end - 1);
  1155. }
  1156. }
  1157. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1158. u64 start, u64 len)
  1159. {
  1160. int ret = 0;
  1161. u64 tmpend, end = start + len;
  1162. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1163. unsigned int csize = osb->s_clustersize;
  1164. handle_t *handle;
  1165. /*
  1166. * The "start" and "end" values are NOT necessarily part of
  1167. * the range whose allocation is being deleted. Rather, this
  1168. * is what the user passed in with the request. We must zero
  1169. * partial clusters here. There's no need to worry about
  1170. * physical allocation - the zeroing code knows to skip holes.
  1171. */
  1172. mlog(0, "byte start: %llu, end: %llu\n",
  1173. (unsigned long long)start, (unsigned long long)end);
  1174. /*
  1175. * If both edges are on a cluster boundary then there's no
  1176. * zeroing required as the region is part of the allocation to
  1177. * be truncated.
  1178. */
  1179. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1180. goto out;
  1181. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1182. if (handle == NULL) {
  1183. ret = -ENOMEM;
  1184. mlog_errno(ret);
  1185. goto out;
  1186. }
  1187. /*
  1188. * We want to get the byte offset of the end of the 1st cluster.
  1189. */
  1190. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1191. if (tmpend > end)
  1192. tmpend = end;
  1193. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1194. (unsigned long long)start, (unsigned long long)tmpend);
  1195. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1196. if (ret)
  1197. mlog_errno(ret);
  1198. if (tmpend < end) {
  1199. /*
  1200. * This may make start and end equal, but the zeroing
  1201. * code will skip any work in that case so there's no
  1202. * need to catch it up here.
  1203. */
  1204. start = end & ~(osb->s_clustersize - 1);
  1205. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1206. (unsigned long long)start, (unsigned long long)end);
  1207. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1208. if (ret)
  1209. mlog_errno(ret);
  1210. }
  1211. ocfs2_commit_trans(osb, handle);
  1212. out:
  1213. return ret;
  1214. }
  1215. static int ocfs2_remove_inode_range(struct inode *inode,
  1216. struct buffer_head *di_bh, u64 byte_start,
  1217. u64 byte_len)
  1218. {
  1219. int ret = 0;
  1220. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1221. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1222. struct ocfs2_cached_dealloc_ctxt dealloc;
  1223. ocfs2_init_dealloc_ctxt(&dealloc);
  1224. if (byte_len == 0)
  1225. return 0;
  1226. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1227. trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1228. if (trunc_len >= trunc_start)
  1229. trunc_len -= trunc_start;
  1230. else
  1231. trunc_len = 0;
  1232. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
  1233. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1234. (unsigned long long)byte_start,
  1235. (unsigned long long)byte_len, trunc_start, trunc_len);
  1236. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1237. if (ret) {
  1238. mlog_errno(ret);
  1239. goto out;
  1240. }
  1241. cpos = trunc_start;
  1242. while (trunc_len) {
  1243. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1244. &alloc_size, NULL);
  1245. if (ret) {
  1246. mlog_errno(ret);
  1247. goto out;
  1248. }
  1249. if (alloc_size > trunc_len)
  1250. alloc_size = trunc_len;
  1251. /* Only do work for non-holes */
  1252. if (phys_cpos != 0) {
  1253. ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
  1254. phys_cpos, alloc_size,
  1255. &dealloc);
  1256. if (ret) {
  1257. mlog_errno(ret);
  1258. goto out;
  1259. }
  1260. }
  1261. cpos += alloc_size;
  1262. trunc_len -= alloc_size;
  1263. }
  1264. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1265. out:
  1266. ocfs2_schedule_truncate_log_flush(osb, 1);
  1267. ocfs2_run_deallocs(osb, &dealloc);
  1268. return ret;
  1269. }
  1270. /*
  1271. * Parts of this function taken from xfs_change_file_space()
  1272. */
  1273. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1274. struct ocfs2_space_resv *sr)
  1275. {
  1276. int ret;
  1277. s64 llen;
  1278. struct inode *inode = file->f_path.dentry->d_inode;
  1279. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1280. struct buffer_head *di_bh = NULL;
  1281. handle_t *handle;
  1282. unsigned long long max_off = ocfs2_max_file_offset(inode->i_sb->s_blocksize_bits);
  1283. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1284. !ocfs2_writes_unwritten_extents(osb))
  1285. return -ENOTTY;
  1286. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1287. !ocfs2_sparse_alloc(osb))
  1288. return -ENOTTY;
  1289. if (!S_ISREG(inode->i_mode))
  1290. return -EINVAL;
  1291. if (!(file->f_mode & FMODE_WRITE))
  1292. return -EBADF;
  1293. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1294. return -EROFS;
  1295. mutex_lock(&inode->i_mutex);
  1296. /*
  1297. * This prevents concurrent writes on other nodes
  1298. */
  1299. ret = ocfs2_rw_lock(inode, 1);
  1300. if (ret) {
  1301. mlog_errno(ret);
  1302. goto out;
  1303. }
  1304. ret = ocfs2_meta_lock(inode, &di_bh, 1);
  1305. if (ret) {
  1306. mlog_errno(ret);
  1307. goto out_rw_unlock;
  1308. }
  1309. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1310. ret = -EPERM;
  1311. goto out_meta_unlock;
  1312. }
  1313. switch (sr->l_whence) {
  1314. case 0: /*SEEK_SET*/
  1315. break;
  1316. case 1: /*SEEK_CUR*/
  1317. sr->l_start += file->f_pos;
  1318. break;
  1319. case 2: /*SEEK_END*/
  1320. sr->l_start += i_size_read(inode);
  1321. break;
  1322. default:
  1323. ret = -EINVAL;
  1324. goto out_meta_unlock;
  1325. }
  1326. sr->l_whence = 0;
  1327. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1328. if (sr->l_start < 0
  1329. || sr->l_start > max_off
  1330. || (sr->l_start + llen) < 0
  1331. || (sr->l_start + llen) > max_off) {
  1332. ret = -EINVAL;
  1333. goto out_meta_unlock;
  1334. }
  1335. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1336. if (sr->l_len <= 0) {
  1337. ret = -EINVAL;
  1338. goto out_meta_unlock;
  1339. }
  1340. }
  1341. if (should_remove_suid(file->f_path.dentry)) {
  1342. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1343. if (ret) {
  1344. mlog_errno(ret);
  1345. goto out_meta_unlock;
  1346. }
  1347. }
  1348. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1349. switch (cmd) {
  1350. case OCFS2_IOC_RESVSP:
  1351. case OCFS2_IOC_RESVSP64:
  1352. /*
  1353. * This takes unsigned offsets, but the signed ones we
  1354. * pass have been checked against overflow above.
  1355. */
  1356. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1357. sr->l_len);
  1358. break;
  1359. case OCFS2_IOC_UNRESVSP:
  1360. case OCFS2_IOC_UNRESVSP64:
  1361. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1362. sr->l_len);
  1363. break;
  1364. default:
  1365. ret = -EINVAL;
  1366. }
  1367. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1368. if (ret) {
  1369. mlog_errno(ret);
  1370. goto out_meta_unlock;
  1371. }
  1372. /*
  1373. * We update c/mtime for these changes
  1374. */
  1375. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1376. if (IS_ERR(handle)) {
  1377. ret = PTR_ERR(handle);
  1378. mlog_errno(ret);
  1379. goto out_meta_unlock;
  1380. }
  1381. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1382. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1383. if (ret < 0)
  1384. mlog_errno(ret);
  1385. ocfs2_commit_trans(osb, handle);
  1386. out_meta_unlock:
  1387. brelse(di_bh);
  1388. ocfs2_meta_unlock(inode, 1);
  1389. out_rw_unlock:
  1390. ocfs2_rw_unlock(inode, 1);
  1391. mutex_unlock(&inode->i_mutex);
  1392. out:
  1393. return ret;
  1394. }
  1395. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1396. loff_t *ppos,
  1397. size_t count,
  1398. int appending,
  1399. int *direct_io)
  1400. {
  1401. int ret = 0, meta_level = appending;
  1402. struct inode *inode = dentry->d_inode;
  1403. u32 clusters;
  1404. loff_t newsize, saved_pos;
  1405. /*
  1406. * We sample i_size under a read level meta lock to see if our write
  1407. * is extending the file, if it is we back off and get a write level
  1408. * meta lock.
  1409. */
  1410. for(;;) {
  1411. ret = ocfs2_meta_lock(inode, NULL, meta_level);
  1412. if (ret < 0) {
  1413. meta_level = -1;
  1414. mlog_errno(ret);
  1415. goto out;
  1416. }
  1417. /* Clear suid / sgid if necessary. We do this here
  1418. * instead of later in the write path because
  1419. * remove_suid() calls ->setattr without any hint that
  1420. * we may have already done our cluster locking. Since
  1421. * ocfs2_setattr() *must* take cluster locks to
  1422. * proceeed, this will lead us to recursively lock the
  1423. * inode. There's also the dinode i_size state which
  1424. * can be lost via setattr during extending writes (we
  1425. * set inode->i_size at the end of a write. */
  1426. if (should_remove_suid(dentry)) {
  1427. if (meta_level == 0) {
  1428. ocfs2_meta_unlock(inode, meta_level);
  1429. meta_level = 1;
  1430. continue;
  1431. }
  1432. ret = ocfs2_write_remove_suid(inode);
  1433. if (ret < 0) {
  1434. mlog_errno(ret);
  1435. goto out_unlock;
  1436. }
  1437. }
  1438. /* work on a copy of ppos until we're sure that we won't have
  1439. * to recalculate it due to relocking. */
  1440. if (appending) {
  1441. saved_pos = i_size_read(inode);
  1442. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1443. } else {
  1444. saved_pos = *ppos;
  1445. }
  1446. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
  1447. loff_t end = saved_pos + count;
  1448. /*
  1449. * Skip the O_DIRECT checks if we don't need
  1450. * them.
  1451. */
  1452. if (!direct_io || !(*direct_io))
  1453. break;
  1454. /*
  1455. * Allowing concurrent direct writes means
  1456. * i_size changes wouldn't be synchronized, so
  1457. * one node could wind up truncating another
  1458. * nodes writes.
  1459. */
  1460. if (end > i_size_read(inode)) {
  1461. *direct_io = 0;
  1462. break;
  1463. }
  1464. /*
  1465. * We don't fill holes during direct io, so
  1466. * check for them here. If any are found, the
  1467. * caller will have to retake some cluster
  1468. * locks and initiate the io as buffered.
  1469. */
  1470. ret = ocfs2_check_range_for_holes(inode, saved_pos,
  1471. count);
  1472. if (ret == 1) {
  1473. *direct_io = 0;
  1474. ret = 0;
  1475. } else if (ret < 0)
  1476. mlog_errno(ret);
  1477. break;
  1478. }
  1479. /*
  1480. * The rest of this loop is concerned with legacy file
  1481. * systems which don't support sparse files.
  1482. */
  1483. newsize = count + saved_pos;
  1484. mlog(0, "pos=%lld newsize=%lld cursize=%lld\n",
  1485. (long long) saved_pos, (long long) newsize,
  1486. (long long) i_size_read(inode));
  1487. /* No need for a higher level metadata lock if we're
  1488. * never going past i_size. */
  1489. if (newsize <= i_size_read(inode))
  1490. break;
  1491. if (meta_level == 0) {
  1492. ocfs2_meta_unlock(inode, meta_level);
  1493. meta_level = 1;
  1494. continue;
  1495. }
  1496. spin_lock(&OCFS2_I(inode)->ip_lock);
  1497. clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) -
  1498. OCFS2_I(inode)->ip_clusters;
  1499. spin_unlock(&OCFS2_I(inode)->ip_lock);
  1500. mlog(0, "Writing at EOF, may need more allocation: "
  1501. "i_size = %lld, newsize = %lld, need %u clusters\n",
  1502. (long long) i_size_read(inode), (long long) newsize,
  1503. clusters);
  1504. /* We only want to continue the rest of this loop if
  1505. * our extend will actually require more
  1506. * allocation. */
  1507. if (!clusters)
  1508. break;
  1509. ret = ocfs2_extend_file(inode, NULL, newsize, count);
  1510. if (ret < 0) {
  1511. if (ret != -ENOSPC)
  1512. mlog_errno(ret);
  1513. goto out_unlock;
  1514. }
  1515. break;
  1516. }
  1517. if (appending)
  1518. *ppos = saved_pos;
  1519. out_unlock:
  1520. ocfs2_meta_unlock(inode, meta_level);
  1521. out:
  1522. return ret;
  1523. }
  1524. static inline void
  1525. ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes)
  1526. {
  1527. const struct iovec *iov = *iovp;
  1528. size_t base = *basep;
  1529. do {
  1530. int copy = min(bytes, iov->iov_len - base);
  1531. bytes -= copy;
  1532. base += copy;
  1533. if (iov->iov_len == base) {
  1534. iov++;
  1535. base = 0;
  1536. }
  1537. } while (bytes);
  1538. *iovp = iov;
  1539. *basep = base;
  1540. }
  1541. static struct page * ocfs2_get_write_source(char **ret_src_buf,
  1542. const struct iovec *cur_iov,
  1543. size_t iov_offset)
  1544. {
  1545. int ret;
  1546. char *buf = cur_iov->iov_base + iov_offset;
  1547. struct page *src_page = NULL;
  1548. unsigned long off;
  1549. off = (unsigned long)(buf) & ~PAGE_CACHE_MASK;
  1550. if (!segment_eq(get_fs(), KERNEL_DS)) {
  1551. /*
  1552. * Pull in the user page. We want to do this outside
  1553. * of the meta data locks in order to preserve locking
  1554. * order in case of page fault.
  1555. */
  1556. ret = get_user_pages(current, current->mm,
  1557. (unsigned long)buf & PAGE_CACHE_MASK, 1,
  1558. 0, 0, &src_page, NULL);
  1559. if (ret == 1)
  1560. *ret_src_buf = kmap(src_page) + off;
  1561. else
  1562. src_page = ERR_PTR(-EFAULT);
  1563. } else {
  1564. *ret_src_buf = buf;
  1565. }
  1566. return src_page;
  1567. }
  1568. static void ocfs2_put_write_source(struct page *page)
  1569. {
  1570. if (page) {
  1571. kunmap(page);
  1572. page_cache_release(page);
  1573. }
  1574. }
  1575. static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos,
  1576. const struct iovec *iov,
  1577. unsigned long nr_segs,
  1578. size_t count,
  1579. ssize_t o_direct_written)
  1580. {
  1581. int ret = 0;
  1582. ssize_t copied, total = 0;
  1583. size_t iov_offset = 0, bytes;
  1584. loff_t pos;
  1585. const struct iovec *cur_iov = iov;
  1586. struct page *user_page, *page;
  1587. char * uninitialized_var(buf);
  1588. char *dst;
  1589. void *fsdata;
  1590. /*
  1591. * handle partial DIO write. Adjust cur_iov if needed.
  1592. */
  1593. ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written);
  1594. do {
  1595. pos = *ppos;
  1596. user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset);
  1597. if (IS_ERR(user_page)) {
  1598. ret = PTR_ERR(user_page);
  1599. goto out;
  1600. }
  1601. /* Stay within our page boundaries */
  1602. bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)),
  1603. (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK)));
  1604. /* Stay within the vector boundary */
  1605. bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset);
  1606. /* Stay within count */
  1607. bytes = min(bytes, count);
  1608. page = NULL;
  1609. ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0,
  1610. &page, &fsdata);
  1611. if (ret) {
  1612. mlog_errno(ret);
  1613. goto out;
  1614. }
  1615. dst = kmap_atomic(page, KM_USER0);
  1616. memcpy(dst + (pos & (PAGE_CACHE_SIZE - 1)), buf, bytes);
  1617. kunmap_atomic(dst, KM_USER0);
  1618. flush_dcache_page(page);
  1619. ocfs2_put_write_source(user_page);
  1620. copied = ocfs2_write_end(file, file->f_mapping, pos, bytes,
  1621. bytes, page, fsdata);
  1622. if (copied < 0) {
  1623. mlog_errno(copied);
  1624. ret = copied;
  1625. goto out;
  1626. }
  1627. total += copied;
  1628. *ppos = pos + copied;
  1629. count -= copied;
  1630. ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied);
  1631. } while(count);
  1632. out:
  1633. return total ? total : ret;
  1634. }
  1635. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1636. const struct iovec *iov,
  1637. unsigned long nr_segs,
  1638. loff_t pos)
  1639. {
  1640. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1641. int can_do_direct, sync = 0;
  1642. ssize_t written = 0;
  1643. size_t ocount; /* original count */
  1644. size_t count; /* after file limit checks */
  1645. loff_t *ppos = &iocb->ki_pos;
  1646. struct file *file = iocb->ki_filp;
  1647. struct inode *inode = file->f_path.dentry->d_inode;
  1648. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1649. (unsigned int)nr_segs,
  1650. file->f_path.dentry->d_name.len,
  1651. file->f_path.dentry->d_name.name);
  1652. if (iocb->ki_left == 0)
  1653. return 0;
  1654. ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
  1655. if (ret)
  1656. return ret;
  1657. count = ocount;
  1658. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1659. appending = file->f_flags & O_APPEND ? 1 : 0;
  1660. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1661. mutex_lock(&inode->i_mutex);
  1662. relock:
  1663. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1664. if (direct_io) {
  1665. down_read(&inode->i_alloc_sem);
  1666. have_alloc_sem = 1;
  1667. }
  1668. /* concurrent O_DIRECT writes are allowed */
  1669. rw_level = !direct_io;
  1670. ret = ocfs2_rw_lock(inode, rw_level);
  1671. if (ret < 0) {
  1672. mlog_errno(ret);
  1673. goto out_sems;
  1674. }
  1675. can_do_direct = direct_io;
  1676. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1677. iocb->ki_left, appending,
  1678. &can_do_direct);
  1679. if (ret < 0) {
  1680. mlog_errno(ret);
  1681. goto out;
  1682. }
  1683. /*
  1684. * We can't complete the direct I/O as requested, fall back to
  1685. * buffered I/O.
  1686. */
  1687. if (direct_io && !can_do_direct) {
  1688. ocfs2_rw_unlock(inode, rw_level);
  1689. up_read(&inode->i_alloc_sem);
  1690. have_alloc_sem = 0;
  1691. rw_level = -1;
  1692. direct_io = 0;
  1693. sync = 1;
  1694. goto relock;
  1695. }
  1696. if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode)))
  1697. sync = 1;
  1698. /*
  1699. * XXX: Is it ok to execute these checks a second time?
  1700. */
  1701. ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode));
  1702. if (ret)
  1703. goto out;
  1704. /*
  1705. * Set pos so that sync_page_range_nolock() below understands
  1706. * where to start from. We might've moved it around via the
  1707. * calls above. The range we want to actually sync starts from
  1708. * *ppos here.
  1709. *
  1710. */
  1711. pos = *ppos;
  1712. /* communicate with ocfs2_dio_end_io */
  1713. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1714. if (direct_io) {
  1715. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1716. ppos, count, ocount);
  1717. if (written < 0) {
  1718. ret = written;
  1719. goto out_dio;
  1720. }
  1721. } else {
  1722. written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs,
  1723. count, written);
  1724. if (written < 0) {
  1725. ret = written;
  1726. if (ret != -EFAULT || ret != -ENOSPC)
  1727. mlog_errno(ret);
  1728. goto out;
  1729. }
  1730. }
  1731. out_dio:
  1732. /* buffered aio wouldn't have proper lock coverage today */
  1733. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1734. /*
  1735. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1736. * function pointer which is called when o_direct io completes so that
  1737. * it can unlock our rw lock. (it's the clustered equivalent of
  1738. * i_alloc_sem; protects truncate from racing with pending ios).
  1739. * Unfortunately there are error cases which call end_io and others
  1740. * that don't. so we don't have to unlock the rw_lock if either an
  1741. * async dio is going to do it in the future or an end_io after an
  1742. * error has already done it.
  1743. */
  1744. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1745. rw_level = -1;
  1746. have_alloc_sem = 0;
  1747. }
  1748. out:
  1749. if (rw_level != -1)
  1750. ocfs2_rw_unlock(inode, rw_level);
  1751. out_sems:
  1752. if (have_alloc_sem)
  1753. up_read(&inode->i_alloc_sem);
  1754. if (written > 0 && sync) {
  1755. ssize_t err;
  1756. err = sync_page_range_nolock(inode, file->f_mapping, pos, count);
  1757. if (err < 0)
  1758. written = err;
  1759. }
  1760. mutex_unlock(&inode->i_mutex);
  1761. mlog_exit(ret);
  1762. return written ? written : ret;
  1763. }
  1764. static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe,
  1765. struct pipe_buffer *buf,
  1766. struct splice_desc *sd)
  1767. {
  1768. int ret, count;
  1769. ssize_t copied = 0;
  1770. struct file *file = sd->u.file;
  1771. unsigned int offset;
  1772. struct page *page = NULL;
  1773. void *fsdata;
  1774. char *src, *dst;
  1775. ret = buf->ops->confirm(pipe, buf);
  1776. if (ret)
  1777. goto out;
  1778. offset = sd->pos & ~PAGE_CACHE_MASK;
  1779. count = sd->len;
  1780. if (count + offset > PAGE_CACHE_SIZE)
  1781. count = PAGE_CACHE_SIZE - offset;
  1782. ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0,
  1783. &page, &fsdata);
  1784. if (ret) {
  1785. mlog_errno(ret);
  1786. goto out;
  1787. }
  1788. src = buf->ops->map(pipe, buf, 1);
  1789. dst = kmap_atomic(page, KM_USER1);
  1790. memcpy(dst + offset, src + buf->offset, count);
  1791. kunmap_atomic(page, KM_USER1);
  1792. buf->ops->unmap(pipe, buf, src);
  1793. copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count,
  1794. page, fsdata);
  1795. if (copied < 0) {
  1796. mlog_errno(copied);
  1797. ret = copied;
  1798. goto out;
  1799. }
  1800. out:
  1801. return copied ? copied : ret;
  1802. }
  1803. static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1804. struct file *out,
  1805. loff_t *ppos,
  1806. size_t len,
  1807. unsigned int flags)
  1808. {
  1809. int ret, err;
  1810. struct address_space *mapping = out->f_mapping;
  1811. struct inode *inode = mapping->host;
  1812. struct splice_desc sd = {
  1813. .total_len = len,
  1814. .flags = flags,
  1815. .pos = *ppos,
  1816. .u.file = out,
  1817. };
  1818. ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor);
  1819. if (ret > 0) {
  1820. *ppos += ret;
  1821. if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) {
  1822. err = generic_osync_inode(inode, mapping,
  1823. OSYNC_METADATA|OSYNC_DATA);
  1824. if (err)
  1825. ret = err;
  1826. }
  1827. }
  1828. return ret;
  1829. }
  1830. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1831. struct file *out,
  1832. loff_t *ppos,
  1833. size_t len,
  1834. unsigned int flags)
  1835. {
  1836. int ret;
  1837. struct inode *inode = out->f_path.dentry->d_inode;
  1838. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1839. (unsigned int)len,
  1840. out->f_path.dentry->d_name.len,
  1841. out->f_path.dentry->d_name.name);
  1842. inode_double_lock(inode, pipe->inode);
  1843. ret = ocfs2_rw_lock(inode, 1);
  1844. if (ret < 0) {
  1845. mlog_errno(ret);
  1846. goto out;
  1847. }
  1848. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1849. NULL);
  1850. if (ret < 0) {
  1851. mlog_errno(ret);
  1852. goto out_unlock;
  1853. }
  1854. /* ok, we're done with i_size and alloc work */
  1855. ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags);
  1856. out_unlock:
  1857. ocfs2_rw_unlock(inode, 1);
  1858. out:
  1859. inode_double_unlock(inode, pipe->inode);
  1860. mlog_exit(ret);
  1861. return ret;
  1862. }
  1863. static ssize_t ocfs2_file_splice_read(struct file *in,
  1864. loff_t *ppos,
  1865. struct pipe_inode_info *pipe,
  1866. size_t len,
  1867. unsigned int flags)
  1868. {
  1869. int ret = 0;
  1870. struct inode *inode = in->f_path.dentry->d_inode;
  1871. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1872. (unsigned int)len,
  1873. in->f_path.dentry->d_name.len,
  1874. in->f_path.dentry->d_name.name);
  1875. /*
  1876. * See the comment in ocfs2_file_aio_read()
  1877. */
  1878. ret = ocfs2_meta_lock(inode, NULL, 0);
  1879. if (ret < 0) {
  1880. mlog_errno(ret);
  1881. goto bail;
  1882. }
  1883. ocfs2_meta_unlock(inode, 0);
  1884. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1885. bail:
  1886. mlog_exit(ret);
  1887. return ret;
  1888. }
  1889. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1890. const struct iovec *iov,
  1891. unsigned long nr_segs,
  1892. loff_t pos)
  1893. {
  1894. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1895. struct file *filp = iocb->ki_filp;
  1896. struct inode *inode = filp->f_path.dentry->d_inode;
  1897. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1898. (unsigned int)nr_segs,
  1899. filp->f_path.dentry->d_name.len,
  1900. filp->f_path.dentry->d_name.name);
  1901. if (!inode) {
  1902. ret = -EINVAL;
  1903. mlog_errno(ret);
  1904. goto bail;
  1905. }
  1906. /*
  1907. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1908. * need locks to protect pending reads from racing with truncate.
  1909. */
  1910. if (filp->f_flags & O_DIRECT) {
  1911. down_read(&inode->i_alloc_sem);
  1912. have_alloc_sem = 1;
  1913. ret = ocfs2_rw_lock(inode, 0);
  1914. if (ret < 0) {
  1915. mlog_errno(ret);
  1916. goto bail;
  1917. }
  1918. rw_level = 0;
  1919. /* communicate with ocfs2_dio_end_io */
  1920. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1921. }
  1922. /*
  1923. * We're fine letting folks race truncates and extending
  1924. * writes with read across the cluster, just like they can
  1925. * locally. Hence no rw_lock during read.
  1926. *
  1927. * Take and drop the meta data lock to update inode fields
  1928. * like i_size. This allows the checks down below
  1929. * generic_file_aio_read() a chance of actually working.
  1930. */
  1931. ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1932. if (ret < 0) {
  1933. mlog_errno(ret);
  1934. goto bail;
  1935. }
  1936. ocfs2_meta_unlock(inode, lock_level);
  1937. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1938. if (ret == -EINVAL)
  1939. mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n");
  1940. /* buffered aio wouldn't have proper lock coverage today */
  1941. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1942. /* see ocfs2_file_aio_write */
  1943. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1944. rw_level = -1;
  1945. have_alloc_sem = 0;
  1946. }
  1947. bail:
  1948. if (have_alloc_sem)
  1949. up_read(&inode->i_alloc_sem);
  1950. if (rw_level != -1)
  1951. ocfs2_rw_unlock(inode, rw_level);
  1952. mlog_exit(ret);
  1953. return ret;
  1954. }
  1955. const struct inode_operations ocfs2_file_iops = {
  1956. .setattr = ocfs2_setattr,
  1957. .getattr = ocfs2_getattr,
  1958. .permission = ocfs2_permission,
  1959. };
  1960. const struct inode_operations ocfs2_special_file_iops = {
  1961. .setattr = ocfs2_setattr,
  1962. .getattr = ocfs2_getattr,
  1963. .permission = ocfs2_permission,
  1964. };
  1965. const struct file_operations ocfs2_fops = {
  1966. .read = do_sync_read,
  1967. .write = do_sync_write,
  1968. .mmap = ocfs2_mmap,
  1969. .fsync = ocfs2_sync_file,
  1970. .release = ocfs2_file_release,
  1971. .open = ocfs2_file_open,
  1972. .aio_read = ocfs2_file_aio_read,
  1973. .aio_write = ocfs2_file_aio_write,
  1974. .ioctl = ocfs2_ioctl,
  1975. #ifdef CONFIG_COMPAT
  1976. .compat_ioctl = ocfs2_compat_ioctl,
  1977. #endif
  1978. .splice_read = ocfs2_file_splice_read,
  1979. .splice_write = ocfs2_file_splice_write,
  1980. };
  1981. const struct file_operations ocfs2_dops = {
  1982. .read = generic_read_dir,
  1983. .readdir = ocfs2_readdir,
  1984. .fsync = ocfs2_sync_file,
  1985. .ioctl = ocfs2_ioctl,
  1986. #ifdef CONFIG_COMPAT
  1987. .compat_ioctl = ocfs2_compat_ioctl,
  1988. #endif
  1989. };