xfs_vnodeops.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126
  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. if (ip->i_itemp->ili_last_lsn) {
  571. error = _xfs_log_force_lsn(ip->i_mount,
  572. ip->i_itemp->ili_last_lsn,
  573. XFS_LOG_SYNC, &log_flushed);
  574. } else {
  575. error = _xfs_log_force(ip->i_mount,
  576. XFS_LOG_SYNC, &log_flushed);
  577. }
  578. }
  579. } else {
  580. /*
  581. * Kick off a transaction to log the inode core to get the
  582. * updates. The sync transaction will also force the log.
  583. */
  584. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  585. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  586. error = xfs_trans_reserve(tp, 0,
  587. XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
  588. if (error) {
  589. xfs_trans_cancel(tp, 0);
  590. return error;
  591. }
  592. xfs_ilock(ip, XFS_ILOCK_EXCL);
  593. /*
  594. * Note - it's possible that we might have pushed ourselves out
  595. * of the way during trans_reserve which would flush the inode.
  596. * But there's no guarantee that the inode buffer has actually
  597. * gone out yet (it's delwri). Plus the buffer could be pinned
  598. * anyway if it's part of an inode in another recent
  599. * transaction. So we play it safe and fire off the
  600. * transaction anyway.
  601. */
  602. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  603. xfs_trans_ihold(tp, ip);
  604. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  605. xfs_trans_set_sync(tp);
  606. error = _xfs_trans_commit(tp, 0, &log_flushed);
  607. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  608. }
  609. if (ip->i_mount->m_flags & XFS_MOUNT_BARRIER) {
  610. /*
  611. * If the log write didn't issue an ordered tag we need
  612. * to flush the disk cache for the data device now.
  613. */
  614. if (!log_flushed)
  615. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  616. /*
  617. * If this inode is on the RT dev we need to flush that
  618. * cache as well.
  619. */
  620. if (XFS_IS_REALTIME_INODE(ip))
  621. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  622. }
  623. return error;
  624. }
  625. /*
  626. * Flags for xfs_free_eofblocks
  627. */
  628. #define XFS_FREE_EOF_TRYLOCK (1<<0)
  629. /*
  630. * This is called by xfs_inactive to free any blocks beyond eof
  631. * when the link count isn't zero and by xfs_dm_punch_hole() when
  632. * punching a hole to EOF.
  633. */
  634. STATIC int
  635. xfs_free_eofblocks(
  636. xfs_mount_t *mp,
  637. xfs_inode_t *ip,
  638. int flags)
  639. {
  640. xfs_trans_t *tp;
  641. int error;
  642. xfs_fileoff_t end_fsb;
  643. xfs_fileoff_t last_fsb;
  644. xfs_filblks_t map_len;
  645. int nimaps;
  646. xfs_bmbt_irec_t imap;
  647. /*
  648. * Figure out if there are any blocks beyond the end
  649. * of the file. If not, then there is nothing to do.
  650. */
  651. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
  652. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  653. map_len = last_fsb - end_fsb;
  654. if (map_len <= 0)
  655. return 0;
  656. nimaps = 1;
  657. xfs_ilock(ip, XFS_ILOCK_SHARED);
  658. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  659. NULL, 0, &imap, &nimaps, NULL, NULL);
  660. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  661. if (!error && (nimaps != 0) &&
  662. (imap.br_startblock != HOLESTARTBLOCK ||
  663. ip->i_delayed_blks)) {
  664. /*
  665. * Attach the dquots to the inode up front.
  666. */
  667. error = xfs_qm_dqattach(ip, 0);
  668. if (error)
  669. return error;
  670. /*
  671. * There are blocks after the end of file.
  672. * Free them up now by truncating the file to
  673. * its current size.
  674. */
  675. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  676. /*
  677. * Do the xfs_itruncate_start() call before
  678. * reserving any log space because
  679. * itruncate_start will call into the buffer
  680. * cache and we can't
  681. * do that within a transaction.
  682. */
  683. if (flags & XFS_FREE_EOF_TRYLOCK) {
  684. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  685. xfs_trans_cancel(tp, 0);
  686. return 0;
  687. }
  688. } else {
  689. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  690. }
  691. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  692. ip->i_size);
  693. if (error) {
  694. xfs_trans_cancel(tp, 0);
  695. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  696. return error;
  697. }
  698. error = xfs_trans_reserve(tp, 0,
  699. XFS_ITRUNCATE_LOG_RES(mp),
  700. 0, XFS_TRANS_PERM_LOG_RES,
  701. XFS_ITRUNCATE_LOG_COUNT);
  702. if (error) {
  703. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  704. xfs_trans_cancel(tp, 0);
  705. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  706. return error;
  707. }
  708. xfs_ilock(ip, XFS_ILOCK_EXCL);
  709. xfs_trans_ijoin(tp, ip,
  710. XFS_IOLOCK_EXCL |
  711. XFS_ILOCK_EXCL);
  712. xfs_trans_ihold(tp, ip);
  713. error = xfs_itruncate_finish(&tp, ip,
  714. ip->i_size,
  715. XFS_DATA_FORK,
  716. 0);
  717. /*
  718. * If we get an error at this point we
  719. * simply don't bother truncating the file.
  720. */
  721. if (error) {
  722. xfs_trans_cancel(tp,
  723. (XFS_TRANS_RELEASE_LOG_RES |
  724. XFS_TRANS_ABORT));
  725. } else {
  726. error = xfs_trans_commit(tp,
  727. XFS_TRANS_RELEASE_LOG_RES);
  728. }
  729. xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
  730. }
  731. return error;
  732. }
  733. /*
  734. * Free a symlink that has blocks associated with it.
  735. */
  736. STATIC int
  737. xfs_inactive_symlink_rmt(
  738. xfs_inode_t *ip,
  739. xfs_trans_t **tpp)
  740. {
  741. xfs_buf_t *bp;
  742. int committed;
  743. int done;
  744. int error;
  745. xfs_fsblock_t first_block;
  746. xfs_bmap_free_t free_list;
  747. int i;
  748. xfs_mount_t *mp;
  749. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  750. int nmaps;
  751. xfs_trans_t *ntp;
  752. int size;
  753. xfs_trans_t *tp;
  754. tp = *tpp;
  755. mp = ip->i_mount;
  756. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  757. /*
  758. * We're freeing a symlink that has some
  759. * blocks allocated to it. Free the
  760. * blocks here. We know that we've got
  761. * either 1 or 2 extents and that we can
  762. * free them all in one bunmapi call.
  763. */
  764. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  765. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  766. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  767. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  768. xfs_trans_cancel(tp, 0);
  769. *tpp = NULL;
  770. return error;
  771. }
  772. /*
  773. * Lock the inode, fix the size, and join it to the transaction.
  774. * Hold it so in the normal path, we still have it locked for
  775. * the second transaction. In the error paths we need it
  776. * held so the cancel won't rele it, see below.
  777. */
  778. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  779. size = (int)ip->i_d.di_size;
  780. ip->i_d.di_size = 0;
  781. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  782. xfs_trans_ihold(tp, ip);
  783. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  784. /*
  785. * Find the block(s) so we can inval and unmap them.
  786. */
  787. done = 0;
  788. xfs_bmap_init(&free_list, &first_block);
  789. nmaps = ARRAY_SIZE(mval);
  790. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  791. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  792. &free_list, NULL)))
  793. goto error0;
  794. /*
  795. * Invalidate the block(s).
  796. */
  797. for (i = 0; i < nmaps; i++) {
  798. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  799. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  800. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  801. xfs_trans_binval(tp, bp);
  802. }
  803. /*
  804. * Unmap the dead block(s) to the free_list.
  805. */
  806. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  807. &first_block, &free_list, NULL, &done)))
  808. goto error1;
  809. ASSERT(done);
  810. /*
  811. * Commit the first transaction. This logs the EFI and the inode.
  812. */
  813. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  814. goto error1;
  815. /*
  816. * The transaction must have been committed, since there were
  817. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  818. * The new tp has the extent freeing and EFDs.
  819. */
  820. ASSERT(committed);
  821. /*
  822. * The first xact was committed, so add the inode to the new one.
  823. * Mark it dirty so it will be logged and moved forward in the log as
  824. * part of every commit.
  825. */
  826. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  827. xfs_trans_ihold(tp, ip);
  828. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  829. /*
  830. * Get a new, empty transaction to return to our caller.
  831. */
  832. ntp = xfs_trans_dup(tp);
  833. /*
  834. * Commit the transaction containing extent freeing and EFDs.
  835. * If we get an error on the commit here or on the reserve below,
  836. * we need to unlock the inode since the new transaction doesn't
  837. * have the inode attached.
  838. */
  839. error = xfs_trans_commit(tp, 0);
  840. tp = ntp;
  841. if (error) {
  842. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  843. goto error0;
  844. }
  845. /*
  846. * transaction commit worked ok so we can drop the extra ticket
  847. * reference that we gained in xfs_trans_dup()
  848. */
  849. xfs_log_ticket_put(tp->t_ticket);
  850. /*
  851. * Remove the memory for extent descriptions (just bookkeeping).
  852. */
  853. if (ip->i_df.if_bytes)
  854. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  855. ASSERT(ip->i_df.if_bytes == 0);
  856. /*
  857. * Put an itruncate log reservation in the new transaction
  858. * for our caller.
  859. */
  860. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  861. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  862. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  863. goto error0;
  864. }
  865. /*
  866. * Return with the inode locked but not joined to the transaction.
  867. */
  868. *tpp = tp;
  869. return 0;
  870. error1:
  871. xfs_bmap_cancel(&free_list);
  872. error0:
  873. /*
  874. * Have to come here with the inode locked and either
  875. * (held and in the transaction) or (not in the transaction).
  876. * If the inode isn't held then cancel would iput it, but
  877. * that's wrong since this is inactive and the vnode ref
  878. * count is 0 already.
  879. * Cancel won't do anything to the inode if held, but it still
  880. * needs to be locked until the cancel is done, if it was
  881. * joined to the transaction.
  882. */
  883. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  884. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  885. *tpp = NULL;
  886. return error;
  887. }
  888. STATIC int
  889. xfs_inactive_symlink_local(
  890. xfs_inode_t *ip,
  891. xfs_trans_t **tpp)
  892. {
  893. int error;
  894. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  895. /*
  896. * We're freeing a symlink which fit into
  897. * the inode. Just free the memory used
  898. * to hold the old symlink.
  899. */
  900. error = xfs_trans_reserve(*tpp, 0,
  901. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  902. 0, XFS_TRANS_PERM_LOG_RES,
  903. XFS_ITRUNCATE_LOG_COUNT);
  904. if (error) {
  905. xfs_trans_cancel(*tpp, 0);
  906. *tpp = NULL;
  907. return error;
  908. }
  909. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  910. /*
  911. * Zero length symlinks _can_ exist.
  912. */
  913. if (ip->i_df.if_bytes > 0) {
  914. xfs_idata_realloc(ip,
  915. -(ip->i_df.if_bytes),
  916. XFS_DATA_FORK);
  917. ASSERT(ip->i_df.if_bytes == 0);
  918. }
  919. return 0;
  920. }
  921. STATIC int
  922. xfs_inactive_attrs(
  923. xfs_inode_t *ip,
  924. xfs_trans_t **tpp)
  925. {
  926. xfs_trans_t *tp;
  927. int error;
  928. xfs_mount_t *mp;
  929. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  930. tp = *tpp;
  931. mp = ip->i_mount;
  932. ASSERT(ip->i_d.di_forkoff != 0);
  933. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  934. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  935. if (error)
  936. goto error_unlock;
  937. error = xfs_attr_inactive(ip);
  938. if (error)
  939. goto error_unlock;
  940. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  941. error = xfs_trans_reserve(tp, 0,
  942. XFS_IFREE_LOG_RES(mp),
  943. 0, XFS_TRANS_PERM_LOG_RES,
  944. XFS_INACTIVE_LOG_COUNT);
  945. if (error)
  946. goto error_cancel;
  947. xfs_ilock(ip, XFS_ILOCK_EXCL);
  948. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  949. xfs_trans_ihold(tp, ip);
  950. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  951. ASSERT(ip->i_d.di_anextents == 0);
  952. *tpp = tp;
  953. return 0;
  954. error_cancel:
  955. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  956. xfs_trans_cancel(tp, 0);
  957. error_unlock:
  958. *tpp = NULL;
  959. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  960. return error;
  961. }
  962. int
  963. xfs_release(
  964. xfs_inode_t *ip)
  965. {
  966. xfs_mount_t *mp = ip->i_mount;
  967. int error;
  968. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  969. return 0;
  970. /* If this is a read-only mount, don't do this (would generate I/O) */
  971. if (mp->m_flags & XFS_MOUNT_RDONLY)
  972. return 0;
  973. if (!XFS_FORCED_SHUTDOWN(mp)) {
  974. int truncated;
  975. /*
  976. * If we are using filestreams, and we have an unlinked
  977. * file that we are processing the last close on, then nothing
  978. * will be able to reopen and write to this file. Purge this
  979. * inode from the filestreams cache so that it doesn't delay
  980. * teardown of the inode.
  981. */
  982. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  983. xfs_filestream_deassociate(ip);
  984. /*
  985. * If we previously truncated this file and removed old data
  986. * in the process, we want to initiate "early" writeout on
  987. * the last close. This is an attempt to combat the notorious
  988. * NULL files problem which is particularly noticable from a
  989. * truncate down, buffered (re-)write (delalloc), followed by
  990. * a crash. What we are effectively doing here is
  991. * significantly reducing the time window where we'd otherwise
  992. * be exposed to that problem.
  993. */
  994. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  995. if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
  996. xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
  997. }
  998. if (ip->i_d.di_nlink != 0) {
  999. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1000. ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
  1001. ip->i_delayed_blks > 0)) &&
  1002. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1003. (!(ip->i_d.di_flags &
  1004. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1005. /*
  1006. * If we can't get the iolock just skip truncating
  1007. * the blocks past EOF because we could deadlock
  1008. * with the mmap_sem otherwise. We'll get another
  1009. * chance to drop them once the last reference to
  1010. * the inode is dropped, so we'll never leak blocks
  1011. * permanently.
  1012. */
  1013. error = xfs_free_eofblocks(mp, ip,
  1014. XFS_FREE_EOF_TRYLOCK);
  1015. if (error)
  1016. return error;
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. /*
  1022. * xfs_inactive
  1023. *
  1024. * This is called when the vnode reference count for the vnode
  1025. * goes to zero. If the file has been unlinked, then it must
  1026. * now be truncated. Also, we clear all of the read-ahead state
  1027. * kept for the inode here since the file is now closed.
  1028. */
  1029. int
  1030. xfs_inactive(
  1031. xfs_inode_t *ip)
  1032. {
  1033. xfs_bmap_free_t free_list;
  1034. xfs_fsblock_t first_block;
  1035. int committed;
  1036. xfs_trans_t *tp;
  1037. xfs_mount_t *mp;
  1038. int error;
  1039. int truncate;
  1040. xfs_itrace_entry(ip);
  1041. /*
  1042. * If the inode is already free, then there can be nothing
  1043. * to clean up here.
  1044. */
  1045. if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
  1046. ASSERT(ip->i_df.if_real_bytes == 0);
  1047. ASSERT(ip->i_df.if_broot_bytes == 0);
  1048. return VN_INACTIVE_CACHE;
  1049. }
  1050. /*
  1051. * Only do a truncate if it's a regular file with
  1052. * some actual space in it. It's OK to look at the
  1053. * inode's fields without the lock because we're the
  1054. * only one with a reference to the inode.
  1055. */
  1056. truncate = ((ip->i_d.di_nlink == 0) &&
  1057. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1058. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1059. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1060. mp = ip->i_mount;
  1061. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
  1062. XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
  1063. error = 0;
  1064. /* If this is a read-only mount, don't do this (would generate I/O) */
  1065. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1066. goto out;
  1067. if (ip->i_d.di_nlink != 0) {
  1068. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1069. ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
  1070. ip->i_delayed_blks > 0)) &&
  1071. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1072. (!(ip->i_d.di_flags &
  1073. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1074. (ip->i_delayed_blks != 0)))) {
  1075. error = xfs_free_eofblocks(mp, ip, 0);
  1076. if (error)
  1077. return VN_INACTIVE_CACHE;
  1078. }
  1079. goto out;
  1080. }
  1081. ASSERT(ip->i_d.di_nlink == 0);
  1082. error = xfs_qm_dqattach(ip, 0);
  1083. if (error)
  1084. return VN_INACTIVE_CACHE;
  1085. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1086. if (truncate) {
  1087. /*
  1088. * Do the xfs_itruncate_start() call before
  1089. * reserving any log space because itruncate_start
  1090. * will call into the buffer cache and we can't
  1091. * do that within a transaction.
  1092. */
  1093. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1094. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1095. if (error) {
  1096. xfs_trans_cancel(tp, 0);
  1097. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1098. return VN_INACTIVE_CACHE;
  1099. }
  1100. error = xfs_trans_reserve(tp, 0,
  1101. XFS_ITRUNCATE_LOG_RES(mp),
  1102. 0, XFS_TRANS_PERM_LOG_RES,
  1103. XFS_ITRUNCATE_LOG_COUNT);
  1104. if (error) {
  1105. /* Don't call itruncate_cleanup */
  1106. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1107. xfs_trans_cancel(tp, 0);
  1108. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1109. return VN_INACTIVE_CACHE;
  1110. }
  1111. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1112. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1113. xfs_trans_ihold(tp, ip);
  1114. /*
  1115. * normally, we have to run xfs_itruncate_finish sync.
  1116. * But if filesystem is wsync and we're in the inactive
  1117. * path, then we know that nlink == 0, and that the
  1118. * xaction that made nlink == 0 is permanently committed
  1119. * since xfs_remove runs as a synchronous transaction.
  1120. */
  1121. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1122. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1123. if (error) {
  1124. xfs_trans_cancel(tp,
  1125. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1126. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1127. return VN_INACTIVE_CACHE;
  1128. }
  1129. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1130. /*
  1131. * If we get an error while cleaning up a
  1132. * symlink we bail out.
  1133. */
  1134. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1135. xfs_inactive_symlink_rmt(ip, &tp) :
  1136. xfs_inactive_symlink_local(ip, &tp);
  1137. if (error) {
  1138. ASSERT(tp == NULL);
  1139. return VN_INACTIVE_CACHE;
  1140. }
  1141. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1142. xfs_trans_ihold(tp, ip);
  1143. } else {
  1144. error = xfs_trans_reserve(tp, 0,
  1145. XFS_IFREE_LOG_RES(mp),
  1146. 0, XFS_TRANS_PERM_LOG_RES,
  1147. XFS_INACTIVE_LOG_COUNT);
  1148. if (error) {
  1149. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1150. xfs_trans_cancel(tp, 0);
  1151. return VN_INACTIVE_CACHE;
  1152. }
  1153. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1154. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1155. xfs_trans_ihold(tp, ip);
  1156. }
  1157. /*
  1158. * If there are attributes associated with the file
  1159. * then blow them away now. The code calls a routine
  1160. * that recursively deconstructs the attribute fork.
  1161. * We need to just commit the current transaction
  1162. * because we can't use it for xfs_attr_inactive().
  1163. */
  1164. if (ip->i_d.di_anextents > 0) {
  1165. error = xfs_inactive_attrs(ip, &tp);
  1166. /*
  1167. * If we got an error, the transaction is already
  1168. * cancelled, and the inode is unlocked. Just get out.
  1169. */
  1170. if (error)
  1171. return VN_INACTIVE_CACHE;
  1172. } else if (ip->i_afp) {
  1173. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1174. }
  1175. /*
  1176. * Free the inode.
  1177. */
  1178. xfs_bmap_init(&free_list, &first_block);
  1179. error = xfs_ifree(tp, ip, &free_list);
  1180. if (error) {
  1181. /*
  1182. * If we fail to free the inode, shut down. The cancel
  1183. * might do that, we need to make sure. Otherwise the
  1184. * inode might be lost for a long time or forever.
  1185. */
  1186. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1187. cmn_err(CE_NOTE,
  1188. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1189. error, mp->m_fsname);
  1190. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1191. }
  1192. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1193. } else {
  1194. /*
  1195. * Credit the quota account(s). The inode is gone.
  1196. */
  1197. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1198. /*
  1199. * Just ignore errors at this point. There is nothing we can
  1200. * do except to try to keep going. Make sure it's not a silent
  1201. * error.
  1202. */
  1203. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1204. if (error)
  1205. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1206. "xfs_bmap_finish() returned error %d", error);
  1207. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1208. if (error)
  1209. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1210. "xfs_trans_commit() returned error %d", error);
  1211. }
  1212. /*
  1213. * Release the dquots held by inode, if any.
  1214. */
  1215. xfs_qm_dqdetach(ip);
  1216. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1217. out:
  1218. return VN_INACTIVE_CACHE;
  1219. }
  1220. /*
  1221. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  1222. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  1223. * ci_name->name will point to a the actual name (caller must free) or
  1224. * will be set to NULL if an exact match is found.
  1225. */
  1226. int
  1227. xfs_lookup(
  1228. xfs_inode_t *dp,
  1229. struct xfs_name *name,
  1230. xfs_inode_t **ipp,
  1231. struct xfs_name *ci_name)
  1232. {
  1233. xfs_ino_t inum;
  1234. int error;
  1235. uint lock_mode;
  1236. xfs_itrace_entry(dp);
  1237. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1238. return XFS_ERROR(EIO);
  1239. lock_mode = xfs_ilock_map_shared(dp);
  1240. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  1241. xfs_iunlock_map_shared(dp, lock_mode);
  1242. if (error)
  1243. goto out;
  1244. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
  1245. if (error)
  1246. goto out_free_name;
  1247. return 0;
  1248. out_free_name:
  1249. if (ci_name)
  1250. kmem_free(ci_name->name);
  1251. out:
  1252. *ipp = NULL;
  1253. return error;
  1254. }
  1255. int
  1256. xfs_create(
  1257. xfs_inode_t *dp,
  1258. struct xfs_name *name,
  1259. mode_t mode,
  1260. xfs_dev_t rdev,
  1261. xfs_inode_t **ipp,
  1262. cred_t *credp)
  1263. {
  1264. int is_dir = S_ISDIR(mode);
  1265. struct xfs_mount *mp = dp->i_mount;
  1266. struct xfs_inode *ip = NULL;
  1267. struct xfs_trans *tp = NULL;
  1268. int error;
  1269. xfs_bmap_free_t free_list;
  1270. xfs_fsblock_t first_block;
  1271. boolean_t unlock_dp_on_error = B_FALSE;
  1272. uint cancel_flags;
  1273. int committed;
  1274. xfs_prid_t prid;
  1275. struct xfs_dquot *udqp = NULL;
  1276. struct xfs_dquot *gdqp = NULL;
  1277. uint resblks;
  1278. uint log_res;
  1279. uint log_count;
  1280. xfs_itrace_entry(dp);
  1281. if (XFS_FORCED_SHUTDOWN(mp))
  1282. return XFS_ERROR(EIO);
  1283. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1284. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1285. dp, DM_RIGHT_NULL, NULL,
  1286. DM_RIGHT_NULL, name->name, NULL,
  1287. mode, 0, 0);
  1288. if (error)
  1289. return error;
  1290. }
  1291. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1292. prid = dp->i_d.di_projid;
  1293. else
  1294. prid = dfltprid;
  1295. /*
  1296. * Make sure that we have allocated dquot(s) on disk.
  1297. */
  1298. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1299. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1300. if (error)
  1301. goto std_return;
  1302. if (is_dir) {
  1303. rdev = 0;
  1304. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  1305. log_res = XFS_MKDIR_LOG_RES(mp);
  1306. log_count = XFS_MKDIR_LOG_COUNT;
  1307. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  1308. } else {
  1309. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1310. log_res = XFS_CREATE_LOG_RES(mp);
  1311. log_count = XFS_CREATE_LOG_COUNT;
  1312. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1313. }
  1314. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1315. /*
  1316. * Initially assume that the file does not exist and
  1317. * reserve the resources for that case. If that is not
  1318. * the case we'll drop the one we have and get a more
  1319. * appropriate transaction later.
  1320. */
  1321. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  1322. XFS_TRANS_PERM_LOG_RES, log_count);
  1323. if (error == ENOSPC) {
  1324. /* flush outstanding delalloc blocks and retry */
  1325. xfs_flush_inodes(dp);
  1326. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  1327. XFS_TRANS_PERM_LOG_RES, log_count);
  1328. }
  1329. if (error == ENOSPC) {
  1330. /* No space at all so try a "no-allocation" reservation */
  1331. resblks = 0;
  1332. error = xfs_trans_reserve(tp, 0, log_res, 0,
  1333. XFS_TRANS_PERM_LOG_RES, log_count);
  1334. }
  1335. if (error) {
  1336. cancel_flags = 0;
  1337. goto out_trans_cancel;
  1338. }
  1339. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1340. unlock_dp_on_error = B_TRUE;
  1341. /*
  1342. * Check for directory link count overflow.
  1343. */
  1344. if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
  1345. error = XFS_ERROR(EMLINK);
  1346. goto out_trans_cancel;
  1347. }
  1348. xfs_bmap_init(&free_list, &first_block);
  1349. /*
  1350. * Reserve disk quota and the inode.
  1351. */
  1352. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  1353. if (error)
  1354. goto out_trans_cancel;
  1355. error = xfs_dir_canenter(tp, dp, name, resblks);
  1356. if (error)
  1357. goto out_trans_cancel;
  1358. /*
  1359. * A newly created regular or special file just has one directory
  1360. * entry pointing to them, but a directory also the "." entry
  1361. * pointing to itself.
  1362. */
  1363. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
  1364. prid, resblks > 0, &ip, &committed);
  1365. if (error) {
  1366. if (error == ENOSPC)
  1367. goto out_trans_cancel;
  1368. goto out_trans_abort;
  1369. }
  1370. /*
  1371. * At this point, we've gotten a newly allocated inode.
  1372. * It is locked (and joined to the transaction).
  1373. */
  1374. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1375. /*
  1376. * Now we join the directory inode to the transaction. We do not do it
  1377. * earlier because xfs_dir_ialloc might commit the previous transaction
  1378. * (and release all the locks). An error from here on will result in
  1379. * the transaction cancel unlocking dp so don't do it explicitly in the
  1380. * error path.
  1381. */
  1382. IHOLD(dp);
  1383. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1384. unlock_dp_on_error = B_FALSE;
  1385. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1386. &first_block, &free_list, resblks ?
  1387. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1388. if (error) {
  1389. ASSERT(error != ENOSPC);
  1390. goto out_trans_abort;
  1391. }
  1392. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1393. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1394. if (is_dir) {
  1395. error = xfs_dir_init(tp, ip, dp);
  1396. if (error)
  1397. goto out_bmap_cancel;
  1398. error = xfs_bumplink(tp, dp);
  1399. if (error)
  1400. goto out_bmap_cancel;
  1401. }
  1402. /*
  1403. * If this is a synchronous mount, make sure that the
  1404. * create transaction goes to disk before returning to
  1405. * the user.
  1406. */
  1407. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1408. xfs_trans_set_sync(tp);
  1409. /*
  1410. * Attach the dquot(s) to the inodes and modify them incore.
  1411. * These ids of the inode couldn't have changed since the new
  1412. * inode has been locked ever since it was created.
  1413. */
  1414. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  1415. /*
  1416. * xfs_trans_commit normally decrements the vnode ref count
  1417. * when it unlocks the inode. Since we want to return the
  1418. * vnode to the caller, we bump the vnode ref count now.
  1419. */
  1420. IHOLD(ip);
  1421. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1422. if (error)
  1423. goto out_abort_rele;
  1424. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1425. if (error) {
  1426. IRELE(ip);
  1427. goto out_dqrele;
  1428. }
  1429. xfs_qm_dqrele(udqp);
  1430. xfs_qm_dqrele(gdqp);
  1431. *ipp = ip;
  1432. /* Fallthrough to std_return with error = 0 */
  1433. std_return:
  1434. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1435. XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
  1436. ip, DM_RIGHT_NULL, name->name, NULL, mode,
  1437. error, 0);
  1438. }
  1439. return error;
  1440. out_bmap_cancel:
  1441. xfs_bmap_cancel(&free_list);
  1442. out_trans_abort:
  1443. cancel_flags |= XFS_TRANS_ABORT;
  1444. out_trans_cancel:
  1445. xfs_trans_cancel(tp, cancel_flags);
  1446. out_dqrele:
  1447. xfs_qm_dqrele(udqp);
  1448. xfs_qm_dqrele(gdqp);
  1449. if (unlock_dp_on_error)
  1450. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1451. goto std_return;
  1452. out_abort_rele:
  1453. /*
  1454. * Wait until after the current transaction is aborted to
  1455. * release the inode. This prevents recursive transactions
  1456. * and deadlocks from xfs_inactive.
  1457. */
  1458. xfs_bmap_cancel(&free_list);
  1459. cancel_flags |= XFS_TRANS_ABORT;
  1460. xfs_trans_cancel(tp, cancel_flags);
  1461. IRELE(ip);
  1462. unlock_dp_on_error = B_FALSE;
  1463. goto out_dqrele;
  1464. }
  1465. #ifdef DEBUG
  1466. int xfs_locked_n;
  1467. int xfs_small_retries;
  1468. int xfs_middle_retries;
  1469. int xfs_lots_retries;
  1470. int xfs_lock_delays;
  1471. #endif
  1472. /*
  1473. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1474. * a different value
  1475. */
  1476. static inline int
  1477. xfs_lock_inumorder(int lock_mode, int subclass)
  1478. {
  1479. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1480. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1481. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1482. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1483. return lock_mode;
  1484. }
  1485. /*
  1486. * The following routine will lock n inodes in exclusive mode.
  1487. * We assume the caller calls us with the inodes in i_ino order.
  1488. *
  1489. * We need to detect deadlock where an inode that we lock
  1490. * is in the AIL and we start waiting for another inode that is locked
  1491. * by a thread in a long running transaction (such as truncate). This can
  1492. * result in deadlock since the long running trans might need to wait
  1493. * for the inode we just locked in order to push the tail and free space
  1494. * in the log.
  1495. */
  1496. void
  1497. xfs_lock_inodes(
  1498. xfs_inode_t **ips,
  1499. int inodes,
  1500. uint lock_mode)
  1501. {
  1502. int attempts = 0, i, j, try_lock;
  1503. xfs_log_item_t *lp;
  1504. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1505. try_lock = 0;
  1506. i = 0;
  1507. again:
  1508. for (; i < inodes; i++) {
  1509. ASSERT(ips[i]);
  1510. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1511. continue;
  1512. /*
  1513. * If try_lock is not set yet, make sure all locked inodes
  1514. * are not in the AIL.
  1515. * If any are, set try_lock to be used later.
  1516. */
  1517. if (!try_lock) {
  1518. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1519. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1520. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1521. try_lock++;
  1522. }
  1523. }
  1524. }
  1525. /*
  1526. * If any of the previous locks we have locked is in the AIL,
  1527. * we must TRY to get the second and subsequent locks. If
  1528. * we can't get any, we must release all we have
  1529. * and try again.
  1530. */
  1531. if (try_lock) {
  1532. /* try_lock must be 0 if i is 0. */
  1533. /*
  1534. * try_lock means we have an inode locked
  1535. * that is in the AIL.
  1536. */
  1537. ASSERT(i != 0);
  1538. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  1539. attempts++;
  1540. /*
  1541. * Unlock all previous guys and try again.
  1542. * xfs_iunlock will try to push the tail
  1543. * if the inode is in the AIL.
  1544. */
  1545. for(j = i - 1; j >= 0; j--) {
  1546. /*
  1547. * Check to see if we've already
  1548. * unlocked this one.
  1549. * Not the first one going back,
  1550. * and the inode ptr is the same.
  1551. */
  1552. if ((j != (i - 1)) && ips[j] ==
  1553. ips[j+1])
  1554. continue;
  1555. xfs_iunlock(ips[j], lock_mode);
  1556. }
  1557. if ((attempts % 5) == 0) {
  1558. delay(1); /* Don't just spin the CPU */
  1559. #ifdef DEBUG
  1560. xfs_lock_delays++;
  1561. #endif
  1562. }
  1563. i = 0;
  1564. try_lock = 0;
  1565. goto again;
  1566. }
  1567. } else {
  1568. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  1569. }
  1570. }
  1571. #ifdef DEBUG
  1572. if (attempts) {
  1573. if (attempts < 5) xfs_small_retries++;
  1574. else if (attempts < 100) xfs_middle_retries++;
  1575. else xfs_lots_retries++;
  1576. } else {
  1577. xfs_locked_n++;
  1578. }
  1579. #endif
  1580. }
  1581. /*
  1582. * xfs_lock_two_inodes() can only be used to lock one type of lock
  1583. * at a time - the iolock or the ilock, but not both at once. If
  1584. * we lock both at once, lockdep will report false positives saying
  1585. * we have violated locking orders.
  1586. */
  1587. void
  1588. xfs_lock_two_inodes(
  1589. xfs_inode_t *ip0,
  1590. xfs_inode_t *ip1,
  1591. uint lock_mode)
  1592. {
  1593. xfs_inode_t *temp;
  1594. int attempts = 0;
  1595. xfs_log_item_t *lp;
  1596. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1597. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  1598. ASSERT(ip0->i_ino != ip1->i_ino);
  1599. if (ip0->i_ino > ip1->i_ino) {
  1600. temp = ip0;
  1601. ip0 = ip1;
  1602. ip1 = temp;
  1603. }
  1604. again:
  1605. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  1606. /*
  1607. * If the first lock we have locked is in the AIL, we must TRY to get
  1608. * the second lock. If we can't get it, we must release the first one
  1609. * and try again.
  1610. */
  1611. lp = (xfs_log_item_t *)ip0->i_itemp;
  1612. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1613. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  1614. xfs_iunlock(ip0, lock_mode);
  1615. if ((++attempts % 5) == 0)
  1616. delay(1); /* Don't just spin the CPU */
  1617. goto again;
  1618. }
  1619. } else {
  1620. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  1621. }
  1622. }
  1623. int
  1624. xfs_remove(
  1625. xfs_inode_t *dp,
  1626. struct xfs_name *name,
  1627. xfs_inode_t *ip)
  1628. {
  1629. xfs_mount_t *mp = dp->i_mount;
  1630. xfs_trans_t *tp = NULL;
  1631. int is_dir = S_ISDIR(ip->i_d.di_mode);
  1632. int error = 0;
  1633. xfs_bmap_free_t free_list;
  1634. xfs_fsblock_t first_block;
  1635. int cancel_flags;
  1636. int committed;
  1637. int link_zero;
  1638. uint resblks;
  1639. uint log_count;
  1640. xfs_itrace_entry(dp);
  1641. xfs_itrace_entry(ip);
  1642. if (XFS_FORCED_SHUTDOWN(mp))
  1643. return XFS_ERROR(EIO);
  1644. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  1645. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
  1646. NULL, DM_RIGHT_NULL, name->name, NULL,
  1647. ip->i_d.di_mode, 0, 0);
  1648. if (error)
  1649. return error;
  1650. }
  1651. error = xfs_qm_dqattach(dp, 0);
  1652. if (error)
  1653. goto std_return;
  1654. error = xfs_qm_dqattach(ip, 0);
  1655. if (error)
  1656. goto std_return;
  1657. if (is_dir) {
  1658. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  1659. log_count = XFS_DEFAULT_LOG_COUNT;
  1660. } else {
  1661. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  1662. log_count = XFS_REMOVE_LOG_COUNT;
  1663. }
  1664. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1665. /*
  1666. * We try to get the real space reservation first,
  1667. * allowing for directory btree deletion(s) implying
  1668. * possible bmap insert(s). If we can't get the space
  1669. * reservation then we use 0 instead, and avoid the bmap
  1670. * btree insert(s) in the directory code by, if the bmap
  1671. * insert tries to happen, instead trimming the LAST
  1672. * block from the directory.
  1673. */
  1674. resblks = XFS_REMOVE_SPACE_RES(mp);
  1675. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  1676. XFS_TRANS_PERM_LOG_RES, log_count);
  1677. if (error == ENOSPC) {
  1678. resblks = 0;
  1679. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  1680. XFS_TRANS_PERM_LOG_RES, log_count);
  1681. }
  1682. if (error) {
  1683. ASSERT(error != ENOSPC);
  1684. cancel_flags = 0;
  1685. goto out_trans_cancel;
  1686. }
  1687. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  1688. /*
  1689. * At this point, we've gotten both the directory and the entry
  1690. * inodes locked.
  1691. */
  1692. IHOLD(ip);
  1693. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1694. IHOLD(dp);
  1695. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1696. /*
  1697. * If we're removing a directory perform some additional validation.
  1698. */
  1699. if (is_dir) {
  1700. ASSERT(ip->i_d.di_nlink >= 2);
  1701. if (ip->i_d.di_nlink != 2) {
  1702. error = XFS_ERROR(ENOTEMPTY);
  1703. goto out_trans_cancel;
  1704. }
  1705. if (!xfs_dir_isempty(ip)) {
  1706. error = XFS_ERROR(ENOTEMPTY);
  1707. goto out_trans_cancel;
  1708. }
  1709. }
  1710. xfs_bmap_init(&free_list, &first_block);
  1711. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  1712. &first_block, &free_list, resblks);
  1713. if (error) {
  1714. ASSERT(error != ENOENT);
  1715. goto out_bmap_cancel;
  1716. }
  1717. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1718. if (is_dir) {
  1719. /*
  1720. * Drop the link from ip's "..".
  1721. */
  1722. error = xfs_droplink(tp, dp);
  1723. if (error)
  1724. goto out_bmap_cancel;
  1725. /*
  1726. * Drop the "." link from ip to self.
  1727. */
  1728. error = xfs_droplink(tp, ip);
  1729. if (error)
  1730. goto out_bmap_cancel;
  1731. } else {
  1732. /*
  1733. * When removing a non-directory we need to log the parent
  1734. * inode here. For a directory this is done implicitly
  1735. * by the xfs_droplink call for the ".." entry.
  1736. */
  1737. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1738. }
  1739. /*
  1740. * Drop the link from dp to ip.
  1741. */
  1742. error = xfs_droplink(tp, ip);
  1743. if (error)
  1744. goto out_bmap_cancel;
  1745. /*
  1746. * Determine if this is the last link while
  1747. * we are in the transaction.
  1748. */
  1749. link_zero = (ip->i_d.di_nlink == 0);
  1750. /*
  1751. * If this is a synchronous mount, make sure that the
  1752. * remove transaction goes to disk before returning to
  1753. * the user.
  1754. */
  1755. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1756. xfs_trans_set_sync(tp);
  1757. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1758. if (error)
  1759. goto out_bmap_cancel;
  1760. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1761. if (error)
  1762. goto std_return;
  1763. /*
  1764. * If we are using filestreams, kill the stream association.
  1765. * If the file is still open it may get a new one but that
  1766. * will get killed on last close in xfs_close() so we don't
  1767. * have to worry about that.
  1768. */
  1769. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  1770. xfs_filestream_deassociate(ip);
  1771. std_return:
  1772. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  1773. XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
  1774. NULL, DM_RIGHT_NULL, name->name, NULL,
  1775. ip->i_d.di_mode, error, 0);
  1776. }
  1777. return error;
  1778. out_bmap_cancel:
  1779. xfs_bmap_cancel(&free_list);
  1780. cancel_flags |= XFS_TRANS_ABORT;
  1781. out_trans_cancel:
  1782. xfs_trans_cancel(tp, cancel_flags);
  1783. goto std_return;
  1784. }
  1785. int
  1786. xfs_link(
  1787. xfs_inode_t *tdp,
  1788. xfs_inode_t *sip,
  1789. struct xfs_name *target_name)
  1790. {
  1791. xfs_mount_t *mp = tdp->i_mount;
  1792. xfs_trans_t *tp;
  1793. int error;
  1794. xfs_bmap_free_t free_list;
  1795. xfs_fsblock_t first_block;
  1796. int cancel_flags;
  1797. int committed;
  1798. int resblks;
  1799. xfs_itrace_entry(tdp);
  1800. xfs_itrace_entry(sip);
  1801. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1802. if (XFS_FORCED_SHUTDOWN(mp))
  1803. return XFS_ERROR(EIO);
  1804. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  1805. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  1806. tdp, DM_RIGHT_NULL,
  1807. sip, DM_RIGHT_NULL,
  1808. target_name->name, NULL, 0, 0, 0);
  1809. if (error)
  1810. return error;
  1811. }
  1812. /* Return through std_return after this point. */
  1813. error = xfs_qm_dqattach(sip, 0);
  1814. if (error)
  1815. goto std_return;
  1816. error = xfs_qm_dqattach(tdp, 0);
  1817. if (error)
  1818. goto std_return;
  1819. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1820. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1821. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1822. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  1823. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1824. if (error == ENOSPC) {
  1825. resblks = 0;
  1826. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  1827. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1828. }
  1829. if (error) {
  1830. cancel_flags = 0;
  1831. goto error_return;
  1832. }
  1833. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1834. /*
  1835. * Increment vnode ref counts since xfs_trans_commit &
  1836. * xfs_trans_cancel will both unlock the inodes and
  1837. * decrement the associated ref counts.
  1838. */
  1839. IHOLD(sip);
  1840. IHOLD(tdp);
  1841. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1842. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1843. /*
  1844. * If the source has too many links, we can't make any more to it.
  1845. */
  1846. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  1847. error = XFS_ERROR(EMLINK);
  1848. goto error_return;
  1849. }
  1850. /*
  1851. * If we are using project inheritance, we only allow hard link
  1852. * creation in our tree when the project IDs are the same; else
  1853. * the tree quota mechanism could be circumvented.
  1854. */
  1855. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1856. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  1857. error = XFS_ERROR(EXDEV);
  1858. goto error_return;
  1859. }
  1860. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  1861. if (error)
  1862. goto error_return;
  1863. xfs_bmap_init(&free_list, &first_block);
  1864. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1865. &first_block, &free_list, resblks);
  1866. if (error)
  1867. goto abort_return;
  1868. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1869. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1870. error = xfs_bumplink(tp, sip);
  1871. if (error)
  1872. goto abort_return;
  1873. /*
  1874. * If this is a synchronous mount, make sure that the
  1875. * link transaction goes to disk before returning to
  1876. * the user.
  1877. */
  1878. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1879. xfs_trans_set_sync(tp);
  1880. }
  1881. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1882. if (error) {
  1883. xfs_bmap_cancel(&free_list);
  1884. goto abort_return;
  1885. }
  1886. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1887. if (error)
  1888. goto std_return;
  1889. /* Fall through to std_return with error = 0. */
  1890. std_return:
  1891. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  1892. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  1893. tdp, DM_RIGHT_NULL,
  1894. sip, DM_RIGHT_NULL,
  1895. target_name->name, NULL, 0, error, 0);
  1896. }
  1897. return error;
  1898. abort_return:
  1899. cancel_flags |= XFS_TRANS_ABORT;
  1900. /* FALLTHROUGH */
  1901. error_return:
  1902. xfs_trans_cancel(tp, cancel_flags);
  1903. goto std_return;
  1904. }
  1905. int
  1906. xfs_symlink(
  1907. xfs_inode_t *dp,
  1908. struct xfs_name *link_name,
  1909. const char *target_path,
  1910. mode_t mode,
  1911. xfs_inode_t **ipp,
  1912. cred_t *credp)
  1913. {
  1914. xfs_mount_t *mp = dp->i_mount;
  1915. xfs_trans_t *tp;
  1916. xfs_inode_t *ip;
  1917. int error;
  1918. int pathlen;
  1919. xfs_bmap_free_t free_list;
  1920. xfs_fsblock_t first_block;
  1921. boolean_t unlock_dp_on_error = B_FALSE;
  1922. uint cancel_flags;
  1923. int committed;
  1924. xfs_fileoff_t first_fsb;
  1925. xfs_filblks_t fs_blocks;
  1926. int nmaps;
  1927. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1928. xfs_daddr_t d;
  1929. const char *cur_chunk;
  1930. int byte_cnt;
  1931. int n;
  1932. xfs_buf_t *bp;
  1933. xfs_prid_t prid;
  1934. struct xfs_dquot *udqp, *gdqp;
  1935. uint resblks;
  1936. *ipp = NULL;
  1937. error = 0;
  1938. ip = NULL;
  1939. tp = NULL;
  1940. xfs_itrace_entry(dp);
  1941. if (XFS_FORCED_SHUTDOWN(mp))
  1942. return XFS_ERROR(EIO);
  1943. /*
  1944. * Check component lengths of the target path name.
  1945. */
  1946. pathlen = strlen(target_path);
  1947. if (pathlen >= MAXPATHLEN) /* total string too long */
  1948. return XFS_ERROR(ENAMETOOLONG);
  1949. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  1950. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
  1951. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  1952. link_name->name,
  1953. (unsigned char *)target_path, 0, 0, 0);
  1954. if (error)
  1955. return error;
  1956. }
  1957. /* Return through std_return after this point. */
  1958. udqp = gdqp = NULL;
  1959. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1960. prid = dp->i_d.di_projid;
  1961. else
  1962. prid = (xfs_prid_t)dfltprid;
  1963. /*
  1964. * Make sure that we have allocated dquot(s) on disk.
  1965. */
  1966. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  1967. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1968. if (error)
  1969. goto std_return;
  1970. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  1971. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1972. /*
  1973. * The symlink will fit into the inode data fork?
  1974. * There can't be any attributes so we get the whole variable part.
  1975. */
  1976. if (pathlen <= XFS_LITINO(mp))
  1977. fs_blocks = 0;
  1978. else
  1979. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  1980. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  1981. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  1982. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  1983. if (error == ENOSPC && fs_blocks == 0) {
  1984. resblks = 0;
  1985. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  1986. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  1987. }
  1988. if (error) {
  1989. cancel_flags = 0;
  1990. goto error_return;
  1991. }
  1992. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1993. unlock_dp_on_error = B_TRUE;
  1994. /*
  1995. * Check whether the directory allows new symlinks or not.
  1996. */
  1997. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  1998. error = XFS_ERROR(EPERM);
  1999. goto error_return;
  2000. }
  2001. /*
  2002. * Reserve disk quota : blocks and inode.
  2003. */
  2004. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  2005. if (error)
  2006. goto error_return;
  2007. /*
  2008. * Check for ability to enter directory entry, if no space reserved.
  2009. */
  2010. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  2011. if (error)
  2012. goto error_return;
  2013. /*
  2014. * Initialize the bmap freelist prior to calling either
  2015. * bmapi or the directory create code.
  2016. */
  2017. xfs_bmap_init(&free_list, &first_block);
  2018. /*
  2019. * Allocate an inode for the symlink.
  2020. */
  2021. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2022. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2023. if (error) {
  2024. if (error == ENOSPC)
  2025. goto error_return;
  2026. goto error1;
  2027. }
  2028. /*
  2029. * An error after we've joined dp to the transaction will result in the
  2030. * transaction cancel unlocking dp so don't do it explicitly in the
  2031. * error path.
  2032. */
  2033. IHOLD(dp);
  2034. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2035. unlock_dp_on_error = B_FALSE;
  2036. /*
  2037. * Also attach the dquot(s) to it, if applicable.
  2038. */
  2039. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  2040. if (resblks)
  2041. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2042. /*
  2043. * If the symlink will fit into the inode, write it inline.
  2044. */
  2045. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2046. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2047. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2048. ip->i_d.di_size = pathlen;
  2049. /*
  2050. * The inode was initially created in extent format.
  2051. */
  2052. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2053. ip->i_df.if_flags |= XFS_IFINLINE;
  2054. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2055. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2056. } else {
  2057. first_fsb = 0;
  2058. nmaps = SYMLINK_MAPS;
  2059. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2060. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2061. &first_block, resblks, mval, &nmaps,
  2062. &free_list, NULL);
  2063. if (error) {
  2064. goto error1;
  2065. }
  2066. if (resblks)
  2067. resblks -= fs_blocks;
  2068. ip->i_d.di_size = pathlen;
  2069. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2070. cur_chunk = target_path;
  2071. for (n = 0; n < nmaps; n++) {
  2072. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2073. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2074. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2075. BTOBB(byte_cnt), 0);
  2076. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2077. if (pathlen < byte_cnt) {
  2078. byte_cnt = pathlen;
  2079. }
  2080. pathlen -= byte_cnt;
  2081. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2082. cur_chunk += byte_cnt;
  2083. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2084. }
  2085. }
  2086. /*
  2087. * Create the directory entry for the symlink.
  2088. */
  2089. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  2090. &first_block, &free_list, resblks);
  2091. if (error)
  2092. goto error1;
  2093. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2094. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2095. /*
  2096. * If this is a synchronous mount, make sure that the
  2097. * symlink transaction goes to disk before returning to
  2098. * the user.
  2099. */
  2100. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2101. xfs_trans_set_sync(tp);
  2102. }
  2103. /*
  2104. * xfs_trans_commit normally decrements the vnode ref count
  2105. * when it unlocks the inode. Since we want to return the
  2106. * vnode to the caller, we bump the vnode ref count now.
  2107. */
  2108. IHOLD(ip);
  2109. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2110. if (error) {
  2111. goto error2;
  2112. }
  2113. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2114. xfs_qm_dqrele(udqp);
  2115. xfs_qm_dqrele(gdqp);
  2116. /* Fall through to std_return with error = 0 or errno from
  2117. * xfs_trans_commit */
  2118. std_return:
  2119. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  2120. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  2121. dp, DM_RIGHT_NULL,
  2122. error ? NULL : ip,
  2123. DM_RIGHT_NULL, link_name->name,
  2124. (unsigned char *)target_path,
  2125. 0, error, 0);
  2126. }
  2127. if (!error)
  2128. *ipp = ip;
  2129. return error;
  2130. error2:
  2131. IRELE(ip);
  2132. error1:
  2133. xfs_bmap_cancel(&free_list);
  2134. cancel_flags |= XFS_TRANS_ABORT;
  2135. error_return:
  2136. xfs_trans_cancel(tp, cancel_flags);
  2137. xfs_qm_dqrele(udqp);
  2138. xfs_qm_dqrele(gdqp);
  2139. if (unlock_dp_on_error)
  2140. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2141. goto std_return;
  2142. }
  2143. int
  2144. xfs_set_dmattrs(
  2145. xfs_inode_t *ip,
  2146. u_int evmask,
  2147. u_int16_t state)
  2148. {
  2149. xfs_mount_t *mp = ip->i_mount;
  2150. xfs_trans_t *tp;
  2151. int error;
  2152. if (!capable(CAP_SYS_ADMIN))
  2153. return XFS_ERROR(EPERM);
  2154. if (XFS_FORCED_SHUTDOWN(mp))
  2155. return XFS_ERROR(EIO);
  2156. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  2157. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  2158. if (error) {
  2159. xfs_trans_cancel(tp, 0);
  2160. return error;
  2161. }
  2162. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2163. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2164. ip->i_d.di_dmevmask = evmask;
  2165. ip->i_d.di_dmstate = state;
  2166. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2167. IHOLD(ip);
  2168. error = xfs_trans_commit(tp, 0);
  2169. return error;
  2170. }
  2171. /*
  2172. * xfs_alloc_file_space()
  2173. * This routine allocates disk space for the given file.
  2174. *
  2175. * If alloc_type == 0, this request is for an ALLOCSP type
  2176. * request which will change the file size. In this case, no
  2177. * DMAPI event will be generated by the call. A TRUNCATE event
  2178. * will be generated later by xfs_setattr.
  2179. *
  2180. * If alloc_type != 0, this request is for a RESVSP type
  2181. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  2182. * lower block boundary byte address is less than the file's
  2183. * length.
  2184. *
  2185. * RETURNS:
  2186. * 0 on success
  2187. * errno on error
  2188. *
  2189. */
  2190. STATIC int
  2191. xfs_alloc_file_space(
  2192. xfs_inode_t *ip,
  2193. xfs_off_t offset,
  2194. xfs_off_t len,
  2195. int alloc_type,
  2196. int attr_flags)
  2197. {
  2198. xfs_mount_t *mp = ip->i_mount;
  2199. xfs_off_t count;
  2200. xfs_filblks_t allocated_fsb;
  2201. xfs_filblks_t allocatesize_fsb;
  2202. xfs_extlen_t extsz, temp;
  2203. xfs_fileoff_t startoffset_fsb;
  2204. xfs_fsblock_t firstfsb;
  2205. int nimaps;
  2206. int bmapi_flag;
  2207. int quota_flag;
  2208. int rt;
  2209. xfs_trans_t *tp;
  2210. xfs_bmbt_irec_t imaps[1], *imapp;
  2211. xfs_bmap_free_t free_list;
  2212. uint qblocks, resblks, resrtextents;
  2213. int committed;
  2214. int error;
  2215. xfs_itrace_entry(ip);
  2216. if (XFS_FORCED_SHUTDOWN(mp))
  2217. return XFS_ERROR(EIO);
  2218. error = xfs_qm_dqattach(ip, 0);
  2219. if (error)
  2220. return error;
  2221. if (len <= 0)
  2222. return XFS_ERROR(EINVAL);
  2223. rt = XFS_IS_REALTIME_INODE(ip);
  2224. extsz = xfs_get_extsz_hint(ip);
  2225. count = len;
  2226. imapp = &imaps[0];
  2227. nimaps = 1;
  2228. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  2229. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  2230. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  2231. /* Generate a DMAPI event if needed. */
  2232. if (alloc_type != 0 && offset < ip->i_size &&
  2233. (attr_flags & XFS_ATTR_DMI) == 0 &&
  2234. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  2235. xfs_off_t end_dmi_offset;
  2236. end_dmi_offset = offset+len;
  2237. if (end_dmi_offset > ip->i_size)
  2238. end_dmi_offset = ip->i_size;
  2239. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
  2240. end_dmi_offset - offset, 0, NULL);
  2241. if (error)
  2242. return error;
  2243. }
  2244. /*
  2245. * Allocate file space until done or until there is an error
  2246. */
  2247. retry:
  2248. while (allocatesize_fsb && !error) {
  2249. xfs_fileoff_t s, e;
  2250. /*
  2251. * Determine space reservations for data/realtime.
  2252. */
  2253. if (unlikely(extsz)) {
  2254. s = startoffset_fsb;
  2255. do_div(s, extsz);
  2256. s *= extsz;
  2257. e = startoffset_fsb + allocatesize_fsb;
  2258. if ((temp = do_mod(startoffset_fsb, extsz)))
  2259. e += temp;
  2260. if ((temp = do_mod(e, extsz)))
  2261. e += extsz - temp;
  2262. } else {
  2263. s = 0;
  2264. e = allocatesize_fsb;
  2265. }
  2266. if (unlikely(rt)) {
  2267. resrtextents = qblocks = (uint)(e - s);
  2268. resrtextents /= mp->m_sb.sb_rextsize;
  2269. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  2270. quota_flag = XFS_QMOPT_RES_RTBLKS;
  2271. } else {
  2272. resrtextents = 0;
  2273. resblks = qblocks = \
  2274. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  2275. quota_flag = XFS_QMOPT_RES_REGBLKS;
  2276. }
  2277. /*
  2278. * Allocate and setup the transaction.
  2279. */
  2280. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  2281. error = xfs_trans_reserve(tp, resblks,
  2282. XFS_WRITE_LOG_RES(mp), resrtextents,
  2283. XFS_TRANS_PERM_LOG_RES,
  2284. XFS_WRITE_LOG_COUNT);
  2285. /*
  2286. * Check for running out of space
  2287. */
  2288. if (error) {
  2289. /*
  2290. * Free the transaction structure.
  2291. */
  2292. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  2293. xfs_trans_cancel(tp, 0);
  2294. break;
  2295. }
  2296. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2297. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  2298. 0, quota_flag);
  2299. if (error)
  2300. goto error1;
  2301. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2302. xfs_trans_ihold(tp, ip);
  2303. /*
  2304. * Issue the xfs_bmapi() call to allocate the blocks
  2305. */
  2306. xfs_bmap_init(&free_list, &firstfsb);
  2307. error = xfs_bmapi(tp, ip, startoffset_fsb,
  2308. allocatesize_fsb, bmapi_flag,
  2309. &firstfsb, 0, imapp, &nimaps,
  2310. &free_list, NULL);
  2311. if (error) {
  2312. goto error0;
  2313. }
  2314. /*
  2315. * Complete the transaction
  2316. */
  2317. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2318. if (error) {
  2319. goto error0;
  2320. }
  2321. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2322. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2323. if (error) {
  2324. break;
  2325. }
  2326. allocated_fsb = imapp->br_blockcount;
  2327. if (nimaps == 0) {
  2328. error = XFS_ERROR(ENOSPC);
  2329. break;
  2330. }
  2331. startoffset_fsb += allocated_fsb;
  2332. allocatesize_fsb -= allocated_fsb;
  2333. }
  2334. dmapi_enospc_check:
  2335. if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
  2336. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  2337. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  2338. ip, DM_RIGHT_NULL,
  2339. ip, DM_RIGHT_NULL,
  2340. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  2341. if (error == 0)
  2342. goto retry; /* Maybe DMAPI app. has made space */
  2343. /* else fall through with error from XFS_SEND_DATA */
  2344. }
  2345. return error;
  2346. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  2347. xfs_bmap_cancel(&free_list);
  2348. xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
  2349. error1: /* Just cancel transaction */
  2350. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  2351. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2352. goto dmapi_enospc_check;
  2353. }
  2354. /*
  2355. * Zero file bytes between startoff and endoff inclusive.
  2356. * The iolock is held exclusive and no blocks are buffered.
  2357. *
  2358. * This function is used by xfs_free_file_space() to zero
  2359. * partial blocks when the range to free is not block aligned.
  2360. * When unreserving space with boundaries that are not block
  2361. * aligned we round up the start and round down the end
  2362. * boundaries and then use this function to zero the parts of
  2363. * the blocks that got dropped during the rounding.
  2364. */
  2365. STATIC int
  2366. xfs_zero_remaining_bytes(
  2367. xfs_inode_t *ip,
  2368. xfs_off_t startoff,
  2369. xfs_off_t endoff)
  2370. {
  2371. xfs_bmbt_irec_t imap;
  2372. xfs_fileoff_t offset_fsb;
  2373. xfs_off_t lastoffset;
  2374. xfs_off_t offset;
  2375. xfs_buf_t *bp;
  2376. xfs_mount_t *mp = ip->i_mount;
  2377. int nimap;
  2378. int error = 0;
  2379. /*
  2380. * Avoid doing I/O beyond eof - it's not necessary
  2381. * since nothing can read beyond eof. The space will
  2382. * be zeroed when the file is extended anyway.
  2383. */
  2384. if (startoff >= ip->i_size)
  2385. return 0;
  2386. if (endoff > ip->i_size)
  2387. endoff = ip->i_size;
  2388. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  2389. XFS_IS_REALTIME_INODE(ip) ?
  2390. mp->m_rtdev_targp : mp->m_ddev_targp);
  2391. if (!bp)
  2392. return XFS_ERROR(ENOMEM);
  2393. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  2394. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  2395. nimap = 1;
  2396. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  2397. NULL, 0, &imap, &nimap, NULL, NULL);
  2398. if (error || nimap < 1)
  2399. break;
  2400. ASSERT(imap.br_blockcount >= 1);
  2401. ASSERT(imap.br_startoff == offset_fsb);
  2402. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  2403. if (lastoffset > endoff)
  2404. lastoffset = endoff;
  2405. if (imap.br_startblock == HOLESTARTBLOCK)
  2406. continue;
  2407. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2408. if (imap.br_state == XFS_EXT_UNWRITTEN)
  2409. continue;
  2410. XFS_BUF_UNDONE(bp);
  2411. XFS_BUF_UNWRITE(bp);
  2412. XFS_BUF_READ(bp);
  2413. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  2414. xfsbdstrat(mp, bp);
  2415. error = xfs_iowait(bp);
  2416. if (error) {
  2417. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  2418. mp, bp, XFS_BUF_ADDR(bp));
  2419. break;
  2420. }
  2421. memset(XFS_BUF_PTR(bp) +
  2422. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  2423. 0, lastoffset - offset + 1);
  2424. XFS_BUF_UNDONE(bp);
  2425. XFS_BUF_UNREAD(bp);
  2426. XFS_BUF_WRITE(bp);
  2427. xfsbdstrat(mp, bp);
  2428. error = xfs_iowait(bp);
  2429. if (error) {
  2430. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  2431. mp, bp, XFS_BUF_ADDR(bp));
  2432. break;
  2433. }
  2434. }
  2435. xfs_buf_free(bp);
  2436. return error;
  2437. }
  2438. /*
  2439. * xfs_free_file_space()
  2440. * This routine frees disk space for the given file.
  2441. *
  2442. * This routine is only called by xfs_change_file_space
  2443. * for an UNRESVSP type call.
  2444. *
  2445. * RETURNS:
  2446. * 0 on success
  2447. * errno on error
  2448. *
  2449. */
  2450. STATIC int
  2451. xfs_free_file_space(
  2452. xfs_inode_t *ip,
  2453. xfs_off_t offset,
  2454. xfs_off_t len,
  2455. int attr_flags)
  2456. {
  2457. int committed;
  2458. int done;
  2459. xfs_off_t end_dmi_offset;
  2460. xfs_fileoff_t endoffset_fsb;
  2461. int error;
  2462. xfs_fsblock_t firstfsb;
  2463. xfs_bmap_free_t free_list;
  2464. xfs_bmbt_irec_t imap;
  2465. xfs_off_t ioffset;
  2466. xfs_extlen_t mod=0;
  2467. xfs_mount_t *mp;
  2468. int nimap;
  2469. uint resblks;
  2470. uint rounding;
  2471. int rt;
  2472. xfs_fileoff_t startoffset_fsb;
  2473. xfs_trans_t *tp;
  2474. int need_iolock = 1;
  2475. mp = ip->i_mount;
  2476. xfs_itrace_entry(ip);
  2477. error = xfs_qm_dqattach(ip, 0);
  2478. if (error)
  2479. return error;
  2480. error = 0;
  2481. if (len <= 0) /* if nothing being freed */
  2482. return error;
  2483. rt = XFS_IS_REALTIME_INODE(ip);
  2484. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  2485. end_dmi_offset = offset + len;
  2486. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  2487. if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
  2488. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  2489. if (end_dmi_offset > ip->i_size)
  2490. end_dmi_offset = ip->i_size;
  2491. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
  2492. offset, end_dmi_offset - offset,
  2493. AT_DELAY_FLAG(attr_flags), NULL);
  2494. if (error)
  2495. return error;
  2496. }
  2497. if (attr_flags & XFS_ATTR_NOLOCK)
  2498. need_iolock = 0;
  2499. if (need_iolock) {
  2500. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  2501. /* wait for the completion of any pending DIOs */
  2502. xfs_ioend_wait(ip);
  2503. }
  2504. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  2505. ioffset = offset & ~(rounding - 1);
  2506. if (VN_CACHED(VFS_I(ip)) != 0) {
  2507. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  2508. if (error)
  2509. goto out_unlock_iolock;
  2510. }
  2511. /*
  2512. * Need to zero the stuff we're not freeing, on disk.
  2513. * If it's a realtime file & can't use unwritten extents then we
  2514. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  2515. * will take care of it for us.
  2516. */
  2517. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  2518. nimap = 1;
  2519. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  2520. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  2521. if (error)
  2522. goto out_unlock_iolock;
  2523. ASSERT(nimap == 0 || nimap == 1);
  2524. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  2525. xfs_daddr_t block;
  2526. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2527. block = imap.br_startblock;
  2528. mod = do_div(block, mp->m_sb.sb_rextsize);
  2529. if (mod)
  2530. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  2531. }
  2532. nimap = 1;
  2533. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  2534. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  2535. if (error)
  2536. goto out_unlock_iolock;
  2537. ASSERT(nimap == 0 || nimap == 1);
  2538. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  2539. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2540. mod++;
  2541. if (mod && (mod != mp->m_sb.sb_rextsize))
  2542. endoffset_fsb -= mod;
  2543. }
  2544. }
  2545. if ((done = (endoffset_fsb <= startoffset_fsb)))
  2546. /*
  2547. * One contiguous piece to clear
  2548. */
  2549. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  2550. else {
  2551. /*
  2552. * Some full blocks, possibly two pieces to clear
  2553. */
  2554. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  2555. error = xfs_zero_remaining_bytes(ip, offset,
  2556. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  2557. if (!error &&
  2558. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  2559. error = xfs_zero_remaining_bytes(ip,
  2560. XFS_FSB_TO_B(mp, endoffset_fsb),
  2561. offset + len - 1);
  2562. }
  2563. /*
  2564. * free file space until done or until there is an error
  2565. */
  2566. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  2567. while (!error && !done) {
  2568. /*
  2569. * allocate and setup the transaction. Allow this
  2570. * transaction to dip into the reserve blocks to ensure
  2571. * the freeing of the space succeeds at ENOSPC.
  2572. */
  2573. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  2574. tp->t_flags |= XFS_TRANS_RESERVE;
  2575. error = xfs_trans_reserve(tp,
  2576. resblks,
  2577. XFS_WRITE_LOG_RES(mp),
  2578. 0,
  2579. XFS_TRANS_PERM_LOG_RES,
  2580. XFS_WRITE_LOG_COUNT);
  2581. /*
  2582. * check for running out of space
  2583. */
  2584. if (error) {
  2585. /*
  2586. * Free the transaction structure.
  2587. */
  2588. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  2589. xfs_trans_cancel(tp, 0);
  2590. break;
  2591. }
  2592. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2593. error = xfs_trans_reserve_quota(tp, mp,
  2594. ip->i_udquot, ip->i_gdquot,
  2595. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  2596. if (error)
  2597. goto error1;
  2598. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2599. xfs_trans_ihold(tp, ip);
  2600. /*
  2601. * issue the bunmapi() call to free the blocks
  2602. */
  2603. xfs_bmap_init(&free_list, &firstfsb);
  2604. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  2605. endoffset_fsb - startoffset_fsb,
  2606. 0, 2, &firstfsb, &free_list, NULL, &done);
  2607. if (error) {
  2608. goto error0;
  2609. }
  2610. /*
  2611. * complete the transaction
  2612. */
  2613. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2614. if (error) {
  2615. goto error0;
  2616. }
  2617. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2618. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2619. }
  2620. out_unlock_iolock:
  2621. if (need_iolock)
  2622. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  2623. return error;
  2624. error0:
  2625. xfs_bmap_cancel(&free_list);
  2626. error1:
  2627. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  2628. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  2629. XFS_ILOCK_EXCL);
  2630. return error;
  2631. }
  2632. /*
  2633. * xfs_change_file_space()
  2634. * This routine allocates or frees disk space for the given file.
  2635. * The user specified parameters are checked for alignment and size
  2636. * limitations.
  2637. *
  2638. * RETURNS:
  2639. * 0 on success
  2640. * errno on error
  2641. *
  2642. */
  2643. int
  2644. xfs_change_file_space(
  2645. xfs_inode_t *ip,
  2646. int cmd,
  2647. xfs_flock64_t *bf,
  2648. xfs_off_t offset,
  2649. int attr_flags)
  2650. {
  2651. xfs_mount_t *mp = ip->i_mount;
  2652. int clrprealloc;
  2653. int error;
  2654. xfs_fsize_t fsize;
  2655. int setprealloc;
  2656. xfs_off_t startoffset;
  2657. xfs_off_t llen;
  2658. xfs_trans_t *tp;
  2659. struct iattr iattr;
  2660. xfs_itrace_entry(ip);
  2661. if (!S_ISREG(ip->i_d.di_mode))
  2662. return XFS_ERROR(EINVAL);
  2663. switch (bf->l_whence) {
  2664. case 0: /*SEEK_SET*/
  2665. break;
  2666. case 1: /*SEEK_CUR*/
  2667. bf->l_start += offset;
  2668. break;
  2669. case 2: /*SEEK_END*/
  2670. bf->l_start += ip->i_size;
  2671. break;
  2672. default:
  2673. return XFS_ERROR(EINVAL);
  2674. }
  2675. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  2676. if ( (bf->l_start < 0)
  2677. || (bf->l_start > XFS_MAXIOFFSET(mp))
  2678. || (bf->l_start + llen < 0)
  2679. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  2680. return XFS_ERROR(EINVAL);
  2681. bf->l_whence = 0;
  2682. startoffset = bf->l_start;
  2683. fsize = ip->i_size;
  2684. /*
  2685. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  2686. * file space.
  2687. * These calls do NOT zero the data space allocated to the file,
  2688. * nor do they change the file size.
  2689. *
  2690. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  2691. * space.
  2692. * These calls cause the new file data to be zeroed and the file
  2693. * size to be changed.
  2694. */
  2695. setprealloc = clrprealloc = 0;
  2696. switch (cmd) {
  2697. case XFS_IOC_RESVSP:
  2698. case XFS_IOC_RESVSP64:
  2699. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  2700. 1, attr_flags);
  2701. if (error)
  2702. return error;
  2703. setprealloc = 1;
  2704. break;
  2705. case XFS_IOC_UNRESVSP:
  2706. case XFS_IOC_UNRESVSP64:
  2707. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  2708. attr_flags)))
  2709. return error;
  2710. break;
  2711. case XFS_IOC_ALLOCSP:
  2712. case XFS_IOC_ALLOCSP64:
  2713. case XFS_IOC_FREESP:
  2714. case XFS_IOC_FREESP64:
  2715. if (startoffset > fsize) {
  2716. error = xfs_alloc_file_space(ip, fsize,
  2717. startoffset - fsize, 0, attr_flags);
  2718. if (error)
  2719. break;
  2720. }
  2721. iattr.ia_valid = ATTR_SIZE;
  2722. iattr.ia_size = startoffset;
  2723. error = xfs_setattr(ip, &iattr, attr_flags);
  2724. if (error)
  2725. return error;
  2726. clrprealloc = 1;
  2727. break;
  2728. default:
  2729. ASSERT(0);
  2730. return XFS_ERROR(EINVAL);
  2731. }
  2732. /*
  2733. * update the inode timestamp, mode, and prealloc flag bits
  2734. */
  2735. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  2736. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  2737. 0, 0, 0))) {
  2738. /* ASSERT(0); */
  2739. xfs_trans_cancel(tp, 0);
  2740. return error;
  2741. }
  2742. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2743. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2744. xfs_trans_ihold(tp, ip);
  2745. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  2746. ip->i_d.di_mode &= ~S_ISUID;
  2747. /*
  2748. * Note that we don't have to worry about mandatory
  2749. * file locking being disabled here because we only
  2750. * clear the S_ISGID bit if the Group execute bit is
  2751. * on, but if it was on then mandatory locking wouldn't
  2752. * have been enabled.
  2753. */
  2754. if (ip->i_d.di_mode & S_IXGRP)
  2755. ip->i_d.di_mode &= ~S_ISGID;
  2756. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2757. }
  2758. if (setprealloc)
  2759. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  2760. else if (clrprealloc)
  2761. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  2762. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2763. xfs_trans_set_sync(tp);
  2764. error = xfs_trans_commit(tp, 0);
  2765. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2766. return error;
  2767. }