file.c 57 KB

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