file.c 56 KB

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