file.c 56 KB

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