file.c 53 KB

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