xfs_vnodeops.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_itable.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_acl.h"
  45. #include "xfs_attr.h"
  46. #include "xfs_rw.h"
  47. #include "xfs_error.h"
  48. #include "xfs_quota.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_rtalloc.h"
  51. #include "xfs_trans_space.h"
  52. #include "xfs_log_priv.h"
  53. #include "xfs_filestream.h"
  54. #include "xfs_vnodeops.h"
  55. #include "xfs_trace.h"
  56. int
  57. xfs_setattr(
  58. struct xfs_inode *ip,
  59. struct iattr *iattr,
  60. int flags)
  61. {
  62. xfs_mount_t *mp = ip->i_mount;
  63. struct inode *inode = VFS_I(ip);
  64. int mask = iattr->ia_valid;
  65. xfs_trans_t *tp;
  66. int code;
  67. uint lock_flags;
  68. uint commit_flags=0;
  69. uid_t uid=0, iuid=0;
  70. gid_t gid=0, igid=0;
  71. struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
  72. int need_iolock = 1;
  73. xfs_itrace_entry(ip);
  74. if (mp->m_flags & XFS_MOUNT_RDONLY)
  75. return XFS_ERROR(EROFS);
  76. if (XFS_FORCED_SHUTDOWN(mp))
  77. return XFS_ERROR(EIO);
  78. code = -inode_change_ok(inode, iattr);
  79. if (code)
  80. return code;
  81. olddquot1 = olddquot2 = NULL;
  82. udqp = gdqp = NULL;
  83. /*
  84. * If disk quotas is on, we make sure that the dquots do exist on disk,
  85. * before we start any other transactions. Trying to do this later
  86. * is messy. We don't care to take a readlock to look at the ids
  87. * in inode here, because we can't hold it across the trans_reserve.
  88. * If the IDs do change before we take the ilock, we're covered
  89. * because the i_*dquot fields will get updated anyway.
  90. */
  91. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  92. uint qflags = 0;
  93. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  94. uid = iattr->ia_uid;
  95. qflags |= XFS_QMOPT_UQUOTA;
  96. } else {
  97. uid = ip->i_d.di_uid;
  98. }
  99. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  100. gid = iattr->ia_gid;
  101. qflags |= XFS_QMOPT_GQUOTA;
  102. } else {
  103. gid = ip->i_d.di_gid;
  104. }
  105. /*
  106. * We take a reference when we initialize udqp and gdqp,
  107. * so it is important that we never blindly double trip on
  108. * the same variable. See xfs_create() for an example.
  109. */
  110. ASSERT(udqp == NULL);
  111. ASSERT(gdqp == NULL);
  112. code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
  113. qflags, &udqp, &gdqp);
  114. if (code)
  115. return code;
  116. }
  117. /*
  118. * For the other attributes, we acquire the inode lock and
  119. * first do an error checking pass.
  120. */
  121. tp = NULL;
  122. lock_flags = XFS_ILOCK_EXCL;
  123. if (flags & XFS_ATTR_NOLOCK)
  124. need_iolock = 0;
  125. if (!(mask & ATTR_SIZE)) {
  126. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  127. commit_flags = 0;
  128. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp),
  129. 0, 0, 0);
  130. if (code) {
  131. lock_flags = 0;
  132. goto error_return;
  133. }
  134. } else {
  135. if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
  136. !(flags & XFS_ATTR_DMI)) {
  137. int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
  138. code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
  139. iattr->ia_size, 0, dmflags, NULL);
  140. if (code) {
  141. lock_flags = 0;
  142. goto error_return;
  143. }
  144. }
  145. if (need_iolock)
  146. lock_flags |= XFS_IOLOCK_EXCL;
  147. }
  148. xfs_ilock(ip, lock_flags);
  149. /*
  150. * Change file ownership. Must be the owner or privileged.
  151. */
  152. if (mask & (ATTR_UID|ATTR_GID)) {
  153. /*
  154. * These IDs could have changed since we last looked at them.
  155. * But, we're assured that if the ownership did change
  156. * while we didn't have the inode locked, inode's dquot(s)
  157. * would have changed also.
  158. */
  159. iuid = ip->i_d.di_uid;
  160. igid = ip->i_d.di_gid;
  161. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  162. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  163. /*
  164. * Do a quota reservation only if uid/gid is actually
  165. * going to change.
  166. */
  167. if (XFS_IS_QUOTA_RUNNING(mp) &&
  168. ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  169. (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
  170. ASSERT(tp);
  171. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  172. capable(CAP_FOWNER) ?
  173. XFS_QMOPT_FORCE_RES : 0);
  174. if (code) /* out of quota */
  175. goto error_return;
  176. }
  177. }
  178. /*
  179. * Truncate file. Must have write permission and not be a directory.
  180. */
  181. if (mask & ATTR_SIZE) {
  182. /* Short circuit the truncate case for zero length files */
  183. if (iattr->ia_size == 0 &&
  184. ip->i_size == 0 && ip->i_d.di_nextents == 0) {
  185. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  186. lock_flags &= ~XFS_ILOCK_EXCL;
  187. if (mask & ATTR_CTIME)
  188. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  189. code = 0;
  190. goto error_return;
  191. }
  192. if (S_ISDIR(ip->i_d.di_mode)) {
  193. code = XFS_ERROR(EISDIR);
  194. goto error_return;
  195. } else if (!S_ISREG(ip->i_d.di_mode)) {
  196. code = XFS_ERROR(EINVAL);
  197. goto error_return;
  198. }
  199. /*
  200. * Make sure that the dquots are attached to the inode.
  201. */
  202. code = xfs_qm_dqattach_locked(ip, 0);
  203. if (code)
  204. goto error_return;
  205. /*
  206. * Now we can make the changes. Before we join the inode
  207. * to the transaction, if ATTR_SIZE is set then take care of
  208. * the part of the truncation that must be done without the
  209. * inode lock. This needs to be done before joining the inode
  210. * to the transaction, because the inode cannot be unlocked
  211. * once it is a part of the transaction.
  212. */
  213. if (iattr->ia_size > ip->i_size) {
  214. /*
  215. * Do the first part of growing a file: zero any data
  216. * in the last block that is beyond the old EOF. We
  217. * need to do this before the inode is joined to the
  218. * transaction to modify the i_size.
  219. */
  220. code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
  221. }
  222. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  223. /*
  224. * We are going to log the inode size change in this
  225. * transaction so any previous writes that are beyond the on
  226. * disk EOF and the new EOF that have not been written out need
  227. * to be written here. If we do not write the data out, we
  228. * expose ourselves to the null files problem.
  229. *
  230. * Only flush from the on disk size to the smaller of the in
  231. * memory file size or the new size as that's the range we
  232. * really care about here and prevents waiting for other data
  233. * not within the range we care about here.
  234. */
  235. if (!code &&
  236. ip->i_size != ip->i_d.di_size &&
  237. iattr->ia_size > ip->i_d.di_size) {
  238. code = xfs_flush_pages(ip,
  239. ip->i_d.di_size, iattr->ia_size,
  240. XBF_ASYNC, FI_NONE);
  241. }
  242. /* wait for all I/O to complete */
  243. xfs_ioend_wait(ip);
  244. if (!code)
  245. code = xfs_itruncate_data(ip, iattr->ia_size);
  246. if (code) {
  247. ASSERT(tp == NULL);
  248. lock_flags &= ~XFS_ILOCK_EXCL;
  249. ASSERT(lock_flags == XFS_IOLOCK_EXCL);
  250. goto error_return;
  251. }
  252. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  253. if ((code = xfs_trans_reserve(tp, 0,
  254. XFS_ITRUNCATE_LOG_RES(mp), 0,
  255. XFS_TRANS_PERM_LOG_RES,
  256. XFS_ITRUNCATE_LOG_COUNT))) {
  257. xfs_trans_cancel(tp, 0);
  258. if (need_iolock)
  259. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  260. return code;
  261. }
  262. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  263. xfs_ilock(ip, XFS_ILOCK_EXCL);
  264. xfs_trans_ijoin(tp, ip, lock_flags);
  265. xfs_trans_ihold(tp, ip);
  266. /*
  267. * Only change the c/mtime if we are changing the size
  268. * or we are explicitly asked to change it. This handles
  269. * the semantic difference between truncate() and ftruncate()
  270. * as implemented in the VFS.
  271. *
  272. * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
  273. * is a special case where we need to update the times despite
  274. * not having these flags set. For all other operations the
  275. * VFS set these flags explicitly if it wants a timestamp
  276. * update.
  277. */
  278. if (iattr->ia_size != ip->i_size &&
  279. (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
  280. iattr->ia_ctime = iattr->ia_mtime =
  281. current_fs_time(inode->i_sb);
  282. mask |= ATTR_CTIME | ATTR_MTIME;
  283. }
  284. if (iattr->ia_size > ip->i_size) {
  285. ip->i_d.di_size = iattr->ia_size;
  286. ip->i_size = iattr->ia_size;
  287. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  288. } else if (iattr->ia_size <= ip->i_size ||
  289. (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
  290. /*
  291. * signal a sync transaction unless
  292. * we're truncating an already unlinked
  293. * file on a wsync filesystem
  294. */
  295. code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
  296. XFS_DATA_FORK,
  297. ((ip->i_d.di_nlink != 0 ||
  298. !(mp->m_flags & XFS_MOUNT_WSYNC))
  299. ? 1 : 0));
  300. if (code)
  301. goto abort_return;
  302. /*
  303. * Truncated "down", so we're removing references
  304. * to old data here - if we now delay flushing for
  305. * a long time, we expose ourselves unduly to the
  306. * notorious NULL files problem. So, we mark this
  307. * vnode and flush it when the file is closed, and
  308. * do not wait the usual (long) time for writeout.
  309. */
  310. xfs_iflags_set(ip, XFS_ITRUNCATED);
  311. }
  312. } else if (tp) {
  313. xfs_trans_ijoin(tp, ip, lock_flags);
  314. xfs_trans_ihold(tp, ip);
  315. }
  316. /*
  317. * Change file ownership. Must be the owner or privileged.
  318. */
  319. if (mask & (ATTR_UID|ATTR_GID)) {
  320. /*
  321. * CAP_FSETID overrides the following restrictions:
  322. *
  323. * The set-user-ID and set-group-ID bits of a file will be
  324. * cleared upon successful return from chown()
  325. */
  326. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  327. !capable(CAP_FSETID)) {
  328. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  329. }
  330. /*
  331. * Change the ownerships and register quota modifications
  332. * in the transaction.
  333. */
  334. if (iuid != uid) {
  335. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
  336. ASSERT(mask & ATTR_UID);
  337. ASSERT(udqp);
  338. olddquot1 = xfs_qm_vop_chown(tp, ip,
  339. &ip->i_udquot, udqp);
  340. }
  341. ip->i_d.di_uid = uid;
  342. inode->i_uid = uid;
  343. }
  344. if (igid != gid) {
  345. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
  346. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  347. ASSERT(mask & ATTR_GID);
  348. ASSERT(gdqp);
  349. olddquot2 = xfs_qm_vop_chown(tp, ip,
  350. &ip->i_gdquot, gdqp);
  351. }
  352. ip->i_d.di_gid = gid;
  353. inode->i_gid = gid;
  354. }
  355. }
  356. /*
  357. * Change file access modes.
  358. */
  359. if (mask & ATTR_MODE) {
  360. umode_t mode = iattr->ia_mode;
  361. if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
  362. mode &= ~S_ISGID;
  363. ip->i_d.di_mode &= S_IFMT;
  364. ip->i_d.di_mode |= mode & ~S_IFMT;
  365. inode->i_mode &= S_IFMT;
  366. inode->i_mode |= mode & ~S_IFMT;
  367. }
  368. /*
  369. * Change file access or modified times.
  370. */
  371. if (mask & ATTR_ATIME) {
  372. inode->i_atime = iattr->ia_atime;
  373. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  374. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  375. ip->i_update_core = 1;
  376. }
  377. if (mask & ATTR_CTIME) {
  378. inode->i_ctime = iattr->ia_ctime;
  379. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  380. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  381. ip->i_update_core = 1;
  382. }
  383. if (mask & ATTR_MTIME) {
  384. inode->i_mtime = iattr->ia_mtime;
  385. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  386. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  387. ip->i_update_core = 1;
  388. }
  389. /*
  390. * And finally, log the inode core if any attribute in it
  391. * has been changed.
  392. */
  393. if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE|
  394. ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
  395. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  396. XFS_STATS_INC(xs_ig_attrchg);
  397. /*
  398. * If this is a synchronous mount, make sure that the
  399. * transaction goes to disk before returning to the user.
  400. * This is slightly sub-optimal in that truncates require
  401. * two sync transactions instead of one for wsync filesystems.
  402. * One for the truncate and one for the timestamps since we
  403. * don't want to change the timestamps unless we're sure the
  404. * truncate worked. Truncates are less than 1% of the laddis
  405. * mix so this probably isn't worth the trouble to optimize.
  406. */
  407. code = 0;
  408. if (mp->m_flags & XFS_MOUNT_WSYNC)
  409. xfs_trans_set_sync(tp);
  410. code = xfs_trans_commit(tp, commit_flags);
  411. xfs_iunlock(ip, lock_flags);
  412. /*
  413. * Release any dquot(s) the inode had kept before chown.
  414. */
  415. xfs_qm_dqrele(olddquot1);
  416. xfs_qm_dqrele(olddquot2);
  417. xfs_qm_dqrele(udqp);
  418. xfs_qm_dqrele(gdqp);
  419. if (code)
  420. return code;
  421. /*
  422. * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
  423. * update. We could avoid this with linked transactions
  424. * and passing down the transaction pointer all the way
  425. * to attr_set. No previous user of the generic
  426. * Posix ACL code seems to care about this issue either.
  427. */
  428. if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
  429. code = -xfs_acl_chmod(inode);
  430. if (code)
  431. return XFS_ERROR(code);
  432. }
  433. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
  434. !(flags & XFS_ATTR_DMI)) {
  435. (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
  436. NULL, DM_RIGHT_NULL, NULL, NULL,
  437. 0, 0, AT_DELAY_FLAG(flags));
  438. }
  439. return 0;
  440. abort_return:
  441. commit_flags |= XFS_TRANS_ABORT;
  442. /* FALLTHROUGH */
  443. error_return:
  444. xfs_qm_dqrele(udqp);
  445. xfs_qm_dqrele(gdqp);
  446. if (tp) {
  447. xfs_trans_cancel(tp, commit_flags);
  448. }
  449. if (lock_flags != 0) {
  450. xfs_iunlock(ip, lock_flags);
  451. }
  452. return code;
  453. }
  454. /*
  455. * The maximum pathlen is 1024 bytes. Since the minimum file system
  456. * blocksize is 512 bytes, we can get a max of 2 extents back from
  457. * bmapi.
  458. */
  459. #define SYMLINK_MAPS 2
  460. STATIC int
  461. xfs_readlink_bmap(
  462. xfs_inode_t *ip,
  463. char *link)
  464. {
  465. xfs_mount_t *mp = ip->i_mount;
  466. int pathlen = ip->i_d.di_size;
  467. int nmaps = SYMLINK_MAPS;
  468. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  469. xfs_daddr_t d;
  470. int byte_cnt;
  471. int n;
  472. xfs_buf_t *bp;
  473. int error = 0;
  474. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
  475. mval, &nmaps, NULL, NULL);
  476. if (error)
  477. goto out;
  478. for (n = 0; n < nmaps; n++) {
  479. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  480. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  481. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
  482. XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
  483. error = XFS_BUF_GETERROR(bp);
  484. if (error) {
  485. xfs_ioerror_alert("xfs_readlink",
  486. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  487. xfs_buf_relse(bp);
  488. goto out;
  489. }
  490. if (pathlen < byte_cnt)
  491. byte_cnt = pathlen;
  492. pathlen -= byte_cnt;
  493. memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
  494. xfs_buf_relse(bp);
  495. }
  496. link[ip->i_d.di_size] = '\0';
  497. error = 0;
  498. out:
  499. return error;
  500. }
  501. int
  502. xfs_readlink(
  503. xfs_inode_t *ip,
  504. char *link)
  505. {
  506. xfs_mount_t *mp = ip->i_mount;
  507. int pathlen;
  508. int error = 0;
  509. xfs_itrace_entry(ip);
  510. if (XFS_FORCED_SHUTDOWN(mp))
  511. return XFS_ERROR(EIO);
  512. xfs_ilock(ip, XFS_ILOCK_SHARED);
  513. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  514. ASSERT(ip->i_d.di_size <= MAXPATHLEN);
  515. pathlen = ip->i_d.di_size;
  516. if (!pathlen)
  517. goto out;
  518. if (ip->i_df.if_flags & XFS_IFINLINE) {
  519. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  520. link[pathlen] = '\0';
  521. } else {
  522. error = xfs_readlink_bmap(ip, link);
  523. }
  524. out:
  525. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  526. return error;
  527. }
  528. /*
  529. * xfs_fsync
  530. *
  531. * This is called to sync the inode and its data out to disk. We need to hold
  532. * the I/O lock while flushing the data, and the inode lock while flushing the
  533. * inode. The inode lock CANNOT be held while flushing the data, so acquire
  534. * after we're done with that.
  535. */
  536. int
  537. xfs_fsync(
  538. xfs_inode_t *ip)
  539. {
  540. xfs_trans_t *tp;
  541. int error = 0;
  542. int log_flushed = 0;
  543. xfs_itrace_entry(ip);
  544. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  545. return XFS_ERROR(EIO);
  546. /*
  547. * We always need to make sure that the required inode state is safe on
  548. * disk. The inode might be clean but we still might need to force the
  549. * log because of committed transactions that haven't hit the disk yet.
  550. * Likewise, there could be unflushed non-transactional changes to the
  551. * inode core that have to go to disk and this requires us to issue
  552. * a synchronous transaction to capture these changes correctly.
  553. *
  554. * This code relies on the assumption that if the update_* fields
  555. * of the inode are clear and the inode is unpinned then it is clean
  556. * and no action is required.
  557. */
  558. xfs_ilock(ip, XFS_ILOCK_SHARED);
  559. if (!ip->i_update_core) {
  560. /*
  561. * Timestamps/size haven't changed since last inode flush or
  562. * inode transaction commit. That means either nothing got
  563. * written or a transaction committed which caught the updates.
  564. * If the latter happened and the transaction hasn't hit the
  565. * disk yet, the inode will be still be pinned. If it is,
  566. * force the log.
  567. */
  568. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  569. if (xfs_ipincount(ip)) {
  570. error = _xfs_log_force(ip->i_mount, XFS_LOG_SYNC,
  571. &log_flushed);
  572. }
  573. } else {
  574. /*
  575. * Kick off a transaction to log the inode core to get the
  576. * updates. The sync transaction will also force the log.
  577. */
  578. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  579. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  580. error = xfs_trans_reserve(tp, 0,
  581. XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
  582. if (error) {
  583. xfs_trans_cancel(tp, 0);
  584. return error;
  585. }
  586. xfs_ilock(ip, XFS_ILOCK_EXCL);
  587. /*
  588. * Note - it's possible that we might have pushed ourselves out
  589. * of the way during trans_reserve which would flush the inode.
  590. * But there's no guarantee that the inode buffer has actually
  591. * gone out yet (it's delwri). Plus the buffer could be pinned
  592. * anyway if it's part of an inode in another recent
  593. * transaction. So we play it safe and fire off the
  594. * transaction anyway.
  595. */
  596. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  597. xfs_trans_ihold(tp, ip);
  598. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  599. xfs_trans_set_sync(tp);
  600. error = _xfs_trans_commit(tp, 0, &log_flushed);
  601. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  602. }
  603. if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
  604. /*
  605. * If the log write didn't issue an ordered tag we need
  606. * to flush the disk cache for the data device now.
  607. */
  608. if (!log_flushed)
  609. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  610. /*
  611. * If this inode is on the RT dev we need to flush that
  612. * cache as well.
  613. */
  614. if (XFS_IS_REALTIME_INODE(ip))
  615. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  616. }
  617. return error;
  618. }
  619. /*
  620. * Flags for xfs_free_eofblocks
  621. */
  622. #define XFS_FREE_EOF_TRYLOCK (1<<0)
  623. /*
  624. * This is called by xfs_inactive to free any blocks beyond eof
  625. * when the link count isn't zero and by xfs_dm_punch_hole() when
  626. * punching a hole to EOF.
  627. */
  628. STATIC int
  629. xfs_free_eofblocks(
  630. xfs_mount_t *mp,
  631. xfs_inode_t *ip,
  632. int flags)
  633. {
  634. xfs_trans_t *tp;
  635. int error;
  636. xfs_fileoff_t end_fsb;
  637. xfs_fileoff_t last_fsb;
  638. xfs_filblks_t map_len;
  639. int nimaps;
  640. xfs_bmbt_irec_t imap;
  641. /*
  642. * Figure out if there are any blocks beyond the end
  643. * of the file. If not, then there is nothing to do.
  644. */
  645. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
  646. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  647. map_len = last_fsb - end_fsb;
  648. if (map_len <= 0)
  649. return 0;
  650. nimaps = 1;
  651. xfs_ilock(ip, XFS_ILOCK_SHARED);
  652. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  653. NULL, 0, &imap, &nimaps, NULL, NULL);
  654. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  655. if (!error && (nimaps != 0) &&
  656. (imap.br_startblock != HOLESTARTBLOCK ||
  657. ip->i_delayed_blks)) {
  658. /*
  659. * Attach the dquots to the inode up front.
  660. */
  661. error = xfs_qm_dqattach(ip, 0);
  662. if (error)
  663. return error;
  664. /*
  665. * There are blocks after the end of file.
  666. * Free them up now by truncating the file to
  667. * its current size.
  668. */
  669. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  670. /*
  671. * Do the xfs_itruncate_start() call before
  672. * reserving any log space because
  673. * itruncate_start will call into the buffer
  674. * cache and we can't
  675. * do that within a transaction.
  676. */
  677. if (flags & XFS_FREE_EOF_TRYLOCK) {
  678. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  679. xfs_trans_cancel(tp, 0);
  680. return 0;
  681. }
  682. } else {
  683. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  684. }
  685. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  686. ip->i_size);
  687. if (error) {
  688. xfs_trans_cancel(tp, 0);
  689. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  690. return error;
  691. }
  692. error = xfs_trans_reserve(tp, 0,
  693. XFS_ITRUNCATE_LOG_RES(mp),
  694. 0, XFS_TRANS_PERM_LOG_RES,
  695. XFS_ITRUNCATE_LOG_COUNT);
  696. if (error) {
  697. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  698. xfs_trans_cancel(tp, 0);
  699. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  700. return error;
  701. }
  702. xfs_ilock(ip, XFS_ILOCK_EXCL);
  703. xfs_trans_ijoin(tp, ip,
  704. XFS_IOLOCK_EXCL |
  705. XFS_ILOCK_EXCL);
  706. xfs_trans_ihold(tp, ip);
  707. error = xfs_itruncate_finish(&tp, ip,
  708. ip->i_size,
  709. XFS_DATA_FORK,
  710. 0);
  711. /*
  712. * If we get an error at this point we
  713. * simply don't bother truncating the file.
  714. */
  715. if (error) {
  716. xfs_trans_cancel(tp,
  717. (XFS_TRANS_RELEASE_LOG_RES |
  718. XFS_TRANS_ABORT));
  719. } else {
  720. error = xfs_trans_commit(tp,
  721. XFS_TRANS_RELEASE_LOG_RES);
  722. }
  723. xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
  724. }
  725. return error;
  726. }
  727. /*
  728. * Free a symlink that has blocks associated with it.
  729. */
  730. STATIC int
  731. xfs_inactive_symlink_rmt(
  732. xfs_inode_t *ip,
  733. xfs_trans_t **tpp)
  734. {
  735. xfs_buf_t *bp;
  736. int committed;
  737. int done;
  738. int error;
  739. xfs_fsblock_t first_block;
  740. xfs_bmap_free_t free_list;
  741. int i;
  742. xfs_mount_t *mp;
  743. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  744. int nmaps;
  745. xfs_trans_t *ntp;
  746. int size;
  747. xfs_trans_t *tp;
  748. tp = *tpp;
  749. mp = ip->i_mount;
  750. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  751. /*
  752. * We're freeing a symlink that has some
  753. * blocks allocated to it. Free the
  754. * blocks here. We know that we've got
  755. * either 1 or 2 extents and that we can
  756. * free them all in one bunmapi call.
  757. */
  758. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  759. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  760. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  761. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  762. xfs_trans_cancel(tp, 0);
  763. *tpp = NULL;
  764. return error;
  765. }
  766. /*
  767. * Lock the inode, fix the size, and join it to the transaction.
  768. * Hold it so in the normal path, we still have it locked for
  769. * the second transaction. In the error paths we need it
  770. * held so the cancel won't rele it, see below.
  771. */
  772. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  773. size = (int)ip->i_d.di_size;
  774. ip->i_d.di_size = 0;
  775. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  776. xfs_trans_ihold(tp, ip);
  777. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  778. /*
  779. * Find the block(s) so we can inval and unmap them.
  780. */
  781. done = 0;
  782. xfs_bmap_init(&free_list, &first_block);
  783. nmaps = ARRAY_SIZE(mval);
  784. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  785. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  786. &free_list, NULL)))
  787. goto error0;
  788. /*
  789. * Invalidate the block(s).
  790. */
  791. for (i = 0; i < nmaps; i++) {
  792. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  793. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  794. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  795. xfs_trans_binval(tp, bp);
  796. }
  797. /*
  798. * Unmap the dead block(s) to the free_list.
  799. */
  800. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  801. &first_block, &free_list, NULL, &done)))
  802. goto error1;
  803. ASSERT(done);
  804. /*
  805. * Commit the first transaction. This logs the EFI and the inode.
  806. */
  807. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  808. goto error1;
  809. /*
  810. * The transaction must have been committed, since there were
  811. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  812. * The new tp has the extent freeing and EFDs.
  813. */
  814. ASSERT(committed);
  815. /*
  816. * The first xact was committed, so add the inode to the new one.
  817. * Mark it dirty so it will be logged and moved forward in the log as
  818. * part of every commit.
  819. */
  820. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  821. xfs_trans_ihold(tp, ip);
  822. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  823. /*
  824. * Get a new, empty transaction to return to our caller.
  825. */
  826. ntp = xfs_trans_dup(tp);
  827. /*
  828. * Commit the transaction containing extent freeing and EFDs.
  829. * If we get an error on the commit here or on the reserve below,
  830. * we need to unlock the inode since the new transaction doesn't
  831. * have the inode attached.
  832. */
  833. error = xfs_trans_commit(tp, 0);
  834. tp = ntp;
  835. if (error) {
  836. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  837. goto error0;
  838. }
  839. /*
  840. * transaction commit worked ok so we can drop the extra ticket
  841. * reference that we gained in xfs_trans_dup()
  842. */
  843. xfs_log_ticket_put(tp->t_ticket);
  844. /*
  845. * Remove the memory for extent descriptions (just bookkeeping).
  846. */
  847. if (ip->i_df.if_bytes)
  848. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  849. ASSERT(ip->i_df.if_bytes == 0);
  850. /*
  851. * Put an itruncate log reservation in the new transaction
  852. * for our caller.
  853. */
  854. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  855. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  856. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  857. goto error0;
  858. }
  859. /*
  860. * Return with the inode locked but not joined to the transaction.
  861. */
  862. *tpp = tp;
  863. return 0;
  864. error1:
  865. xfs_bmap_cancel(&free_list);
  866. error0:
  867. /*
  868. * Have to come here with the inode locked and either
  869. * (held and in the transaction) or (not in the transaction).
  870. * If the inode isn't held then cancel would iput it, but
  871. * that's wrong since this is inactive and the vnode ref
  872. * count is 0 already.
  873. * Cancel won't do anything to the inode if held, but it still
  874. * needs to be locked until the cancel is done, if it was
  875. * joined to the transaction.
  876. */
  877. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  878. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  879. *tpp = NULL;
  880. return error;
  881. }
  882. STATIC int
  883. xfs_inactive_symlink_local(
  884. xfs_inode_t *ip,
  885. xfs_trans_t **tpp)
  886. {
  887. int error;
  888. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  889. /*
  890. * We're freeing a symlink which fit into
  891. * the inode. Just free the memory used
  892. * to hold the old symlink.
  893. */
  894. error = xfs_trans_reserve(*tpp, 0,
  895. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  896. 0, XFS_TRANS_PERM_LOG_RES,
  897. XFS_ITRUNCATE_LOG_COUNT);
  898. if (error) {
  899. xfs_trans_cancel(*tpp, 0);
  900. *tpp = NULL;
  901. return error;
  902. }
  903. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  904. /*
  905. * Zero length symlinks _can_ exist.
  906. */
  907. if (ip->i_df.if_bytes > 0) {
  908. xfs_idata_realloc(ip,
  909. -(ip->i_df.if_bytes),
  910. XFS_DATA_FORK);
  911. ASSERT(ip->i_df.if_bytes == 0);
  912. }
  913. return 0;
  914. }
  915. STATIC int
  916. xfs_inactive_attrs(
  917. xfs_inode_t *ip,
  918. xfs_trans_t **tpp)
  919. {
  920. xfs_trans_t *tp;
  921. int error;
  922. xfs_mount_t *mp;
  923. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  924. tp = *tpp;
  925. mp = ip->i_mount;
  926. ASSERT(ip->i_d.di_forkoff != 0);
  927. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  928. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  929. if (error)
  930. goto error_unlock;
  931. error = xfs_attr_inactive(ip);
  932. if (error)
  933. goto error_unlock;
  934. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  935. error = xfs_trans_reserve(tp, 0,
  936. XFS_IFREE_LOG_RES(mp),
  937. 0, XFS_TRANS_PERM_LOG_RES,
  938. XFS_INACTIVE_LOG_COUNT);
  939. if (error)
  940. goto error_cancel;
  941. xfs_ilock(ip, XFS_ILOCK_EXCL);
  942. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  943. xfs_trans_ihold(tp, ip);
  944. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  945. ASSERT(ip->i_d.di_anextents == 0);
  946. *tpp = tp;
  947. return 0;
  948. error_cancel:
  949. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  950. xfs_trans_cancel(tp, 0);
  951. error_unlock:
  952. *tpp = NULL;
  953. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  954. return error;
  955. }
  956. int
  957. xfs_release(
  958. xfs_inode_t *ip)
  959. {
  960. xfs_mount_t *mp = ip->i_mount;
  961. int error;
  962. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  963. return 0;
  964. /* If this is a read-only mount, don't do this (would generate I/O) */
  965. if (mp->m_flags & XFS_MOUNT_RDONLY)
  966. return 0;
  967. if (!XFS_FORCED_SHUTDOWN(mp)) {
  968. int truncated;
  969. /*
  970. * If we are using filestreams, and we have an unlinked
  971. * file that we are processing the last close on, then nothing
  972. * will be able to reopen and write to this file. Purge this
  973. * inode from the filestreams cache so that it doesn't delay
  974. * teardown of the inode.
  975. */
  976. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  977. xfs_filestream_deassociate(ip);
  978. /*
  979. * If we previously truncated this file and removed old data
  980. * in the process, we want to initiate "early" writeout on
  981. * the last close. This is an attempt to combat the notorious
  982. * NULL files problem which is particularly noticable from a
  983. * truncate down, buffered (re-)write (delalloc), followed by
  984. * a crash. What we are effectively doing here is
  985. * significantly reducing the time window where we'd otherwise
  986. * be exposed to that problem.
  987. */
  988. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  989. if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
  990. xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
  991. }
  992. if (ip->i_d.di_nlink != 0) {
  993. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  994. ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
  995. ip->i_delayed_blks > 0)) &&
  996. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  997. (!(ip->i_d.di_flags &
  998. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  999. /*
  1000. * If we can't get the iolock just skip truncating
  1001. * the blocks past EOF because we could deadlock
  1002. * with the mmap_sem otherwise. We'll get another
  1003. * chance to drop them once the last reference to
  1004. * the inode is dropped, so we'll never leak blocks
  1005. * permanently.
  1006. */
  1007. error = xfs_free_eofblocks(mp, ip,
  1008. XFS_FREE_EOF_TRYLOCK);
  1009. if (error)
  1010. return error;
  1011. }
  1012. }
  1013. return 0;
  1014. }
  1015. /*
  1016. * xfs_inactive
  1017. *
  1018. * This is called when the vnode reference count for the vnode
  1019. * goes to zero. If the file has been unlinked, then it must
  1020. * now be truncated. Also, we clear all of the read-ahead state
  1021. * kept for the inode here since the file is now closed.
  1022. */
  1023. int
  1024. xfs_inactive(
  1025. xfs_inode_t *ip)
  1026. {
  1027. xfs_bmap_free_t free_list;
  1028. xfs_fsblock_t first_block;
  1029. int committed;
  1030. xfs_trans_t *tp;
  1031. xfs_mount_t *mp;
  1032. int error;
  1033. int truncate;
  1034. xfs_itrace_entry(ip);
  1035. /*
  1036. * If the inode is already free, then there can be nothing
  1037. * to clean up here.
  1038. */
  1039. if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
  1040. ASSERT(ip->i_df.if_real_bytes == 0);
  1041. ASSERT(ip->i_df.if_broot_bytes == 0);
  1042. return VN_INACTIVE_CACHE;
  1043. }
  1044. /*
  1045. * Only do a truncate if it's a regular file with
  1046. * some actual space in it. It's OK to look at the
  1047. * inode's fields without the lock because we're the
  1048. * only one with a reference to the inode.
  1049. */
  1050. truncate = ((ip->i_d.di_nlink == 0) &&
  1051. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1052. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1053. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1054. mp = ip->i_mount;
  1055. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
  1056. XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
  1057. error = 0;
  1058. /* If this is a read-only mount, don't do this (would generate I/O) */
  1059. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1060. goto out;
  1061. if (ip->i_d.di_nlink != 0) {
  1062. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1063. ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
  1064. ip->i_delayed_blks > 0)) &&
  1065. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1066. (!(ip->i_d.di_flags &
  1067. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1068. (ip->i_delayed_blks != 0)))) {
  1069. error = xfs_free_eofblocks(mp, ip, 0);
  1070. if (error)
  1071. return VN_INACTIVE_CACHE;
  1072. }
  1073. goto out;
  1074. }
  1075. ASSERT(ip->i_d.di_nlink == 0);
  1076. error = xfs_qm_dqattach(ip, 0);
  1077. if (error)
  1078. return VN_INACTIVE_CACHE;
  1079. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1080. if (truncate) {
  1081. /*
  1082. * Do the xfs_itruncate_start() call before
  1083. * reserving any log space because itruncate_start
  1084. * will call into the buffer cache and we can't
  1085. * do that within a transaction.
  1086. */
  1087. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1088. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1089. if (error) {
  1090. xfs_trans_cancel(tp, 0);
  1091. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1092. return VN_INACTIVE_CACHE;
  1093. }
  1094. error = xfs_trans_reserve(tp, 0,
  1095. XFS_ITRUNCATE_LOG_RES(mp),
  1096. 0, XFS_TRANS_PERM_LOG_RES,
  1097. XFS_ITRUNCATE_LOG_COUNT);
  1098. if (error) {
  1099. /* Don't call itruncate_cleanup */
  1100. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1101. xfs_trans_cancel(tp, 0);
  1102. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1103. return VN_INACTIVE_CACHE;
  1104. }
  1105. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1106. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1107. xfs_trans_ihold(tp, ip);
  1108. /*
  1109. * normally, we have to run xfs_itruncate_finish sync.
  1110. * But if filesystem is wsync and we're in the inactive
  1111. * path, then we know that nlink == 0, and that the
  1112. * xaction that made nlink == 0 is permanently committed
  1113. * since xfs_remove runs as a synchronous transaction.
  1114. */
  1115. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1116. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1117. if (error) {
  1118. xfs_trans_cancel(tp,
  1119. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1120. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1121. return VN_INACTIVE_CACHE;
  1122. }
  1123. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1124. /*
  1125. * If we get an error while cleaning up a
  1126. * symlink we bail out.
  1127. */
  1128. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1129. xfs_inactive_symlink_rmt(ip, &tp) :
  1130. xfs_inactive_symlink_local(ip, &tp);
  1131. if (error) {
  1132. ASSERT(tp == NULL);
  1133. return VN_INACTIVE_CACHE;
  1134. }
  1135. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1136. xfs_trans_ihold(tp, ip);
  1137. } else {
  1138. error = xfs_trans_reserve(tp, 0,
  1139. XFS_IFREE_LOG_RES(mp),
  1140. 0, XFS_TRANS_PERM_LOG_RES,
  1141. XFS_INACTIVE_LOG_COUNT);
  1142. if (error) {
  1143. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1144. xfs_trans_cancel(tp, 0);
  1145. return VN_INACTIVE_CACHE;
  1146. }
  1147. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1148. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1149. xfs_trans_ihold(tp, ip);
  1150. }
  1151. /*
  1152. * If there are attributes associated with the file
  1153. * then blow them away now. The code calls a routine
  1154. * that recursively deconstructs the attribute fork.
  1155. * We need to just commit the current transaction
  1156. * because we can't use it for xfs_attr_inactive().
  1157. */
  1158. if (ip->i_d.di_anextents > 0) {
  1159. error = xfs_inactive_attrs(ip, &tp);
  1160. /*
  1161. * If we got an error, the transaction is already
  1162. * cancelled, and the inode is unlocked. Just get out.
  1163. */
  1164. if (error)
  1165. return VN_INACTIVE_CACHE;
  1166. } else if (ip->i_afp) {
  1167. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1168. }
  1169. /*
  1170. * Free the inode.
  1171. */
  1172. xfs_bmap_init(&free_list, &first_block);
  1173. error = xfs_ifree(tp, ip, &free_list);
  1174. if (error) {
  1175. /*
  1176. * If we fail to free the inode, shut down. The cancel
  1177. * might do that, we need to make sure. Otherwise the
  1178. * inode might be lost for a long time or forever.
  1179. */
  1180. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1181. cmn_err(CE_NOTE,
  1182. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1183. error, mp->m_fsname);
  1184. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1185. }
  1186. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1187. } else {
  1188. /*
  1189. * Credit the quota account(s). The inode is gone.
  1190. */
  1191. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1192. /*
  1193. * Just ignore errors at this point. There is nothing we can
  1194. * do except to try to keep going. Make sure it's not a silent
  1195. * error.
  1196. */
  1197. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1198. if (error)
  1199. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1200. "xfs_bmap_finish() returned error %d", error);
  1201. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1202. if (error)
  1203. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1204. "xfs_trans_commit() returned error %d", error);
  1205. }
  1206. /*
  1207. * Release the dquots held by inode, if any.
  1208. */
  1209. xfs_qm_dqdetach(ip);
  1210. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1211. out:
  1212. return VN_INACTIVE_CACHE;
  1213. }
  1214. /*
  1215. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  1216. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  1217. * ci_name->name will point to a the actual name (caller must free) or
  1218. * will be set to NULL if an exact match is found.
  1219. */
  1220. int
  1221. xfs_lookup(
  1222. xfs_inode_t *dp,
  1223. struct xfs_name *name,
  1224. xfs_inode_t **ipp,
  1225. struct xfs_name *ci_name)
  1226. {
  1227. xfs_ino_t inum;
  1228. int error;
  1229. uint lock_mode;
  1230. xfs_itrace_entry(dp);
  1231. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1232. return XFS_ERROR(EIO);
  1233. lock_mode = xfs_ilock_map_shared(dp);
  1234. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  1235. xfs_iunlock_map_shared(dp, lock_mode);
  1236. if (error)
  1237. goto out;
  1238. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
  1239. if (error)
  1240. goto out_free_name;
  1241. return 0;
  1242. out_free_name:
  1243. if (ci_name)
  1244. kmem_free(ci_name->name);
  1245. out:
  1246. *ipp = NULL;
  1247. return error;
  1248. }
  1249. int
  1250. xfs_create(
  1251. xfs_inode_t *dp,
  1252. struct xfs_name *name,
  1253. mode_t mode,
  1254. xfs_dev_t rdev,
  1255. xfs_inode_t **ipp,
  1256. cred_t *credp)
  1257. {
  1258. int is_dir = S_ISDIR(mode);
  1259. struct xfs_mount *mp = dp->i_mount;
  1260. struct xfs_inode *ip = NULL;
  1261. struct xfs_trans *tp = NULL;
  1262. int error;
  1263. xfs_bmap_free_t free_list;
  1264. xfs_fsblock_t first_block;
  1265. boolean_t unlock_dp_on_error = B_FALSE;
  1266. uint cancel_flags;
  1267. int committed;
  1268. xfs_prid_t prid;
  1269. struct xfs_dquot *udqp = NULL;
  1270. struct xfs_dquot *gdqp = NULL;
  1271. uint resblks;
  1272. uint log_res;
  1273. uint log_count;
  1274. xfs_itrace_entry(dp);
  1275. if (XFS_FORCED_SHUTDOWN(mp))
  1276. return XFS_ERROR(EIO);
  1277. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1278. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1279. dp, DM_RIGHT_NULL, NULL,
  1280. DM_RIGHT_NULL, name->name, NULL,
  1281. mode, 0, 0);
  1282. if (error)
  1283. return error;
  1284. }
  1285. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1286. prid = dp->i_d.di_projid;
  1287. else
  1288. prid = dfltprid;
  1289. /*
  1290. * Make sure that we have allocated dquot(s) on disk.
  1291. */
  1292. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1293. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1294. if (error)
  1295. goto std_return;
  1296. if (is_dir) {
  1297. rdev = 0;
  1298. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1299. log_res = XFS_MKDIR_LOG_RES(mp);
  1300. log_count = XFS_MKDIR_LOG_COUNT;
  1301. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  1302. } else {
  1303. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1304. log_res = XFS_CREATE_LOG_RES(mp);
  1305. log_count = XFS_CREATE_LOG_COUNT;
  1306. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1307. }
  1308. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1309. /*
  1310. * Initially assume that the file does not exist and
  1311. * reserve the resources for that case. If that is not
  1312. * the case we'll drop the one we have and get a more
  1313. * appropriate transaction later.
  1314. */
  1315. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  1316. XFS_TRANS_PERM_LOG_RES, log_count);
  1317. if (error == ENOSPC) {
  1318. /* flush outstanding delalloc blocks and retry */
  1319. xfs_flush_inodes(dp);
  1320. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  1321. XFS_TRANS_PERM_LOG_RES, log_count);
  1322. }
  1323. if (error == ENOSPC) {
  1324. /* No space at all so try a "no-allocation" reservation */
  1325. resblks = 0;
  1326. error = xfs_trans_reserve(tp, 0, log_res, 0,
  1327. XFS_TRANS_PERM_LOG_RES, log_count);
  1328. }
  1329. if (error) {
  1330. cancel_flags = 0;
  1331. goto out_trans_cancel;
  1332. }
  1333. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1334. unlock_dp_on_error = B_TRUE;
  1335. /*
  1336. * Check for directory link count overflow.
  1337. */
  1338. if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
  1339. error = XFS_ERROR(EMLINK);
  1340. goto out_trans_cancel;
  1341. }
  1342. xfs_bmap_init(&free_list, &first_block);
  1343. /*
  1344. * Reserve disk quota and the inode.
  1345. */
  1346. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  1347. if (error)
  1348. goto out_trans_cancel;
  1349. error = xfs_dir_canenter(tp, dp, name, resblks);
  1350. if (error)
  1351. goto out_trans_cancel;
  1352. /*
  1353. * A newly created regular or special file just has one directory
  1354. * entry pointing to them, but a directory also the "." entry
  1355. * pointing to itself.
  1356. */
  1357. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
  1358. prid, resblks > 0, &ip, &committed);
  1359. if (error) {
  1360. if (error == ENOSPC)
  1361. goto out_trans_cancel;
  1362. goto out_trans_abort;
  1363. }
  1364. /*
  1365. * At this point, we've gotten a newly allocated inode.
  1366. * It is locked (and joined to the transaction).
  1367. */
  1368. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1369. /*
  1370. * Now we join the directory inode to the transaction. We do not do it
  1371. * earlier because xfs_dir_ialloc might commit the previous transaction
  1372. * (and release all the locks). An error from here on will result in
  1373. * the transaction cancel unlocking dp so don't do it explicitly in the
  1374. * error path.
  1375. */
  1376. IHOLD(dp);
  1377. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1378. unlock_dp_on_error = B_FALSE;
  1379. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1380. &first_block, &free_list, resblks ?
  1381. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1382. if (error) {
  1383. ASSERT(error != ENOSPC);
  1384. goto out_trans_abort;
  1385. }
  1386. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1387. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1388. if (is_dir) {
  1389. error = xfs_dir_init(tp, ip, dp);
  1390. if (error)
  1391. goto out_bmap_cancel;
  1392. error = xfs_bumplink(tp, dp);
  1393. if (error)
  1394. goto out_bmap_cancel;
  1395. }
  1396. /*
  1397. * If this is a synchronous mount, make sure that the
  1398. * create transaction goes to disk before returning to
  1399. * the user.
  1400. */
  1401. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1402. xfs_trans_set_sync(tp);
  1403. /*
  1404. * Attach the dquot(s) to the inodes and modify them incore.
  1405. * These ids of the inode couldn't have changed since the new
  1406. * inode has been locked ever since it was created.
  1407. */
  1408. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  1409. /*
  1410. * xfs_trans_commit normally decrements the vnode ref count
  1411. * when it unlocks the inode. Since we want to return the
  1412. * vnode to the caller, we bump the vnode ref count now.
  1413. */
  1414. IHOLD(ip);
  1415. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1416. if (error)
  1417. goto out_abort_rele;
  1418. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1419. if (error) {
  1420. IRELE(ip);
  1421. goto out_dqrele;
  1422. }
  1423. xfs_qm_dqrele(udqp);
  1424. xfs_qm_dqrele(gdqp);
  1425. *ipp = ip;
  1426. /* Fallthrough to std_return with error = 0 */
  1427. std_return:
  1428. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1429. XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
  1430. ip, DM_RIGHT_NULL, name->name, NULL, mode,
  1431. error, 0);
  1432. }
  1433. return error;
  1434. out_bmap_cancel:
  1435. xfs_bmap_cancel(&free_list);
  1436. out_trans_abort:
  1437. cancel_flags |= XFS_TRANS_ABORT;
  1438. out_trans_cancel:
  1439. xfs_trans_cancel(tp, cancel_flags);
  1440. out_dqrele:
  1441. xfs_qm_dqrele(udqp);
  1442. xfs_qm_dqrele(gdqp);
  1443. if (unlock_dp_on_error)
  1444. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1445. goto std_return;
  1446. out_abort_rele:
  1447. /*
  1448. * Wait until after the current transaction is aborted to
  1449. * release the inode. This prevents recursive transactions
  1450. * and deadlocks from xfs_inactive.
  1451. */
  1452. xfs_bmap_cancel(&free_list);
  1453. cancel_flags |= XFS_TRANS_ABORT;
  1454. xfs_trans_cancel(tp, cancel_flags);
  1455. IRELE(ip);
  1456. unlock_dp_on_error = B_FALSE;
  1457. goto out_dqrele;
  1458. }
  1459. #ifdef DEBUG
  1460. int xfs_locked_n;
  1461. int xfs_small_retries;
  1462. int xfs_middle_retries;
  1463. int xfs_lots_retries;
  1464. int xfs_lock_delays;
  1465. #endif
  1466. /*
  1467. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1468. * a different value
  1469. */
  1470. static inline int
  1471. xfs_lock_inumorder(int lock_mode, int subclass)
  1472. {
  1473. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1474. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1475. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1476. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1477. return lock_mode;
  1478. }
  1479. /*
  1480. * The following routine will lock n inodes in exclusive mode.
  1481. * We assume the caller calls us with the inodes in i_ino order.
  1482. *
  1483. * We need to detect deadlock where an inode that we lock
  1484. * is in the AIL and we start waiting for another inode that is locked
  1485. * by a thread in a long running transaction (such as truncate). This can
  1486. * result in deadlock since the long running trans might need to wait
  1487. * for the inode we just locked in order to push the tail and free space
  1488. * in the log.
  1489. */
  1490. void
  1491. xfs_lock_inodes(
  1492. xfs_inode_t **ips,
  1493. int inodes,
  1494. uint lock_mode)
  1495. {
  1496. int attempts = 0, i, j, try_lock;
  1497. xfs_log_item_t *lp;
  1498. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1499. try_lock = 0;
  1500. i = 0;
  1501. again:
  1502. for (; i < inodes; i++) {
  1503. ASSERT(ips[i]);
  1504. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1505. continue;
  1506. /*
  1507. * If try_lock is not set yet, make sure all locked inodes
  1508. * are not in the AIL.
  1509. * If any are, set try_lock to be used later.
  1510. */
  1511. if (!try_lock) {
  1512. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1513. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1514. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1515. try_lock++;
  1516. }
  1517. }
  1518. }
  1519. /*
  1520. * If any of the previous locks we have locked is in the AIL,
  1521. * we must TRY to get the second and subsequent locks. If
  1522. * we can't get any, we must release all we have
  1523. * and try again.
  1524. */
  1525. if (try_lock) {
  1526. /* try_lock must be 0 if i is 0. */
  1527. /*
  1528. * try_lock means we have an inode locked
  1529. * that is in the AIL.
  1530. */
  1531. ASSERT(i != 0);
  1532. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  1533. attempts++;
  1534. /*
  1535. * Unlock all previous guys and try again.
  1536. * xfs_iunlock will try to push the tail
  1537. * if the inode is in the AIL.
  1538. */
  1539. for(j = i - 1; j >= 0; j--) {
  1540. /*
  1541. * Check to see if we've already
  1542. * unlocked this one.
  1543. * Not the first one going back,
  1544. * and the inode ptr is the same.
  1545. */
  1546. if ((j != (i - 1)) && ips[j] ==
  1547. ips[j+1])
  1548. continue;
  1549. xfs_iunlock(ips[j], lock_mode);
  1550. }
  1551. if ((attempts % 5) == 0) {
  1552. delay(1); /* Don't just spin the CPU */
  1553. #ifdef DEBUG
  1554. xfs_lock_delays++;
  1555. #endif
  1556. }
  1557. i = 0;
  1558. try_lock = 0;
  1559. goto again;
  1560. }
  1561. } else {
  1562. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  1563. }
  1564. }
  1565. #ifdef DEBUG
  1566. if (attempts) {
  1567. if (attempts < 5) xfs_small_retries++;
  1568. else if (attempts < 100) xfs_middle_retries++;
  1569. else xfs_lots_retries++;
  1570. } else {
  1571. xfs_locked_n++;
  1572. }
  1573. #endif
  1574. }
  1575. /*
  1576. * xfs_lock_two_inodes() can only be used to lock one type of lock
  1577. * at a time - the iolock or the ilock, but not both at once. If
  1578. * we lock both at once, lockdep will report false positives saying
  1579. * we have violated locking orders.
  1580. */
  1581. void
  1582. xfs_lock_two_inodes(
  1583. xfs_inode_t *ip0,
  1584. xfs_inode_t *ip1,
  1585. uint lock_mode)
  1586. {
  1587. xfs_inode_t *temp;
  1588. int attempts = 0;
  1589. xfs_log_item_t *lp;
  1590. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1591. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  1592. ASSERT(ip0->i_ino != ip1->i_ino);
  1593. if (ip0->i_ino > ip1->i_ino) {
  1594. temp = ip0;
  1595. ip0 = ip1;
  1596. ip1 = temp;
  1597. }
  1598. again:
  1599. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  1600. /*
  1601. * If the first lock we have locked is in the AIL, we must TRY to get
  1602. * the second lock. If we can't get it, we must release the first one
  1603. * and try again.
  1604. */
  1605. lp = (xfs_log_item_t *)ip0->i_itemp;
  1606. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1607. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  1608. xfs_iunlock(ip0, lock_mode);
  1609. if ((++attempts % 5) == 0)
  1610. delay(1); /* Don't just spin the CPU */
  1611. goto again;
  1612. }
  1613. } else {
  1614. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  1615. }
  1616. }
  1617. int
  1618. xfs_remove(
  1619. xfs_inode_t *dp,
  1620. struct xfs_name *name,
  1621. xfs_inode_t *ip)
  1622. {
  1623. xfs_mount_t *mp = dp->i_mount;
  1624. xfs_trans_t *tp = NULL;
  1625. int is_dir = S_ISDIR(ip->i_d.di_mode);
  1626. int error = 0;
  1627. xfs_bmap_free_t free_list;
  1628. xfs_fsblock_t first_block;
  1629. int cancel_flags;
  1630. int committed;
  1631. int link_zero;
  1632. uint resblks;
  1633. uint log_count;
  1634. xfs_itrace_entry(dp);
  1635. xfs_itrace_entry(ip);
  1636. if (XFS_FORCED_SHUTDOWN(mp))
  1637. return XFS_ERROR(EIO);
  1638. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  1639. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
  1640. NULL, DM_RIGHT_NULL, name->name, NULL,
  1641. ip->i_d.di_mode, 0, 0);
  1642. if (error)
  1643. return error;
  1644. }
  1645. error = xfs_qm_dqattach(dp, 0);
  1646. if (error)
  1647. goto std_return;
  1648. error = xfs_qm_dqattach(ip, 0);
  1649. if (error)
  1650. goto std_return;
  1651. if (is_dir) {
  1652. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  1653. log_count = XFS_DEFAULT_LOG_COUNT;
  1654. } else {
  1655. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  1656. log_count = XFS_REMOVE_LOG_COUNT;
  1657. }
  1658. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1659. /*
  1660. * We try to get the real space reservation first,
  1661. * allowing for directory btree deletion(s) implying
  1662. * possible bmap insert(s). If we can't get the space
  1663. * reservation then we use 0 instead, and avoid the bmap
  1664. * btree insert(s) in the directory code by, if the bmap
  1665. * insert tries to happen, instead trimming the LAST
  1666. * block from the directory.
  1667. */
  1668. resblks = XFS_REMOVE_SPACE_RES(mp);
  1669. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  1670. XFS_TRANS_PERM_LOG_RES, log_count);
  1671. if (error == ENOSPC) {
  1672. resblks = 0;
  1673. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  1674. XFS_TRANS_PERM_LOG_RES, log_count);
  1675. }
  1676. if (error) {
  1677. ASSERT(error != ENOSPC);
  1678. cancel_flags = 0;
  1679. goto out_trans_cancel;
  1680. }
  1681. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  1682. /*
  1683. * At this point, we've gotten both the directory and the entry
  1684. * inodes locked.
  1685. */
  1686. IHOLD(ip);
  1687. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1688. IHOLD(dp);
  1689. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1690. /*
  1691. * If we're removing a directory perform some additional validation.
  1692. */
  1693. if (is_dir) {
  1694. ASSERT(ip->i_d.di_nlink >= 2);
  1695. if (ip->i_d.di_nlink != 2) {
  1696. error = XFS_ERROR(ENOTEMPTY);
  1697. goto out_trans_cancel;
  1698. }
  1699. if (!xfs_dir_isempty(ip)) {
  1700. error = XFS_ERROR(ENOTEMPTY);
  1701. goto out_trans_cancel;
  1702. }
  1703. }
  1704. xfs_bmap_init(&free_list, &first_block);
  1705. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  1706. &first_block, &free_list, resblks);
  1707. if (error) {
  1708. ASSERT(error != ENOENT);
  1709. goto out_bmap_cancel;
  1710. }
  1711. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1712. if (is_dir) {
  1713. /*
  1714. * Drop the link from ip's "..".
  1715. */
  1716. error = xfs_droplink(tp, dp);
  1717. if (error)
  1718. goto out_bmap_cancel;
  1719. /*
  1720. * Drop the "." link from ip to self.
  1721. */
  1722. error = xfs_droplink(tp, ip);
  1723. if (error)
  1724. goto out_bmap_cancel;
  1725. } else {
  1726. /*
  1727. * When removing a non-directory we need to log the parent
  1728. * inode here. For a directory this is done implicitly
  1729. * by the xfs_droplink call for the ".." entry.
  1730. */
  1731. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1732. }
  1733. /*
  1734. * Drop the link from dp to ip.
  1735. */
  1736. error = xfs_droplink(tp, ip);
  1737. if (error)
  1738. goto out_bmap_cancel;
  1739. /*
  1740. * Determine if this is the last link while
  1741. * we are in the transaction.
  1742. */
  1743. link_zero = (ip->i_d.di_nlink == 0);
  1744. /*
  1745. * If this is a synchronous mount, make sure that the
  1746. * remove transaction goes to disk before returning to
  1747. * the user.
  1748. */
  1749. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1750. xfs_trans_set_sync(tp);
  1751. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1752. if (error)
  1753. goto out_bmap_cancel;
  1754. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1755. if (error)
  1756. goto std_return;
  1757. /*
  1758. * If we are using filestreams, kill the stream association.
  1759. * If the file is still open it may get a new one but that
  1760. * will get killed on last close in xfs_close() so we don't
  1761. * have to worry about that.
  1762. */
  1763. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  1764. xfs_filestream_deassociate(ip);
  1765. std_return:
  1766. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  1767. XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
  1768. NULL, DM_RIGHT_NULL, name->name, NULL,
  1769. ip->i_d.di_mode, error, 0);
  1770. }
  1771. return error;
  1772. out_bmap_cancel:
  1773. xfs_bmap_cancel(&free_list);
  1774. cancel_flags |= XFS_TRANS_ABORT;
  1775. out_trans_cancel:
  1776. xfs_trans_cancel(tp, cancel_flags);
  1777. goto std_return;
  1778. }
  1779. int
  1780. xfs_link(
  1781. xfs_inode_t *tdp,
  1782. xfs_inode_t *sip,
  1783. struct xfs_name *target_name)
  1784. {
  1785. xfs_mount_t *mp = tdp->i_mount;
  1786. xfs_trans_t *tp;
  1787. int error;
  1788. xfs_bmap_free_t free_list;
  1789. xfs_fsblock_t first_block;
  1790. int cancel_flags;
  1791. int committed;
  1792. int resblks;
  1793. xfs_itrace_entry(tdp);
  1794. xfs_itrace_entry(sip);
  1795. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1796. if (XFS_FORCED_SHUTDOWN(mp))
  1797. return XFS_ERROR(EIO);
  1798. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  1799. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  1800. tdp, DM_RIGHT_NULL,
  1801. sip, DM_RIGHT_NULL,
  1802. target_name->name, NULL, 0, 0, 0);
  1803. if (error)
  1804. return error;
  1805. }
  1806. /* Return through std_return after this point. */
  1807. error = xfs_qm_dqattach(sip, 0);
  1808. if (error)
  1809. goto std_return;
  1810. error = xfs_qm_dqattach(tdp, 0);
  1811. if (error)
  1812. goto std_return;
  1813. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1814. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1815. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1816. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  1817. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1818. if (error == ENOSPC) {
  1819. resblks = 0;
  1820. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  1821. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1822. }
  1823. if (error) {
  1824. cancel_flags = 0;
  1825. goto error_return;
  1826. }
  1827. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1828. /*
  1829. * Increment vnode ref counts since xfs_trans_commit &
  1830. * xfs_trans_cancel will both unlock the inodes and
  1831. * decrement the associated ref counts.
  1832. */
  1833. IHOLD(sip);
  1834. IHOLD(tdp);
  1835. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1836. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1837. /*
  1838. * If the source has too many links, we can't make any more to it.
  1839. */
  1840. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  1841. error = XFS_ERROR(EMLINK);
  1842. goto error_return;
  1843. }
  1844. /*
  1845. * If we are using project inheritance, we only allow hard link
  1846. * creation in our tree when the project IDs are the same; else
  1847. * the tree quota mechanism could be circumvented.
  1848. */
  1849. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1850. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  1851. error = XFS_ERROR(EXDEV);
  1852. goto error_return;
  1853. }
  1854. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  1855. if (error)
  1856. goto error_return;
  1857. xfs_bmap_init(&free_list, &first_block);
  1858. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1859. &first_block, &free_list, resblks);
  1860. if (error)
  1861. goto abort_return;
  1862. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1863. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1864. error = xfs_bumplink(tp, sip);
  1865. if (error)
  1866. goto abort_return;
  1867. /*
  1868. * If this is a synchronous mount, make sure that the
  1869. * link transaction goes to disk before returning to
  1870. * the user.
  1871. */
  1872. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1873. xfs_trans_set_sync(tp);
  1874. }
  1875. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1876. if (error) {
  1877. xfs_bmap_cancel(&free_list);
  1878. goto abort_return;
  1879. }
  1880. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1881. if (error)
  1882. goto std_return;
  1883. /* Fall through to std_return with error = 0. */
  1884. std_return:
  1885. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  1886. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  1887. tdp, DM_RIGHT_NULL,
  1888. sip, DM_RIGHT_NULL,
  1889. target_name->name, NULL, 0, error, 0);
  1890. }
  1891. return error;
  1892. abort_return:
  1893. cancel_flags |= XFS_TRANS_ABORT;
  1894. /* FALLTHROUGH */
  1895. error_return:
  1896. xfs_trans_cancel(tp, cancel_flags);
  1897. goto std_return;
  1898. }
  1899. int
  1900. xfs_symlink(
  1901. xfs_inode_t *dp,
  1902. struct xfs_name *link_name,
  1903. const char *target_path,
  1904. mode_t mode,
  1905. xfs_inode_t **ipp,
  1906. cred_t *credp)
  1907. {
  1908. xfs_mount_t *mp = dp->i_mount;
  1909. xfs_trans_t *tp;
  1910. xfs_inode_t *ip;
  1911. int error;
  1912. int pathlen;
  1913. xfs_bmap_free_t free_list;
  1914. xfs_fsblock_t first_block;
  1915. boolean_t unlock_dp_on_error = B_FALSE;
  1916. uint cancel_flags;
  1917. int committed;
  1918. xfs_fileoff_t first_fsb;
  1919. xfs_filblks_t fs_blocks;
  1920. int nmaps;
  1921. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1922. xfs_daddr_t d;
  1923. const char *cur_chunk;
  1924. int byte_cnt;
  1925. int n;
  1926. xfs_buf_t *bp;
  1927. xfs_prid_t prid;
  1928. struct xfs_dquot *udqp, *gdqp;
  1929. uint resblks;
  1930. *ipp = NULL;
  1931. error = 0;
  1932. ip = NULL;
  1933. tp = NULL;
  1934. xfs_itrace_entry(dp);
  1935. if (XFS_FORCED_SHUTDOWN(mp))
  1936. return XFS_ERROR(EIO);
  1937. /*
  1938. * Check component lengths of the target path name.
  1939. */
  1940. pathlen = strlen(target_path);
  1941. if (pathlen >= MAXPATHLEN) /* total string too long */
  1942. return XFS_ERROR(ENAMETOOLONG);
  1943. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  1944. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
  1945. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  1946. link_name->name,
  1947. (unsigned char *)target_path, 0, 0, 0);
  1948. if (error)
  1949. return error;
  1950. }
  1951. /* Return through std_return after this point. */
  1952. udqp = gdqp = NULL;
  1953. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1954. prid = dp->i_d.di_projid;
  1955. else
  1956. prid = (xfs_prid_t)dfltprid;
  1957. /*
  1958. * Make sure that we have allocated dquot(s) on disk.
  1959. */
  1960. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1961. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1962. if (error)
  1963. goto std_return;
  1964. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  1965. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1966. /*
  1967. * The symlink will fit into the inode data fork?
  1968. * There can't be any attributes so we get the whole variable part.
  1969. */
  1970. if (pathlen <= XFS_LITINO(mp))
  1971. fs_blocks = 0;
  1972. else
  1973. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  1974. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  1975. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  1976. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  1977. if (error == ENOSPC && fs_blocks == 0) {
  1978. resblks = 0;
  1979. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  1980. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  1981. }
  1982. if (error) {
  1983. cancel_flags = 0;
  1984. goto error_return;
  1985. }
  1986. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1987. unlock_dp_on_error = B_TRUE;
  1988. /*
  1989. * Check whether the directory allows new symlinks or not.
  1990. */
  1991. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  1992. error = XFS_ERROR(EPERM);
  1993. goto error_return;
  1994. }
  1995. /*
  1996. * Reserve disk quota : blocks and inode.
  1997. */
  1998. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  1999. if (error)
  2000. goto error_return;
  2001. /*
  2002. * Check for ability to enter directory entry, if no space reserved.
  2003. */
  2004. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  2005. if (error)
  2006. goto error_return;
  2007. /*
  2008. * Initialize the bmap freelist prior to calling either
  2009. * bmapi or the directory create code.
  2010. */
  2011. xfs_bmap_init(&free_list, &first_block);
  2012. /*
  2013. * Allocate an inode for the symlink.
  2014. */
  2015. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2016. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2017. if (error) {
  2018. if (error == ENOSPC)
  2019. goto error_return;
  2020. goto error1;
  2021. }
  2022. /*
  2023. * An error after we've joined dp to the transaction will result in the
  2024. * transaction cancel unlocking dp so don't do it explicitly in the
  2025. * error path.
  2026. */
  2027. IHOLD(dp);
  2028. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2029. unlock_dp_on_error = B_FALSE;
  2030. /*
  2031. * Also attach the dquot(s) to it, if applicable.
  2032. */
  2033. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  2034. if (resblks)
  2035. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2036. /*
  2037. * If the symlink will fit into the inode, write it inline.
  2038. */
  2039. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2040. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2041. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2042. ip->i_d.di_size = pathlen;
  2043. /*
  2044. * The inode was initially created in extent format.
  2045. */
  2046. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2047. ip->i_df.if_flags |= XFS_IFINLINE;
  2048. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2049. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2050. } else {
  2051. first_fsb = 0;
  2052. nmaps = SYMLINK_MAPS;
  2053. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2054. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2055. &first_block, resblks, mval, &nmaps,
  2056. &free_list, NULL);
  2057. if (error) {
  2058. goto error1;
  2059. }
  2060. if (resblks)
  2061. resblks -= fs_blocks;
  2062. ip->i_d.di_size = pathlen;
  2063. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2064. cur_chunk = target_path;
  2065. for (n = 0; n < nmaps; n++) {
  2066. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2067. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2068. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2069. BTOBB(byte_cnt), 0);
  2070. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2071. if (pathlen < byte_cnt) {
  2072. byte_cnt = pathlen;
  2073. }
  2074. pathlen -= byte_cnt;
  2075. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2076. cur_chunk += byte_cnt;
  2077. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2078. }
  2079. }
  2080. /*
  2081. * Create the directory entry for the symlink.
  2082. */
  2083. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  2084. &first_block, &free_list, resblks);
  2085. if (error)
  2086. goto error1;
  2087. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2088. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2089. /*
  2090. * If this is a synchronous mount, make sure that the
  2091. * symlink transaction goes to disk before returning to
  2092. * the user.
  2093. */
  2094. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2095. xfs_trans_set_sync(tp);
  2096. }
  2097. /*
  2098. * xfs_trans_commit normally decrements the vnode ref count
  2099. * when it unlocks the inode. Since we want to return the
  2100. * vnode to the caller, we bump the vnode ref count now.
  2101. */
  2102. IHOLD(ip);
  2103. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2104. if (error) {
  2105. goto error2;
  2106. }
  2107. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2108. xfs_qm_dqrele(udqp);
  2109. xfs_qm_dqrele(gdqp);
  2110. /* Fall through to std_return with error = 0 or errno from
  2111. * xfs_trans_commit */
  2112. std_return:
  2113. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  2114. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  2115. dp, DM_RIGHT_NULL,
  2116. error ? NULL : ip,
  2117. DM_RIGHT_NULL, link_name->name,
  2118. (unsigned char *)target_path,
  2119. 0, error, 0);
  2120. }
  2121. if (!error)
  2122. *ipp = ip;
  2123. return error;
  2124. error2:
  2125. IRELE(ip);
  2126. error1:
  2127. xfs_bmap_cancel(&free_list);
  2128. cancel_flags |= XFS_TRANS_ABORT;
  2129. error_return:
  2130. xfs_trans_cancel(tp, cancel_flags);
  2131. xfs_qm_dqrele(udqp);
  2132. xfs_qm_dqrele(gdqp);
  2133. if (unlock_dp_on_error)
  2134. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2135. goto std_return;
  2136. }
  2137. int
  2138. xfs_set_dmattrs(
  2139. xfs_inode_t *ip,
  2140. u_int evmask,
  2141. u_int16_t state)
  2142. {
  2143. xfs_mount_t *mp = ip->i_mount;
  2144. xfs_trans_t *tp;
  2145. int error;
  2146. if (!capable(CAP_SYS_ADMIN))
  2147. return XFS_ERROR(EPERM);
  2148. if (XFS_FORCED_SHUTDOWN(mp))
  2149. return XFS_ERROR(EIO);
  2150. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  2151. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  2152. if (error) {
  2153. xfs_trans_cancel(tp, 0);
  2154. return error;
  2155. }
  2156. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2157. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2158. ip->i_d.di_dmevmask = evmask;
  2159. ip->i_d.di_dmstate = state;
  2160. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2161. IHOLD(ip);
  2162. error = xfs_trans_commit(tp, 0);
  2163. return error;
  2164. }
  2165. /*
  2166. * xfs_alloc_file_space()
  2167. * This routine allocates disk space for the given file.
  2168. *
  2169. * If alloc_type == 0, this request is for an ALLOCSP type
  2170. * request which will change the file size. In this case, no
  2171. * DMAPI event will be generated by the call. A TRUNCATE event
  2172. * will be generated later by xfs_setattr.
  2173. *
  2174. * If alloc_type != 0, this request is for a RESVSP type
  2175. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  2176. * lower block boundary byte address is less than the file's
  2177. * length.
  2178. *
  2179. * RETURNS:
  2180. * 0 on success
  2181. * errno on error
  2182. *
  2183. */
  2184. STATIC int
  2185. xfs_alloc_file_space(
  2186. xfs_inode_t *ip,
  2187. xfs_off_t offset,
  2188. xfs_off_t len,
  2189. int alloc_type,
  2190. int attr_flags)
  2191. {
  2192. xfs_mount_t *mp = ip->i_mount;
  2193. xfs_off_t count;
  2194. xfs_filblks_t allocated_fsb;
  2195. xfs_filblks_t allocatesize_fsb;
  2196. xfs_extlen_t extsz, temp;
  2197. xfs_fileoff_t startoffset_fsb;
  2198. xfs_fsblock_t firstfsb;
  2199. int nimaps;
  2200. int bmapi_flag;
  2201. int quota_flag;
  2202. int rt;
  2203. xfs_trans_t *tp;
  2204. xfs_bmbt_irec_t imaps[1], *imapp;
  2205. xfs_bmap_free_t free_list;
  2206. uint qblocks, resblks, resrtextents;
  2207. int committed;
  2208. int error;
  2209. xfs_itrace_entry(ip);
  2210. if (XFS_FORCED_SHUTDOWN(mp))
  2211. return XFS_ERROR(EIO);
  2212. error = xfs_qm_dqattach(ip, 0);
  2213. if (error)
  2214. return error;
  2215. if (len <= 0)
  2216. return XFS_ERROR(EINVAL);
  2217. rt = XFS_IS_REALTIME_INODE(ip);
  2218. extsz = xfs_get_extsz_hint(ip);
  2219. count = len;
  2220. imapp = &imaps[0];
  2221. nimaps = 1;
  2222. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  2223. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  2224. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  2225. /* Generate a DMAPI event if needed. */
  2226. if (alloc_type != 0 && offset < ip->i_size &&
  2227. (attr_flags & XFS_ATTR_DMI) == 0 &&
  2228. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  2229. xfs_off_t end_dmi_offset;
  2230. end_dmi_offset = offset+len;
  2231. if (end_dmi_offset > ip->i_size)
  2232. end_dmi_offset = ip->i_size;
  2233. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
  2234. end_dmi_offset - offset, 0, NULL);
  2235. if (error)
  2236. return error;
  2237. }
  2238. /*
  2239. * Allocate file space until done or until there is an error
  2240. */
  2241. retry:
  2242. while (allocatesize_fsb && !error) {
  2243. xfs_fileoff_t s, e;
  2244. /*
  2245. * Determine space reservations for data/realtime.
  2246. */
  2247. if (unlikely(extsz)) {
  2248. s = startoffset_fsb;
  2249. do_div(s, extsz);
  2250. s *= extsz;
  2251. e = startoffset_fsb + allocatesize_fsb;
  2252. if ((temp = do_mod(startoffset_fsb, extsz)))
  2253. e += temp;
  2254. if ((temp = do_mod(e, extsz)))
  2255. e += extsz - temp;
  2256. } else {
  2257. s = 0;
  2258. e = allocatesize_fsb;
  2259. }
  2260. if (unlikely(rt)) {
  2261. resrtextents = qblocks = (uint)(e - s);
  2262. resrtextents /= mp->m_sb.sb_rextsize;
  2263. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  2264. quota_flag = XFS_QMOPT_RES_RTBLKS;
  2265. } else {
  2266. resrtextents = 0;
  2267. resblks = qblocks = \
  2268. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  2269. quota_flag = XFS_QMOPT_RES_REGBLKS;
  2270. }
  2271. /*
  2272. * Allocate and setup the transaction.
  2273. */
  2274. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  2275. error = xfs_trans_reserve(tp, resblks,
  2276. XFS_WRITE_LOG_RES(mp), resrtextents,
  2277. XFS_TRANS_PERM_LOG_RES,
  2278. XFS_WRITE_LOG_COUNT);
  2279. /*
  2280. * Check for running out of space
  2281. */
  2282. if (error) {
  2283. /*
  2284. * Free the transaction structure.
  2285. */
  2286. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  2287. xfs_trans_cancel(tp, 0);
  2288. break;
  2289. }
  2290. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2291. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  2292. 0, quota_flag);
  2293. if (error)
  2294. goto error1;
  2295. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2296. xfs_trans_ihold(tp, ip);
  2297. /*
  2298. * Issue the xfs_bmapi() call to allocate the blocks
  2299. */
  2300. xfs_bmap_init(&free_list, &firstfsb);
  2301. error = xfs_bmapi(tp, ip, startoffset_fsb,
  2302. allocatesize_fsb, bmapi_flag,
  2303. &firstfsb, 0, imapp, &nimaps,
  2304. &free_list, NULL);
  2305. if (error) {
  2306. goto error0;
  2307. }
  2308. /*
  2309. * Complete the transaction
  2310. */
  2311. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2312. if (error) {
  2313. goto error0;
  2314. }
  2315. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2316. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2317. if (error) {
  2318. break;
  2319. }
  2320. allocated_fsb = imapp->br_blockcount;
  2321. if (nimaps == 0) {
  2322. error = XFS_ERROR(ENOSPC);
  2323. break;
  2324. }
  2325. startoffset_fsb += allocated_fsb;
  2326. allocatesize_fsb -= allocated_fsb;
  2327. }
  2328. dmapi_enospc_check:
  2329. if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
  2330. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  2331. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  2332. ip, DM_RIGHT_NULL,
  2333. ip, DM_RIGHT_NULL,
  2334. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  2335. if (error == 0)
  2336. goto retry; /* Maybe DMAPI app. has made space */
  2337. /* else fall through with error from XFS_SEND_DATA */
  2338. }
  2339. return error;
  2340. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  2341. xfs_bmap_cancel(&free_list);
  2342. xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  2343. error1: /* Just cancel transaction */
  2344. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  2345. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2346. goto dmapi_enospc_check;
  2347. }
  2348. /*
  2349. * Zero file bytes between startoff and endoff inclusive.
  2350. * The iolock is held exclusive and no blocks are buffered.
  2351. *
  2352. * This function is used by xfs_free_file_space() to zero
  2353. * partial blocks when the range to free is not block aligned.
  2354. * When unreserving space with boundaries that are not block
  2355. * aligned we round up the start and round down the end
  2356. * boundaries and then use this function to zero the parts of
  2357. * the blocks that got dropped during the rounding.
  2358. */
  2359. STATIC int
  2360. xfs_zero_remaining_bytes(
  2361. xfs_inode_t *ip,
  2362. xfs_off_t startoff,
  2363. xfs_off_t endoff)
  2364. {
  2365. xfs_bmbt_irec_t imap;
  2366. xfs_fileoff_t offset_fsb;
  2367. xfs_off_t lastoffset;
  2368. xfs_off_t offset;
  2369. xfs_buf_t *bp;
  2370. xfs_mount_t *mp = ip->i_mount;
  2371. int nimap;
  2372. int error = 0;
  2373. /*
  2374. * Avoid doing I/O beyond eof - it's not necessary
  2375. * since nothing can read beyond eof. The space will
  2376. * be zeroed when the file is extended anyway.
  2377. */
  2378. if (startoff >= ip->i_size)
  2379. return 0;
  2380. if (endoff > ip->i_size)
  2381. endoff = ip->i_size;
  2382. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  2383. XFS_IS_REALTIME_INODE(ip) ?
  2384. mp->m_rtdev_targp : mp->m_ddev_targp);
  2385. if (!bp)
  2386. return XFS_ERROR(ENOMEM);
  2387. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  2388. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  2389. nimap = 1;
  2390. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  2391. NULL, 0, &imap, &nimap, NULL, NULL);
  2392. if (error || nimap < 1)
  2393. break;
  2394. ASSERT(imap.br_blockcount >= 1);
  2395. ASSERT(imap.br_startoff == offset_fsb);
  2396. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  2397. if (lastoffset > endoff)
  2398. lastoffset = endoff;
  2399. if (imap.br_startblock == HOLESTARTBLOCK)
  2400. continue;
  2401. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2402. if (imap.br_state == XFS_EXT_UNWRITTEN)
  2403. continue;
  2404. XFS_BUF_UNDONE(bp);
  2405. XFS_BUF_UNWRITE(bp);
  2406. XFS_BUF_READ(bp);
  2407. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  2408. xfsbdstrat(mp, bp);
  2409. error = xfs_iowait(bp);
  2410. if (error) {
  2411. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  2412. mp, bp, XFS_BUF_ADDR(bp));
  2413. break;
  2414. }
  2415. memset(XFS_BUF_PTR(bp) +
  2416. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  2417. 0, lastoffset - offset + 1);
  2418. XFS_BUF_UNDONE(bp);
  2419. XFS_BUF_UNREAD(bp);
  2420. XFS_BUF_WRITE(bp);
  2421. xfsbdstrat(mp, bp);
  2422. error = xfs_iowait(bp);
  2423. if (error) {
  2424. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  2425. mp, bp, XFS_BUF_ADDR(bp));
  2426. break;
  2427. }
  2428. }
  2429. xfs_buf_free(bp);
  2430. return error;
  2431. }
  2432. /*
  2433. * xfs_free_file_space()
  2434. * This routine frees disk space for the given file.
  2435. *
  2436. * This routine is only called by xfs_change_file_space
  2437. * for an UNRESVSP type call.
  2438. *
  2439. * RETURNS:
  2440. * 0 on success
  2441. * errno on error
  2442. *
  2443. */
  2444. STATIC int
  2445. xfs_free_file_space(
  2446. xfs_inode_t *ip,
  2447. xfs_off_t offset,
  2448. xfs_off_t len,
  2449. int attr_flags)
  2450. {
  2451. int committed;
  2452. int done;
  2453. xfs_off_t end_dmi_offset;
  2454. xfs_fileoff_t endoffset_fsb;
  2455. int error;
  2456. xfs_fsblock_t firstfsb;
  2457. xfs_bmap_free_t free_list;
  2458. xfs_bmbt_irec_t imap;
  2459. xfs_off_t ioffset;
  2460. xfs_extlen_t mod=0;
  2461. xfs_mount_t *mp;
  2462. int nimap;
  2463. uint resblks;
  2464. uint rounding;
  2465. int rt;
  2466. xfs_fileoff_t startoffset_fsb;
  2467. xfs_trans_t *tp;
  2468. int need_iolock = 1;
  2469. mp = ip->i_mount;
  2470. xfs_itrace_entry(ip);
  2471. error = xfs_qm_dqattach(ip, 0);
  2472. if (error)
  2473. return error;
  2474. error = 0;
  2475. if (len <= 0) /* if nothing being freed */
  2476. return error;
  2477. rt = XFS_IS_REALTIME_INODE(ip);
  2478. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  2479. end_dmi_offset = offset + len;
  2480. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  2481. if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
  2482. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  2483. if (end_dmi_offset > ip->i_size)
  2484. end_dmi_offset = ip->i_size;
  2485. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
  2486. offset, end_dmi_offset - offset,
  2487. AT_DELAY_FLAG(attr_flags), NULL);
  2488. if (error)
  2489. return error;
  2490. }
  2491. if (attr_flags & XFS_ATTR_NOLOCK)
  2492. need_iolock = 0;
  2493. if (need_iolock) {
  2494. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  2495. /* wait for the completion of any pending DIOs */
  2496. xfs_ioend_wait(ip);
  2497. }
  2498. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  2499. ioffset = offset & ~(rounding - 1);
  2500. if (VN_CACHED(VFS_I(ip)) != 0) {
  2501. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  2502. if (error)
  2503. goto out_unlock_iolock;
  2504. }
  2505. /*
  2506. * Need to zero the stuff we're not freeing, on disk.
  2507. * If it's a realtime file & can't use unwritten extents then we
  2508. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  2509. * will take care of it for us.
  2510. */
  2511. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  2512. nimap = 1;
  2513. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  2514. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  2515. if (error)
  2516. goto out_unlock_iolock;
  2517. ASSERT(nimap == 0 || nimap == 1);
  2518. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  2519. xfs_daddr_t block;
  2520. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2521. block = imap.br_startblock;
  2522. mod = do_div(block, mp->m_sb.sb_rextsize);
  2523. if (mod)
  2524. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  2525. }
  2526. nimap = 1;
  2527. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  2528. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  2529. if (error)
  2530. goto out_unlock_iolock;
  2531. ASSERT(nimap == 0 || nimap == 1);
  2532. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  2533. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2534. mod++;
  2535. if (mod && (mod != mp->m_sb.sb_rextsize))
  2536. endoffset_fsb -= mod;
  2537. }
  2538. }
  2539. if ((done = (endoffset_fsb <= startoffset_fsb)))
  2540. /*
  2541. * One contiguous piece to clear
  2542. */
  2543. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  2544. else {
  2545. /*
  2546. * Some full blocks, possibly two pieces to clear
  2547. */
  2548. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  2549. error = xfs_zero_remaining_bytes(ip, offset,
  2550. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  2551. if (!error &&
  2552. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  2553. error = xfs_zero_remaining_bytes(ip,
  2554. XFS_FSB_TO_B(mp, endoffset_fsb),
  2555. offset + len - 1);
  2556. }
  2557. /*
  2558. * free file space until done or until there is an error
  2559. */
  2560. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  2561. while (!error && !done) {
  2562. /*
  2563. * allocate and setup the transaction. Allow this
  2564. * transaction to dip into the reserve blocks to ensure
  2565. * the freeing of the space succeeds at ENOSPC.
  2566. */
  2567. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  2568. tp->t_flags |= XFS_TRANS_RESERVE;
  2569. error = xfs_trans_reserve(tp,
  2570. resblks,
  2571. XFS_WRITE_LOG_RES(mp),
  2572. 0,
  2573. XFS_TRANS_PERM_LOG_RES,
  2574. XFS_WRITE_LOG_COUNT);
  2575. /*
  2576. * check for running out of space
  2577. */
  2578. if (error) {
  2579. /*
  2580. * Free the transaction structure.
  2581. */
  2582. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  2583. xfs_trans_cancel(tp, 0);
  2584. break;
  2585. }
  2586. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2587. error = xfs_trans_reserve_quota(tp, mp,
  2588. ip->i_udquot, ip->i_gdquot,
  2589. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  2590. if (error)
  2591. goto error1;
  2592. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2593. xfs_trans_ihold(tp, ip);
  2594. /*
  2595. * issue the bunmapi() call to free the blocks
  2596. */
  2597. xfs_bmap_init(&free_list, &firstfsb);
  2598. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  2599. endoffset_fsb - startoffset_fsb,
  2600. 0, 2, &firstfsb, &free_list, NULL, &done);
  2601. if (error) {
  2602. goto error0;
  2603. }
  2604. /*
  2605. * complete the transaction
  2606. */
  2607. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2608. if (error) {
  2609. goto error0;
  2610. }
  2611. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2612. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2613. }
  2614. out_unlock_iolock:
  2615. if (need_iolock)
  2616. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  2617. return error;
  2618. error0:
  2619. xfs_bmap_cancel(&free_list);
  2620. error1:
  2621. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  2622. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  2623. XFS_ILOCK_EXCL);
  2624. return error;
  2625. }
  2626. /*
  2627. * xfs_change_file_space()
  2628. * This routine allocates or frees disk space for the given file.
  2629. * The user specified parameters are checked for alignment and size
  2630. * limitations.
  2631. *
  2632. * RETURNS:
  2633. * 0 on success
  2634. * errno on error
  2635. *
  2636. */
  2637. int
  2638. xfs_change_file_space(
  2639. xfs_inode_t *ip,
  2640. int cmd,
  2641. xfs_flock64_t *bf,
  2642. xfs_off_t offset,
  2643. int attr_flags)
  2644. {
  2645. xfs_mount_t *mp = ip->i_mount;
  2646. int clrprealloc;
  2647. int error;
  2648. xfs_fsize_t fsize;
  2649. int setprealloc;
  2650. xfs_off_t startoffset;
  2651. xfs_off_t llen;
  2652. xfs_trans_t *tp;
  2653. struct iattr iattr;
  2654. xfs_itrace_entry(ip);
  2655. if (!S_ISREG(ip->i_d.di_mode))
  2656. return XFS_ERROR(EINVAL);
  2657. switch (bf->l_whence) {
  2658. case 0: /*SEEK_SET*/
  2659. break;
  2660. case 1: /*SEEK_CUR*/
  2661. bf->l_start += offset;
  2662. break;
  2663. case 2: /*SEEK_END*/
  2664. bf->l_start += ip->i_size;
  2665. break;
  2666. default:
  2667. return XFS_ERROR(EINVAL);
  2668. }
  2669. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  2670. if ( (bf->l_start < 0)
  2671. || (bf->l_start > XFS_MAXIOFFSET(mp))
  2672. || (bf->l_start + llen < 0)
  2673. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  2674. return XFS_ERROR(EINVAL);
  2675. bf->l_whence = 0;
  2676. startoffset = bf->l_start;
  2677. fsize = ip->i_size;
  2678. /*
  2679. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  2680. * file space.
  2681. * These calls do NOT zero the data space allocated to the file,
  2682. * nor do they change the file size.
  2683. *
  2684. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  2685. * space.
  2686. * These calls cause the new file data to be zeroed and the file
  2687. * size to be changed.
  2688. */
  2689. setprealloc = clrprealloc = 0;
  2690. switch (cmd) {
  2691. case XFS_IOC_RESVSP:
  2692. case XFS_IOC_RESVSP64:
  2693. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  2694. 1, attr_flags);
  2695. if (error)
  2696. return error;
  2697. setprealloc = 1;
  2698. break;
  2699. case XFS_IOC_UNRESVSP:
  2700. case XFS_IOC_UNRESVSP64:
  2701. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  2702. attr_flags)))
  2703. return error;
  2704. break;
  2705. case XFS_IOC_ALLOCSP:
  2706. case XFS_IOC_ALLOCSP64:
  2707. case XFS_IOC_FREESP:
  2708. case XFS_IOC_FREESP64:
  2709. if (startoffset > fsize) {
  2710. error = xfs_alloc_file_space(ip, fsize,
  2711. startoffset - fsize, 0, attr_flags);
  2712. if (error)
  2713. break;
  2714. }
  2715. iattr.ia_valid = ATTR_SIZE;
  2716. iattr.ia_size = startoffset;
  2717. error = xfs_setattr(ip, &iattr, attr_flags);
  2718. if (error)
  2719. return error;
  2720. clrprealloc = 1;
  2721. break;
  2722. default:
  2723. ASSERT(0);
  2724. return XFS_ERROR(EINVAL);
  2725. }
  2726. /*
  2727. * update the inode timestamp, mode, and prealloc flag bits
  2728. */
  2729. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  2730. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  2731. 0, 0, 0))) {
  2732. /* ASSERT(0); */
  2733. xfs_trans_cancel(tp, 0);
  2734. return error;
  2735. }
  2736. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2737. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2738. xfs_trans_ihold(tp, ip);
  2739. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  2740. ip->i_d.di_mode &= ~S_ISUID;
  2741. /*
  2742. * Note that we don't have to worry about mandatory
  2743. * file locking being disabled here because we only
  2744. * clear the S_ISGID bit if the Group execute bit is
  2745. * on, but if it was on then mandatory locking wouldn't
  2746. * have been enabled.
  2747. */
  2748. if (ip->i_d.di_mode & S_IXGRP)
  2749. ip->i_d.di_mode &= ~S_ISGID;
  2750. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2751. }
  2752. if (setprealloc)
  2753. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  2754. else if (clrprealloc)
  2755. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  2756. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2757. xfs_trans_set_sync(tp);
  2758. error = xfs_trans_commit(tp, 0);
  2759. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2760. return error;
  2761. }