xfs_vnodeops.c 79 KB

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