file.c 53 KB

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