xfs_vnodeops.c 78 KB

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